1
0
Fork 0
mirror of https://github.com/endofunky/sidetiq.git synced 2022-11-09 13:53:30 -05:00

Allow jobs to be scheduled immediately via the web extension.

This commit is contained in:
Tobias Svensson 2013-02-06 10:51:30 +00:00
parent f90d6308f2
commit c8faeadb82
3 changed files with 45 additions and 10 deletions

View file

@ -12,12 +12,13 @@ header.row
- @schedules.each do |worker, schedule| - @schedules.each do |worker, schedule|
tr tr
td td
= worker.name a href="#{root_path}sidetiq/#{worker.name}"= worker.name
td= worker.get_sidekiq_options['queue'] td= worker.get_sidekiq_options['queue']
td td
== relative_time(schedule.next_occurrence(@time)) == relative_time(schedule.next_occurrence(@time))
td td
a href="#{root_path}sidetiq/#{worker.name}" Details form action="#{root_path}sidetiq/#{worker.name}/trigger" method="post"
input.btn.btn-danger.btn-small type="submit" name="trigger" value="Trigger" data-confirm="Are you sure you want to trigger this job?"
- else - else
.alert.alert-success No recurring jobs found. .alert.alert-success No recurring jobs found.

View file

@ -5,27 +5,45 @@ module Sidetiq
VIEWS = File.expand_path('views', File.dirname(__FILE__)) VIEWS = File.expand_path('views', File.dirname(__FILE__))
def self.registered(app) def self.registered(app)
app.helpers do
def sidetiq_clock
Sidetiq::Clock.instance
end
def sidetiq_schedules
sidetiq_clock.schedules
end
end
app.get "/sidetiq" do app.get "/sidetiq" do
clock = Sidetiq::Clock.instance @schedules = sidetiq_schedules
@schedules = clock.schedules @time = sidetiq_clock.gettime
@time = clock.gettime
slim File.read(File.join(VIEWS, 'sidetiq.slim')) slim File.read(File.join(VIEWS, 'sidetiq.slim'))
end end
app.get "/sidetiq/:name" do app.get "/sidetiq/:name" do
halt 404 unless (name = params[:name]) halt 404 unless (name = params[:name])
clock = Sidetiq::Clock.instance @time = sidetiq_clock.gettime
schedules = clock.schedules
@time = clock.gettime @worker, @schedule = sidetiq_schedules.select do |worker, schedule|
@worker, @schedule = schedules.select do |worker, schedule|
worker.name == name worker.name == name
end.flatten end.flatten
slim File.read(File.join(VIEWS, 'sidetiq_details.slim')) slim File.read(File.join(VIEWS, 'sidetiq_details.slim'))
end end
app.post "/sidetiq/:name/trigger" do
halt 404 unless (name = params[:name])
worker, _ = sidetiq_schedules.select do |worker, schedule|
worker.name == name
end.flatten
worker.perform_async
redirect "#{root_path}sidetiq"
end
end end
end end
end end

View file

@ -20,6 +20,15 @@ class TestWeb < Sidetiq::TestCase
Sidekiq::Web Sidekiq::Web
end end
def host
last_request.host
end
def setup
super
Worker.jobs.clear
end
def test_home_tab def test_home_tab
get '/' get '/'
assert_equal 200, last_response.status assert_equal 200, last_response.status
@ -54,5 +63,12 @@ class TestWeb < Sidetiq::TestCase
assert_match /#{time.getutc.to_s}/, last_response.body assert_match /#{time.getutc.to_s}/, last_response.body
end end
end end
def test_trigger
post "/sidetiq/#{Worker.name}/trigger"
assert_equal 302, last_response.status
assert_equal "http://#{host}/sidetiq", last_response.location
assert_equal 1, Worker.jobs.size
end
end end