lib/sinatra/helpers/schedule.rb in sinatra-hexacta-1.7.16 vs lib/sinatra/helpers/schedule.rb in sinatra-hexacta-1.7.17

- old
+ new

@@ -3,64 +3,86 @@ module Sinatra module ScheduleHelper @@scheduler = Rufus::Scheduler.new - def schedule_every(time) + def check_file_locked?(specific_lock_file=nil) + return false if specific_lock_file.nil? + f = File.open("/tmp/#{specific_lock_file}", File::CREAT) + Timeout::timeout(0.001) { f.flock(File::LOCK_EX) } + f.flock(File::LOCK_UN) + false + rescue + true + ensure + unless f.nil? + f.close + end + end + + def schedule_every(time,specific_lock_file) @@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 + unless check_file_locked?(specific_lock_file) + begin + file_path = specific_lock_file.nil?? "/tmp/schedule.lock" : "/tmp/#{specific_lock_file}"; + 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 - ensure - unless f.nil? - f.flock(File::LOCK_UN) - f.close - end end + end end - def schedule_at(cron_expression) + def schedule_at(cron_expression,specific_lock_file) @@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 + unless check_file_locked?(specific_lock_file) + begin + file_path = specific_lock_file.nil?? "/tmp/schedule.lock" : "/tmp/#{specific_lock_file}"; + 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 - ensure - unless f.nil? - f.flock(File::LOCK_UN) - f.close - end end + end end end register ScheduleHelper \ No newline at end of file