Skip to content

Commit

Permalink
DJ setup_lifecycle for < 4.1 (simply patch around Worker.lifecycle)
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Feb 13, 2017
1 parent 10f3542 commit ce31ed3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
23 changes: 18 additions & 5 deletions src/main/ruby/delayed/jruby_worker.rb
Expand Up @@ -12,16 +12,29 @@ class JRubyWorker < Worker
require 'delayed/sleep_calculator'
include SleepCalculator

require 'delayed/sync_lifecycle'
# @patch make sure concurrent worker threads do not cause multiple initializations
Worker.extend SyncLifecycle if Delayed.const_defined? :Lifecycle

# @override to return the same as Delayed::Worker.lifecycle (uses class instance state)
def self.lifecycle; Worker.lifecycle end

# @override since `initialize` does: `self.class.setup_lifecycle`
# @override since `initialize` (DJ 4.1) does: `self.class.setup_lifecycle`
def self.setup_lifecycle; Worker.setup_lifecycle end

unless defined? Worker.setup_lifecycle
# adapt DJ 3.0/4.0 :
# def self.lifecycle
# @lifecycle ||= Delayed::Lifecycle.new
# end
def Worker.lifecycle
@lifecycle ||= setup_lifecycle
end
def Worker.setup_lifecycle
@lifecycle = Delayed::Lifecycle.new
end
end

require 'delayed/sync_lifecycle'
# @patch make sure concurrent worker threads do not cause multiple initializations
Worker.extend SyncLifecycle if Delayed.const_defined? :Lifecycle

THREAD_LOCAL_ACCESSORS = [
:min_priority, :max_priority, :sleep_delay, :read_ahead, :queues, :exit_on_complete
]
Expand Down
1 change: 1 addition & 0 deletions src/main/ruby/delayed/sync_lifecycle.rb
Expand Up @@ -18,6 +18,7 @@ def setup_lifecycle_sync
#@lifecycle = Delayed::Lifecycle.new
#lifecycle = @lifecycle
end
@lifecycle # make sure returns lifecycle (for DJ < 4.1 compat layer)
end
end

Expand Down
28 changes: 21 additions & 7 deletions src/test/ruby/delayed/plugin_test.rb
Expand Up @@ -9,12 +9,16 @@ def self.startup
end

def self.load_plugin!
load_plugin_like!
require 'delayed_cron_job' # order is important - backend needs to be loaded
# gem: spec.add_dependency "delayed_job", ">= 4.1"
end

def self.load_plugin_like!
require 'active_record'
require 'active_record/connection_adapters/jdbcsqlite3_adapter'

require 'delayed_job_active_record' # DJ 3.0+

require 'delayed_cron_job' # order is important - backend needs to be loaded
end

setup do
Expand All @@ -23,12 +27,12 @@ def self.load_plugin!
end

test "only one lifecycle instance is created" do
self.class.load_plugin!
self.class.load_plugin_like!

lifecycle = Delayed::Lifecycle.new
Delayed::Lifecycle.expects(:new).returns(lifecycle).once
begin
Delayed::Worker.reset # @lifecycle = nil
reset_worker
threads = start_threads(3) do
l1 = Delayed::JRubyWorker.lifecycle
l2 = Delayed::Worker.lifecycle
Expand All @@ -42,12 +46,12 @@ def self.load_plugin!


test "setup lifecycle does guard for lifecycle creation" do
self.class.load_plugin!
self.class.load_plugin_like!

lifecycle = Delayed::Lifecycle.new
Delayed::Lifecycle.expects(:new).returns(lifecycle).once
begin
Delayed::Worker.reset # @lifecycle = nil
reset_worker
threads = start_threads(5) do
Delayed::JRubyWorker.new
sleep 0.1
Expand All @@ -61,13 +65,15 @@ def self.load_plugin!

context "with backend" do

@@plugin = nil

def self.startup
require 'active_record'
require 'active_record/connection_adapters/jdbcsqlite3_adapter'
load 'delayed/active_record_schema_cron.rb'
Delayed::Job.reset_column_information

load_plugin!
@@plugin = begin; load_plugin!; rescue Exception => ex; ex end
end

setup do
Expand All @@ -93,6 +99,8 @@ def perform
end

test "works (integration)" do
omit "#{@@plugin.inspect}" if @@plugin.is_a?(Exception) # 'plugin not supported on DJ < 4.1'

worker = Delayed::JRubyWorker.new({ :sleep_delay => 0.10 })
start = Time.now
Delayed::Job.enqueue job = CronJob.new(:boo), cron: '0-59/1 * * * *'
Expand Down Expand Up @@ -134,5 +142,11 @@ def start_threads(count)
threads
end

def reset_worker
# Worker.reset only does `@lifecycle = nil` on DJ 4.1
Delayed::Worker.instance_variable_set :@lifecycle, nil
Delayed::Worker.reset
end

end
end

0 comments on commit ce31ed3

Please sign in to comment.