Sha256: 566c8523c48525b2330a25a43419316e37eb92197f946540ee9084217ef9e6b9

Contents?: true

Size: 1.82 KB

Versions: 103

Compression:

Stored size: 1.82 KB

Contents

require 'timeout'
# encoding: utf-8
module Sinatra
  module ScheduleHelper

    @@scheduler = Rufus::Scheduler.new

    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

    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
  end

  register ScheduleHelper
end

Version data entries

103 entries across 103 versions & 1 rubygems

Version Path
sinatra-hexacta-1.7.16 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.7.15 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.7.14 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.7.13 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.7.12 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.7.11 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.7.10 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.7.9 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.7.8 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.7.7 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.7.6 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.7.5 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.7.4 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.7.3 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.7.2 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.7.0 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.6.4 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.6.3 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.6.2 lib/sinatra/helpers/schedule.rb
sinatra-hexacta-1.6.1 lib/sinatra/helpers/schedule.rb