-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec_async.rb
54 lines (44 loc) · 1.47 KB
/
exec_async.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true
module ExecAsync
extend ActiveSupport::Concern
class_methods do
def extract_args(context = self, method_name = nil, args)
arity = context.method(method_name).arity
args_size = args&.size&.to_i
unless arity.negative? || arity == args_size
raise "Invalid number of arguments, #{method_name} " \
"expected #{arity} but received #{args_size}"
end
id =
if context.is_a?(Class)
'class_method'
else
raise "#{context} does not respond to id" unless context.respond_to?(:id)
context.id
end
[name, id, method_name.to_s, args]
end
def exec_async(method_name, *args, options: { queue: :default })
ExecAsyncWorker
.set(options)
.perform_async(*extract_args(self, method_name, args))
end
def exec_async_in(seconds, method_name, *args, options: { queue: :default })
ExecAsyncWorker
.set(options)
.perform_in(seconds,
*extract_args(self, method_name, args))
end
end
def exec_async(method_name, *args, options: { queue: :default })
ExecAsyncWorker
.set(options)
.perform_async(*self.class.extract_args(self, method_name, args))
end
def exec_async_in(seconds, method_name, *args, options: { queue: :default })
ExecAsyncWorker
.set(options)
.perform_in(seconds,
*self.class.extract_args(self, method_name, args))
end
end