2014-02-05 18:14:41 -05:00
|
|
|
# Based off sidetiq https://github.com/tobiassvn/sidetiq/blob/master/lib/sidetiq/web.rb
|
|
|
|
module Scheduler
|
|
|
|
module Web
|
2014-02-06 16:37:02 -05:00
|
|
|
VIEWS = File.expand_path('views', File.dirname(__FILE__)) unless defined? VIEWS
|
2014-02-05 18:14:41 -05:00
|
|
|
|
|
|
|
def self.registered(app)
|
2016-05-30 17:57:15 -04:00
|
|
|
|
|
|
|
app.helpers do
|
|
|
|
def sane_time(time)
|
|
|
|
return unless time
|
|
|
|
time
|
|
|
|
end
|
|
|
|
|
|
|
|
def sane_duration(duration)
|
|
|
|
return unless duration
|
|
|
|
if duration < 1000
|
|
|
|
"#{duration}ms"
|
2017-07-27 21:20:09 -04:00
|
|
|
elsif duration < 60 * 1000
|
|
|
|
"#{'%.2f' % (duration / 1000.0)} secs"
|
2016-05-30 17:57:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-05 18:14:41 -05:00
|
|
|
app.get "/scheduler" do
|
2014-02-06 16:37:02 -05:00
|
|
|
RailsMultisite::ConnectionManagement.with_connection("default") do
|
|
|
|
@manager = Scheduler::Manager.without_runner
|
2017-07-27 21:20:09 -04:00
|
|
|
@schedules = Scheduler::Manager.discover_schedules.sort do |a, b|
|
2014-02-09 17:21:46 -05:00
|
|
|
a_next = a.schedule_info.next_run
|
|
|
|
b_next = b.schedule_info.next_run
|
|
|
|
if a_next && b_next
|
|
|
|
a_next <=> b_next
|
|
|
|
elsif a_next
|
|
|
|
-1
|
|
|
|
else
|
|
|
|
1
|
|
|
|
end
|
2014-02-06 16:37:02 -05:00
|
|
|
end
|
2017-07-27 21:20:09 -04:00
|
|
|
erb File.read(File.join(VIEWS, 'scheduler.erb')), locals: { view_path: VIEWS }
|
2014-02-05 18:59:25 -05:00
|
|
|
end
|
2014-02-05 18:14:41 -05:00
|
|
|
end
|
|
|
|
|
2016-05-29 21:38:08 -04:00
|
|
|
app.get "/scheduler/history" do
|
|
|
|
@scheduler_stats = SchedulerStat.order('started_at desc').limit(200)
|
2017-07-27 21:20:09 -04:00
|
|
|
erb File.read(File.join(VIEWS, 'history.erb')), locals: { view_path: VIEWS }
|
2016-05-29 21:38:08 -04:00
|
|
|
end
|
|
|
|
|
2014-02-05 18:14:41 -05:00
|
|
|
app.post "/scheduler/:name/trigger" do
|
|
|
|
halt 404 unless (name = params[:name])
|
|
|
|
|
2014-02-06 16:37:02 -05:00
|
|
|
RailsMultisite::ConnectionManagement.with_connection("default") do
|
|
|
|
klass = name.constantize
|
|
|
|
info = klass.schedule_info
|
2017-10-10 23:26:50 -04:00
|
|
|
info.next_run = Time.now.to_i
|
2014-02-06 16:37:02 -05:00
|
|
|
info.write!
|
2014-02-05 18:14:41 -05:00
|
|
|
|
2014-02-06 16:37:02 -05:00
|
|
|
redirect "#{root_path}scheduler"
|
|
|
|
end
|
2014-02-05 18:14:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Sidekiq::Web.register(Scheduler::Web)
|
|
|
|
Sidekiq::Web.tabs["Scheduler"] = "scheduler"
|