Module: SidekiqAdhocScheduler::Web

Defined in:
lib/sidekiq_adhoc_scheduler/web.rb

Constant Summary collapse

VIEW_PATH =
File.expand_path('../../web/views', __dir__)

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



5
6
7
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/sidekiq_adhoc_scheduler/web.rb', line 5

def self.registered(app)
  app.get '/schedulers' do
    @schedulers = Sidekiq.get_all_schedules
    erb File.read(File.join(VIEW_PATH, 'scheduler.html.erb'))
  end

  app.get '/scheduler/:id' do
    @scheduler = Sidekiq.get_schedule(params[:id])
    erb File.read(File.join(VIEW_PATH, 'scheduler_edit.html.erb'))
  end

  app.put '/scheduler/:id' do
    job_name = params[:id]

    unless job_name.to_s.strip.empty?
      @scheduler = Sidekiq.get_schedule(params[:id])
      data = {
        class: params[:scheduler]['class'],
        queue: params[:scheduler]['queue'],
        description: params[:scheduler]['description'],
        args: params[:scheduler]['args']
      }

      unless params[:scheduler]['args'].to_s.strip.empty?
        args = params[:scheduler]['args'].to_s.strip
        if args.include?('[')
          begin
            data[:args] = JSON.parse(args)
          rescue JSON::ParserError => e
            redirect "#{root_path}scheduler/#{params[:id]}"
          end
        else
          data[:args] = args
        end
      end

      if !params[:scheduler]['cron'].to_s.strip.empty?
        data[:cron] = params[:scheduler]['cron']
      elsif !params[:scheduler]['every'].to_s.strip.empty?
        every = params[:scheduler]['every'].to_s.strip
        if every.include?('[')
          begin
            data[:every] = JSON.parse(every)
          rescue JSON::ParserError => e
            redirect "#{root_path}scheduler/#{params[:id]}"
          end
        else
          data[:every] = every
        end
      end

      Sidekiq.set_schedule(job_name, data)
    end

    redirect "#{root_path}schedulers"
  end

  app.get '/schedulers/new' do
    erb File.read(File.join(VIEW_PATH, 'scheduler_new.html.erb'))
  end

  app.post '/schedulers' do
    job_name = params[:scheduler]['name']

    unless job_name.to_s.strip.empty?
      data = {
        class: params[:scheduler]['class'],
        queue: params[:scheduler]['queue'],
        description: params[:scheduler]['description'],
        args: params[:scheduler]['args']
      }

      unless params[:scheduler]['args'].to_s.strip.empty?
        args = params[:scheduler]['args'].to_s.strip
        if args.include?('[')
          begin
            data[:args] = JSON.parse(args)
          rescue JSON::ParserError => e
            redirect "#{root_path}scheduler/new"
          end
        else
          data[:args] = args
        end
      end

      if !params[:scheduler]['cron'].to_s.strip.empty?
        data[:cron] = params[:scheduler]['cron']
      elsif !params[:scheduler]['every'].to_s.strip.empty?
        every = params[:scheduler]['every'].to_s.strip
        if every.include?('[')
          begin
            data[:every] = JSON.parse(every)
          rescue JSON::ParserError => e
            redirect "#{root_path}schedulers/new"
          end
        else
          data[:every] = every
        end
      end

      Sidekiq.set_schedule(job_name, data)
    end

    redirect "#{root_path}schedulers"
  end

  app.delete '/scheduler/:id' do
    schedules = Sidekiq.get_all_schedules.reject { |k, _v| k == params[:id] }
    Sidekiq.schedule = schedules
    redirect "#{root_path}schedulers"
  end

  app.get '/scheduler/:id/reject' do
    Sidekiq.remove_schedule(params[:id])
    redirect "#{root_path}schedulers"
  end

  app.get '/scheduler/:id/enqueue' do
    schedule = Sidekiq.get_schedule(params[:id])
    Sidekiq::Scheduler.enqueue_job(schedule)
    redirect "#{root_path}schedulers"
  end
end