Skip to content

Commit abf1b9c

Browse files
committed
Job.perform_inline method to execute jobs inline
1 parent d0a9f05 commit abf1b9c

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

lib/active_interaction/extras/active_job.rb

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ module ActiveInteraction::Extras::ActiveJob
88
def configured_job_class
99
ConfiguredJob
1010
end
11+
12+
def perform_inline(&block)
13+
job_class.perform_inline(&block)
14+
end
1115
end
1216

1317
module Perform
@@ -22,15 +26,37 @@ def perform(*args)
2226
end
2327
end
2428
end
29+
30+
class_methods do
31+
def perform_inline
32+
Thread.current[name] ||= []
33+
Thread.current[name].push true
34+
yield
35+
ensure
36+
if Thread.current[name].size == 1
37+
Thread.current[name] = nil
38+
else
39+
Thread.current[name].pop
40+
end
41+
end
42+
43+
def perform_inline?
44+
Thread.current[name]
45+
end
46+
end
2547
end
2648

2749
class ConfiguredJob < ::ActiveJob::ConfiguredJob
2850
def run(*args)
29-
perform_later(*args)
51+
run!(*args)
3052
end
3153

3254
def run!(*args)
33-
perform_later(*args)
55+
if @job_class.perform_inline?
56+
perform_now(*args)
57+
else
58+
perform_later(*args)
59+
end
3460
end
3561
end
3662
end

0 commit comments

Comments
 (0)