-
Notifications
You must be signed in to change notification settings - Fork 726
Exclusive cron task lock with flock
John Bachir edited this page Feb 1, 2020
·
3 revisions
If you need ensure to run cron task only once at time, you can use file lock with flock
Add those templates to your schedule.rb
# usage script_with_lock 'script_name', lock: 'lock_name'
job_type :script_with_lock, "cd :path && :environment_variable=:environment flock -n /var/lock/:lock.lock :bundle_command script/:task :output"
# usage runner_with_lock 'ruby code', lock: 'lock_name'
job_type :runner_with_lock, "cd :path && flock -n /var/lock/:lock.lock :bundle_command :runner_command -e :environment ':task' :output"
- Compare to the build-in templates here: https://github.com/javan/whenever/blob/master/lib/whenever/setup.rb#L27-L30
- see
man flock
for information about what-n
means. It's likely you do not want it.