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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/mini_scheduler/web.rb', line 21
def self.registered(app)
app.helpers do
def sane_time(time)
return unless time
time
end
def sane_duration(duration)
return unless duration
if duration < 1000
"#{duration}ms"
else
"#{"%.2f" % (duration / 1000.0)} secs"
end
end
end
app.get "/scheduler" do
MiniScheduler.before_sidekiq_web_request&.call
@schedules = Web.find_schedules_by_time
erb File.read(File.join(VIEWS, "scheduler.erb")), locals: { view_path: VIEWS }
end
app.get "/scheduler/history" do
MiniScheduler.before_sidekiq_web_request&.call
@schedules = Manager.discover_schedules
@schedules.sort_by!(&:to_s)
@scheduler_stats = Stat.order("started_at desc")
@filter = params[:filter]
names = @schedules.map(&:to_s)
@filter = nil if !names.include?(@filter)
@scheduler_stats = @scheduler_stats.where(name: @filter) if @filter
@scheduler_stats = @scheduler_stats.limit(200)
erb File.read(File.join(VIEWS, "history.erb")), locals: { view_path: VIEWS }
end
app.post "/scheduler/:name/trigger" do
halt 404 unless (name = params[:name])
MiniScheduler.before_sidekiq_web_request&.call
klass = name.constantize
info = klass.schedule_info
info.next_run = Time.now.to_i
info.write!
redirect "#{root_path}scheduler"
end
end
|