mirror of https://github.com/rails/rails
Merge pull request #37568 from gmcgibbon/run_inline_jobs_in_theor_own_thread
Run inline jobs in separate threads
This commit is contained in:
commit
9bfb73a5f6
|
@ -12,7 +12,7 @@ module ActiveJob
|
|||
# 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 "helper"
|
|||
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
|
Loading…
Reference in New Issue