1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activejob/test/support/integration/adapters/sidekiq.rb
yuuji.yaginuma ce98cd0d0d Explicitly require sidekiq/cli
Currently, sidekiq integration test + Ruby 2.5.0-rc1 show exception as follows.

```
#<Thread:0x000000000670bec0@/home/travis/build/rails/rails/vendor/bundle/ruby/2.5.0/gems/sidekiq-5.0.5/lib/sidekiq/util.rb:23 run> terminated with exception (report_on_exception is true):
/home/travis/build/rails/rails/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.1.7/lib/bootsnap/load_path_cache/core_ext/active_support.rb:53:in `block in load_missing_constant': uninitialized constant Sidekiq::CLI (NameError)
	from /home/travis/build/rails/rails/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.1.7/lib/bootsnap/load_path_cache/core_ext/active_support.rb:8:in `without_bootsnap_cache'
	from /home/travis/build/rails/rails/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.1.7/lib/bootsnap/load_path_cache/core_ext/active_support.rb:53:in `rescue in load_missing_constant'
	from /home/travis/build/rails/rails/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.1.7/lib/bootsnap/load_path_cache/core_ext/active_support.rb:42:in `load_missing_constant'
	from /home/travis/build/rails/rails/vendor/bundle/ruby/2.5.0/gems/sidekiq-5.0.5/lib/sidekiq/launcher.rb:65:in `heartbeat'
	from /home/travis/build/rails/rails/vendor/bundle/ruby/2.5.0/gems/sidekiq-5.0.5/lib/sidekiq/launcher.rb:123:in `start_heartbeat'
```

https://travis-ci.org/rails/rails/jobs/317187279#L2152

The reason for this is that `Sidekiq::CLI` has not been loaded.
Sidekiq integration test launches a Sidekiq instance within
another Ruby process. In such a case, need to require 'sidekiq/cli'
in that launch code.

Ref: https://github.com/mperham/sidekiq/pull/3692#issuecomment-352032251
2017-12-16 11:02:26 +09:00

98 lines
2.3 KiB
Ruby

# frozen_string_literal: true
require "sidekiq/api"
require "sidekiq/testing"
Sidekiq::Testing.disable!
module SidekiqJobsManager
def setup
ActiveJob::Base.queue_adapter = :sidekiq
unless can_run?
puts "Cannot run integration tests for sidekiq. To be able to run integration tests for sidekiq you need to install and start redis.\n"
status = ENV["CI"] ? false : true
exit status
end
end
def clear_jobs
Sidekiq::ScheduledSet.new.clear
Sidekiq::Queue.new("integration_tests").clear
end
def start_workers
continue_read, continue_write = IO.pipe
death_read, death_write = IO.pipe
@pid = fork do
continue_read.close
death_write.close
# Sidekiq is not warning-clean :(
$VERBOSE = false
$stdin.reopen(File::NULL)
$stdout.sync = true
$stderr.sync = true
logfile = Rails.root.join("log/sidekiq.log").to_s
Sidekiq::Logging.initialize_logger(logfile)
self_read, self_write = IO.pipe
trap "TERM" do
self_write.puts("TERM")
end
Thread.new do
begin
death_read.read
rescue Exception
end
self_write.puts("TERM")
end
require "sidekiq/cli"
require "sidekiq/launcher"
sidekiq = Sidekiq::Launcher.new(queues: ["integration_tests"],
environment: "test",
concurrency: 1,
timeout: 1)
Sidekiq.average_scheduled_poll_interval = 0.5
Sidekiq.options[:poll_interval_average] = 1
begin
sidekiq.run
continue_write.puts "started"
while readable_io = IO.select([self_read])
signal = readable_io.first[0].gets.strip
raise Interrupt if signal == "TERM"
end
rescue Interrupt
end
sidekiq.stop
exit!
end
continue_write.close
death_read.close
@worker_lifeline = death_write
raise "Failed to start worker" unless continue_read.gets == "started\n"
end
def stop_workers
if @pid
Process.kill "TERM", @pid
Process.wait @pid
end
end
def can_run?
begin
Sidekiq.redis(&:info)
Sidekiq.logger = nil
rescue
return false
end
true
end
end