Run inline jobs in separate threads

This commit is contained in:
Gannon McGibbon 2019-10-25 18:36:15 -04:00
parent 82f9d7c8b6
commit 8319f9ecea
3 changed files with 41 additions and 1 deletions

@ -12,7 +12,7 @@ module QueueAdapters
# Rails.application.config.active_job.queue_adapter = :inline
class InlineAdapter
def enqueue(job) #:nodoc:
Base.execute(job.serialize)
Thread.new { Base.execute(job.serialize) }.join
end
def enqueue_at(*) #:nodoc:

@ -4,6 +4,7 @@
require "jobs/logging_job"
require "jobs/hello_job"
require "jobs/provider_jid_job"
require "jobs/thread_job"
require "active_support/core_ext/numeric/time"
class QueuingTest < ActiveSupport::TestCase
@ -145,4 +146,21 @@ class QueuingTest < ActiveSupport::TestCase
assert job_executed "#{@id}.2"
assert job_executed_at("#{@id}.2") < job_executed_at("#{@id}.1")
end
test "inline jobs run on separate threads" do
skip unless adapter_is?(:inline)
after_job_thread = Thread.new do
ThreadJob.latch.wait
assert_nil Thread.current[:job_ran]
assert ThreadJob.thread[:job_ran]
ThreadJob.test_latch.count_down
end
ThreadJob.perform_later
after_job_thread.join
assert_nil Thread.current[:job_ran]
end
end

@ -0,0 +1,22 @@
# frozen_string_literal: true
class ThreadJob < ActiveJob::Base
class << self
attr_accessor :thread
def latch
@latch ||= Concurrent::CountDownLatch.new
end
def test_latch
@test_latch ||= Concurrent::CountDownLatch.new
end
end
def perform
Thread.current[:job_ran] = true
self.class.thread = Thread.current
self.class.latch.count_down
self.class.test_latch.wait
end
end