Module: Sinatra::ScheduleHelper

Defined in:
lib/sinatra/helpers/schedule.rb

Constant Summary collapse

@@scheduler =
Rufus::Scheduler.new

Instance Method Summary collapse

Instance Method Details

#schedule_at(cron_expression) ⇒ Object



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
# File 'lib/sinatra/helpers/schedule.rb', line 37

def schedule_at(cron_expression) 
  @@scheduler.cron cron_expression do
    begin
      file_path = "/tmp/schedule.lock";
      f = File.open(file_path, "w+")
      # if file was previosly locked then flock throw a exception
      f.flock(File::LOCK_EX)
      ten_minutes = 600
      # if the process overcome ten minutes, the timeout api throw a exception
      Timeout::timeout(ten_minutes) do 

        begin
          yield
        rescue error
          title = error.message.split(':')[0].gsub('#<','');
          message = error.backtrace.join(',');
          NotificationSender.instance.send_error(nil,title,message)
        end

      end
    ensure
      unless f.nil?
        f.flock(File::LOCK_UN)
        f.close
      end
    end
  end
end

#schedule_every(time) ⇒ 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
# File 'lib/sinatra/helpers/schedule.rb', line 8

def schedule_every(time) 
  @@scheduler.every time do
    begin
      file_path = "/tmp/schedule.lock";
      f = File.open(file_path, "w+")
      # if file was previosly locked then flock throw a exception
      f.flock(File::LOCK_EX)
      ten_minutes = 600
      # if the process overcome ten minutes, the timeout api throw a exception
      Timeout::timeout(ten_minutes) do 

        begin
          yield
        rescue StandardError => error
          title = error.message.split(':')[0].gsub('#<','');
          message = error.backtrace.join(',');
          NotificationSender.instance.send_error(nil,title,message)
        end

      end
    ensure
      unless f.nil?
        f.flock(File::LOCK_UN)
        f.close
      end
    end
  end
end