Skip to content

Instantly share code, notes, and snippets.

@anka
Last active July 15, 2021 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anka/9755fa1808c127ab8b1b9d7c118f8c7b to your computer and use it in GitHub Desktop.
Save anka/9755fa1808c127ab8b1b9d7c118f8c7b to your computer and use it in GitHub Desktop.
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