|
| 1 | +require "rubygems" |
| 2 | +require "bundler/setup" |
| 3 | +require "sinatra" |
| 4 | +require 'time_difference' |
| 5 | +require File.join(File.dirname(__FILE__), "environment") |
| 6 | + |
| 7 | +configure do |
| 8 | + set :views, "#{File.dirname(__FILE__)}/views" |
| 9 | + set :show_exceptions, :after_handler |
| 10 | +end |
| 11 | + |
| 12 | +configure :production, :development do |
| 13 | + enable :logging |
| 14 | +end |
| 15 | + |
| 16 | +def _log msg |
| 17 | + f = File.open("/tmp/sinatra.log", "a") |
| 18 | + f.puts msg |
| 19 | + f.close |
| 20 | +end |
| 21 | + |
| 22 | +def populate_cron_runs(action,cron_name) |
| 23 | + if action == 'start' |
| 24 | + CronRun.create(:cron_id => Cron.first(:name => cron_name).id,:start_time => DateTime.now, :alert => 0) |
| 25 | + elsif action == 'complete' |
| 26 | + cr = CronRun.last(:cron_id => Cron.first(:name => cron_name).id) |
| 27 | + cr.update(:end_time => DateTime.now) |
| 28 | + end |
| 29 | +end |
| 30 | + |
| 31 | +def alert cron_id, error_type, time, freq |
| 32 | + if error_type == 1 |
| 33 | + _log "Cron #{Cron.first(:id => cron_id).name} started at #{time.strftime("%d-%m-%Y - %H:%M:%S")} but did not complete in #{freq} minutes" |
| 34 | + elsif error_type == 2 |
| 35 | + _log "Cron #{Cron.first(:id => cron_id).name} did not start. It was supposed to run every #{freq} minutes. Last started at #{time.strftime("%d-%m-%Y - %H:%M:%S")}" |
| 36 | + end |
| 37 | +end |
| 38 | + |
| 39 | +Thread.new do |
| 40 | + while true do |
| 41 | + _log "#{Time.now} --- running thread" |
| 42 | + Cron.each do |cron| |
| 43 | + begin |
| 44 | + CronRun.all(:cron_id => cron.id).each do |cr| |
| 45 | + if ( cr.end_time == nil && cr.alert == 0 ) |
| 46 | + cron_exec_time = Cron.first(:id => cr.cron_id).exec_time |
| 47 | + if TimeDifference.between(cr.start_time,DateTime.now).in_minutes.to_i > cron_exec_time |
| 48 | + cr.update(:alert => 1) |
| 49 | + alert(cr.cron_id, 1, cr.start_time, cron_exec_time) |
| 50 | + end |
| 51 | + end |
| 52 | + end |
| 53 | + alert(cron.id, 2, CronRun.last(:cron_id => cron.id).start_time, cron.ping_freq) if TimeDifference.between(CronRun.last(:cron_id => cron.id).start_time, DateTime.now).in_minutes.to_i > cron.ping_freq |
| 54 | + rescue Exception => e |
| 55 | + _log e.message |
| 56 | + _log e.backtrace |
| 57 | + ensure |
| 58 | + next |
| 59 | + end |
| 60 | + end |
| 61 | + sleep 60 |
| 62 | + end |
| 63 | +end |
| 64 | + |
| 65 | +get "/" do |
| 66 | + @cron_hash = {} |
| 67 | + Cron.each do |cron| |
| 68 | + cr = CronRun.last(:cron_id => cron.id) |
| 69 | + @cron_hash[cron.name] = {:start_time => cr.start_time, :end_time => cr.end_time, :ping_freq => cron.ping_freq, :exec_freq => cron.exec_time} |
| 70 | + end |
| 71 | + erb :root |
| 72 | +end |
| 73 | + |
| 74 | +get "/cron" do |
| 75 | + @cron = Cron.first(:name => params[:name]) |
| 76 | + cron_run = CronRun.all(:cron_id => @cron.id, :order => [ :id.desc ], :limit => 20) |
| 77 | + @cron_hash = { @cron.name => [] } |
| 78 | + cron_run.each do |cr| |
| 79 | + @cron_hash[@cron.name] << {:start_time => cr.start_time, :end_time => cr.end_time, :ping_freq => @cron.ping_freq, :exec_freq => @cron.exec_time} |
| 80 | + end |
| 81 | + erb :cron |
| 82 | +end |
| 83 | + |
| 84 | +post "/ping" do |
| 85 | + begin |
| 86 | + if Cron.first(:name => params[:name]) |
| 87 | + populate_cron_runs params[:action], params[:name] |
| 88 | + else |
| 89 | + Cron.create(:name => params[:name], :exec_time => params[:et], :ping_freq => params[:pf]) |
| 90 | + populate_cron_runs params[:action], params[:name] |
| 91 | + end |
| 92 | + rescue Exception => e |
| 93 | + puts e.message |
| 94 | + puts e.backtrace |
| 95 | + end |
| 96 | +end |
0 commit comments