Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run inline jobs in separate threads #37568

Merged

Conversation

@gmcgibbon
Copy link
Member

gmcgibbon commented Oct 25, 2019

Summary

Closes #37526.

Runs inline jobs in separate threads so that thread locals (eg. current attributes) are properly scoped and reset when running jobs inline.

@rails-bot rails-bot bot added the activejob label Oct 25, 2019
@kaspth
kaspth approved these changes Oct 26, 2019

assert_nil Thread.current[:job_ran]
ThreadJob.perform_later
assert_nil Thread.current[:job_ran]

This comment has been minimized.

Copy link
@eileencodes

eileencodes Oct 28, 2019

Member

I think this test needs 2 threads to test that threading is working correctly. Otherwise you just have one thread and it will always pass regardless of the Thread.new block

This comment has been minimized.

Copy link
@gmcgibbon

gmcgibbon Oct 28, 2019

Author Member

I agree this test could be better. I tried inspecting the thread spun up by the inline adapter, but Thread.list doesn't show it because it dies immediately after joining. How can I use two threads to improve this test?

This comment has been minimized.

Copy link
@Edouard-chin

Edouard-chin Oct 30, 2019

Contributor

That should do it

diff --git a/activejob/test/integration/queuing_test.rb b/activejob/test/integration/queuing_test.rb
index 7c92496331..a7d3865c42 100644
--- a/activejob/test/integration/queuing_test.rb
+++ b/activejob/test/integration/queuing_test.rb
@@ -150,8 +150,15 @@ class QueuingTest < ActiveSupport::TestCase
   test "inline jobs run on separate threads" do
     skip unless adapter_is?(:inline)

-    assert_nil Thread.current[:job_ran]
+    t1 = Thread.new do
+      ThreadJob::LatchManager.latch1.wait
+      assert_nil Thread.current[:job_ran]
+      assert(ThreadJob::LatchManager.job_thread[:job_ran])
+      ThreadJob::LatchManager.latch2.count_down
+    end
+
     ThreadJob.perform_later
+    t1.join
     assert_nil Thread.current[:job_ran]
   end
 end
diff --git a/activejob/test/jobs/thread_job.rb b/activejob/test/jobs/thread_job.rb
index 804d18b4a9..27218760a0 100644
--- a/activejob/test/jobs/thread_job.rb
+++ b/activejob/test/jobs/thread_job.rb
@@ -1,7 +1,24 @@
 # frozen_string_literal: true

 class ThreadJob < ActiveJob::Base
+  class LatchManager
+    class << self
+      attr_accessor :job_thread
+
+      def latch1
+        @@latch1 ||= Concurrent::CountDownLatch.new
+      end
+
+      def latch2
+        @@latch2 ||= Concurrent::CountDownLatch.new
+      end
+    end
+  end
+
   def perform
     Thread.current[:job_ran] = true
+    LatchManager.job_thread = Thread.current
+    LatchManager.latch1.count_down
+    LatchManager.latch2.wait
   end
 end
@gmcgibbon gmcgibbon force-pushed the gmcgibbon:run_inline_jobs_in_theor_own_thread branch 3 times, most recently from baa21e2 to b0adf6d Oct 31, 2019
@gmcgibbon gmcgibbon force-pushed the gmcgibbon:run_inline_jobs_in_theor_own_thread branch from b0adf6d to 8319f9e Nov 2, 2019
@gmcgibbon gmcgibbon merged commit 9bfb73a into rails:master Nov 2, 2019
2 checks passed
2 checks passed
build
Details
buildkite/rails Build #64691 passed (15 minutes, 40 seconds)
Details
@gmcgibbon gmcgibbon deleted the gmcgibbon:run_inline_jobs_in_theor_own_thread branch Nov 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.