Skip to content

Instantly share code, notes, and snippets.

@anka
Last active Jul 15, 2021
Embed
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 }\""
end
# Custom job type
job_type :processing_rake, wrap_with_lock('processing', RAKE_COMMAND)
every 5.minute do
processing_rake 'data:process'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment