1module Admin |
|
2 class TaskRunner |
|
3 TASKS = { |
|
|
4 "backfill_constituencies" => lambda { |params| |
5 case params[:since] |
|
6 when "week" |
|
7 since = 1.week.ago |
|
8 when "month" |
|
9 since = 1.month.ago |
|
10 when "three_months" |
|
11 since = 3.months.ago |
|
12 else |
|
13 since = nil |
|
14 end |
|
16 BackfillConstituenciesJob.perform_later(since: since.try(:iso8601)) |
|
17 }
|
|
18 }
|
|
20 attr_reader :params |
|
22 class << self |
|
23 def run(params) |
|
24 new(params).run.any?
|
|
25 rescue StandardError => e |
|
26 Appsignal.send_exception(e) |
|
27 return false |
|
28 end |
|
29 end |
|
|
31 def initialize(params) |
32 @params = params |
|
33 end |
|
|
35 def run |
36 tasks.each { |task| run_task(task) }
|
|
37 end |
|
39 private
|
|
|
41 def run_task(task) |
42 TASKS[task].call(params[task]) |
|
43 end |
|
|
45 def tasks |
46 Array(params[:tasks]).select { |t| TASKS.key?(t) } |
|
47 end |
|
48 end |
|
49end |