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
55
56
|
# File 'lib/canvas_sync/misc_helper.rb', line 15
def self.invoke_task(job)
job = job.symbolize_keys
job_args = job[:args] || job[:parameters] || []
job_kwargs = job[:kwargs] || {}
if (mthd = job[:method]) && !(job[:class] || job[:instance_of] || job[:model])
if mthd.include?('#')
clazz, method = clazz.split("#")
job[:instance_of] = clazz
job[:method] = method
elsif mthd.include?('.')
clazz, method = mthd.split(".")
job[:class] = clazz
job[:method] = method
end
end
if job[:model]
model_class = load_constant(job[:model])
find_by = job[:find_by]
target = find_by.is_a?(Hash) ? model_class.find_by(find_by) : model_class.find_by(id: find_by)
target.send(job[:method], *job_args, **job_kwargs)
elsif job[:class]
target = load_constant(job[:class])
target.send(job[:method], *job_args, **job_kwargs)
elsif job[:instance_of]
target = load_constant(job[:instance_of]).new
target.send(job[:method], *job_args, **job_kwargs)
elsif job[:job]
job_class = load_constant(job[:job])
job_class = job_class.set(job[:options]) if job[:options].present?
if job_class < ActiveJob::Base
job_class.perform_later(*job_args, **job_kwargs)
else
job_args << job_kwargs.symbolize_keys if job_kwargs
job_class.perform_async(*job_args)
end
end
end
|