From 3a480a7d9a6190815049bfe68f186ca0ef5704d2 Mon Sep 17 00:00:00 2001 From: avi_alima Date: Thu, 20 Aug 2015 12:54:16 +0300 Subject: [PATCH] Add ability to define minimal time between job executions to support multiple corno nodes, so two different nodes will not execute the same job Add Locking for the case that two nodes start perform job together. --- lib/crono/job.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/crono/job.rb b/lib/crono/job.rb index b1557b0..feabe9b 100644 --- a/lib/crono/job.rb +++ b/lib/crono/job.rb @@ -107,7 +107,23 @@ module Crono end def perform_before_interval? - self.last_performed_at.present? && self.last_performed_at > execution_interval.ago + return true if self.last_performed_at.present? && self.last_performed_at > execution_interval.ago + return true if model.updated_at.present? && model.created_at != model.updated_at && model.updated_at > execution_interval.ago + + Crono::CronoJob.transaction do + job_record = Crono::CronoJob.where(job_id: job_id).lock(true).first + + return true if job_record.updated_at.present? && + job_record.updated_at != job_record.created_at && + job_record.updated_at > execution_interval.ago + + job_record.touch + + return true unless job_record.save + end + + # Means that this node is permit to perform the job. + return false end end end