Module: MiniScheduler::Web

Defined in:
lib/mini_scheduler/web.rb

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



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
# File 'lib/mini_scheduler/web.rb', line 6

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"
      elsif duration < 60 * 1000
        "#{'%.2f' % (duration / 1000.0)} secs"
      end
    end
  end

  app.get "/scheduler" do
    @schedules = Manager.discover_schedules.sort do |a, b|
      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
    end
    erb File.read(File.join(VIEWS, 'scheduler.erb')), locals: { view_path: VIEWS }
  end

  app.get "/scheduler/history" do
    @scheduler_stats = Stat.order('started_at desc').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])

    klass = name.constantize
    info = klass.schedule_info
    info.next_run = Time.now.to_i
    info.write!

    redirect "#{root_path}scheduler"
  end

end