Skip to content

Commit

Permalink
Merge pull request #37568 from gmcgibbon/run_inline_jobs_in_theor_own…
Browse files Browse the repository at this point in the history
…_thread

Run inline jobs in separate threads
  • Loading branch information
gmcgibbon committed Nov 2, 2019
2 parents 042b6fe + 8319f9e commit 9bfb73a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activejob/lib/active_job/queue_adapters/inline_adapter.rb
Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions activejob/test/integration/queuing_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
22 changes: 22 additions & 0 deletions activejob/test/jobs/thread_job.rb
@@ -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

0 comments on commit 9bfb73a

Please sign in to comment.