Module: Sidetiq::Web

Defined in:
lib/sidetiq/web.rb

Constant Summary collapse

VIEWS =
File.expand_path('views', File.dirname(__FILE__))

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object


8
9
10
11
12
13
14
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sidetiq/web.rb', line 8

def self.registered(app)
  app.get "/sidetiq" do
    @workers = Sidetiq.workers.sort_by { |worker| worker.name }
    @time = Sidetiq.clock.gettime
    erb File.read(File.join(VIEWS, 'sidetiq.erb')), locals: {view_path: VIEWS}
  end

  app.get "/sidetiq/locks" do
    begin
      @locks = Sidetiq::Lock::Redis.all.map(&:meta_data)
      @locks_available = true
    rescue Redis::CommandError
      @locks_available = false
    end

    erb File.read(File.join(VIEWS, 'locks.erb')), locals: {view_path: VIEWS}
  end

  app.get "/sidetiq/:name/schedule" do
    halt 404 unless (name = params[:name])

    @time = Sidetiq.clock.gettime

    @worker = Sidetiq.workers.detect do |worker|
      worker.name == name
    end

    @schedule = @worker.schedule

    erb File.read(File.join(VIEWS, 'schedule.erb')), locals: {view_path: VIEWS}
  end

  app.get "/sidetiq/:name/history" do
    halt 404 unless (name = params[:name])

    @time = Sidetiq.clock.gettime

    @worker = Sidetiq.workers.detect do |worker|
      worker.name == name
    end

    @history = Sidekiq.redis do |redis|
      redis.lrange("sidetiq:#{name}:history", 0, -1)
    end

    erb File.read(File.join(VIEWS, 'history.erb')), locals: {view_path: VIEWS}
  end

  app.post "/sidetiq/:name/trigger" do
    halt 404 unless (name = params[:name])

    worker = Sidetiq.workers.detect do |worker|
      worker.name == name
    end

    case worker.instance_method(:perform).arity.abs
    when 0
      worker.perform_async
    when 1
      Sidekiq.redis do |redis|
        last_run = (redis.get("sidetiq:#{worker.name}:last") || -1).to_f
        worker.perform_async(last_run)
      end
    else
      Sidekiq.redis do |redis|
        last_run = (redis.get("sidetiq:#{worker.name}:last") || -1).to_f
        worker.perform_async(last_run, Sidetiq.clock.gettime.to_i)
      end
    end

    redirect "#{root_path}sidetiq"
  end

  app.post "/sidetiq/:name/unlock" do
    halt 404 unless (name = params[:name])

    Sidetiq::Lock::Redis.new(name).unlock!

    redirect "#{root_path}sidetiq/locks"
  end
end