Last active
July 15, 2021 07:42
-
-
Save anka/9755fa1808c127ab8b1b9d7c118f8c7b to your computer and use it in GitHub Desktop.
Example schedule.rb to use flock as a background job mutex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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