Skip to content

Commit

Permalink
Only call Que.migrate! once
Browse files Browse the repository at this point in the history
  • Loading branch information
imjoehaines committed Dec 14, 2023
1 parent 0532c85 commit 1341a55
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 79 deletions.
78 changes: 1 addition & 77 deletions features/fixtures/que/app/app.rb
Original file line number Diff line number Diff line change
@@ -1,79 +1,3 @@
require 'pg'
require 'que'
require 'socket'
require 'bugsnag'
require 'active_record'
require_relative "setup-que"

QUE_VERSION = ENV.fetch("QUE_VERSION")

Bugsnag.configure do |config|
puts "Configuring `api_key` to #{ENV['BUGSNAG_API_KEY']}"
config.api_key = ENV['BUGSNAG_API_KEY']
puts "Configuring `endpoint` to #{ENV['BUGSNAG_ENDPOINT']}"
config.endpoint = ENV['BUGSNAG_ENDPOINT']
end

postgres_ready = false
attempts = 0
MAX_ATTEMPTS = 10

until postgres_ready || attempts >= MAX_ATTEMPTS
begin
Timeout::timeout(5) { TCPSocket.new('postgres', 5432).close }

postgres_ready = true
rescue Exception
attempts += 1
sleep 1
end
end

raise 'postgres was not ready in time!' unless postgres_ready

ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
database: 'postgres',
username: 'postgres',
password: 'test_password',
host: 'postgres'
)

Que.connection = ActiveRecord
Que.migrate!(version: Que::Migrations::CURRENT_VERSION)

# Workaround a bug in que/pg
# see https://github.com/que-rb/que/issues/247
if QUE_VERSION == '0.14'
Que::Adapters::Base::CAST_PROCS[1184] = lambda do |value|
case value
when Time then value
when String then Time.parse(value)
else raise "Unexpected time class: #{value.class} (#{value.inspect})"
end
end
end

class UnhandledJob < Que::Job
def run
raise RuntimeError.new("Unhandled error")
end

def handle_error(error)
destroy
end
end

class HandledJob < Que::Job
def run
raise RuntimeError.new("Handled error")
rescue => exception
Bugsnag.notify(exception)
end
end

case ARGV[0]
when "unhandled"
UnhandledJob.enqueue
when "handled"
HandledJob.enqueue
end
8 changes: 8 additions & 0 deletions features/fixtures/que/app/enqueue-job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require_relative "setup-que"

case ARGV[0]
when "unhandled"
UnhandledJob.enqueue
when "handled"
HandledJob.enqueue
end
71 changes: 71 additions & 0 deletions features/fixtures/que/app/setup-que.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require 'pg'
require 'que'
require 'socket'
require 'bugsnag'
require 'active_record'

QUE_VERSION = ENV.fetch("QUE_VERSION")

Bugsnag.configure do |config|
puts "Configuring `api_key` to #{ENV['BUGSNAG_API_KEY']}"
config.api_key = ENV['BUGSNAG_API_KEY']
puts "Configuring `endpoint` to #{ENV['BUGSNAG_ENDPOINT']}"
config.endpoint = ENV['BUGSNAG_ENDPOINT']
end

postgres_ready = false
attempts = 0
MAX_ATTEMPTS = 10

until postgres_ready || attempts >= MAX_ATTEMPTS
begin
Timeout::timeout(5) { TCPSocket.new('postgres', 5432).close }

postgres_ready = true
rescue Exception
attempts += 1
sleep 1
end
end

raise 'postgres was not ready in time!' unless postgres_ready

ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
database: 'postgres',
username: 'postgres',
password: 'test_password',
host: 'postgres'
)

Que.connection = ActiveRecord

# Workaround a bug in que/pg
# see https://github.com/que-rb/que/issues/247
if QUE_VERSION == '0.14'
Que::Adapters::Base::CAST_PROCS[1184] = lambda do |value|
case value
when Time then value
when String then Time.parse(value)
else raise "Unexpected time class: #{value.class} (#{value.inspect})"
end
end
end

class UnhandledJob < Que::Job
def run
raise RuntimeError.new("Unhandled error")
end

def handle_error(error)
destroy
end
end

class HandledJob < Que::Job
def run
raise RuntimeError.new("Handled error")
rescue => exception
Bugsnag.notify(exception)
end
end
4 changes: 2 additions & 2 deletions features/que.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Feature: Errors are delivered to Bugsnag from Que

Scenario: Que will deliver unhandled errors
Given I start the service "que"
When I execute the command "bundle exec ruby app.rb unhandled" in the service "que"
When I execute the command "bundle exec ruby enqueue-job.rb unhandled" in the service "que"
And I wait to receive an error
Then the error is valid for the error reporting API version "4.0" for the "Ruby Bugsnag Notifier" notifier
And the event "unhandled" is true
Expand All @@ -15,7 +15,7 @@ Scenario: Que will deliver unhandled errors

Scenario: Que will deliver handled errors
Given I start the service "que"
When I execute the command "bundle exec ruby app.rb handled" in the service "que"
When I execute the command "bundle exec ruby enqueue-job.rb handled" in the service "que"
And I wait to receive an error
Then the error is valid for the error reporting API version "4.0" for the "Ruby Bugsnag Notifier" notifier
And the event "unhandled" is false
Expand Down

0 comments on commit 1341a55

Please sign in to comment.