Skip to content

Instantly share code, notes, and snippets.

Last active July 15, 2021 07:42
What would you like to do?
Example schedule.rb to use flock as a background job mutex
# frozen_string_literal: true
require 'digest'
# The command pattern to use. Note that placeholders like :path
# :task and :output will be replaced by whenever.
RAKE_COMMAND = 'cd :path && bundle exec rails :task --silent :output'
# Helper method for uniq file locks
def wrap_with_lock(prefix, command)
lockfile_name = Digest::SHA256.hexdigest(prefix + command)
"/usr/bin/flock -n :path/tmp/#{lockfile_name}.lock -c \"#{ command }\""
# Custom job type
job_type :processing_rake, wrap_with_lock('processing', RAKE_COMMAND)
every 5.minute do
processing_rake 'data:process'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment