Skip to content

Commit

Permalink
removes stopped pid files
Browse files Browse the repository at this point in the history
  • Loading branch information
JackBlackLight committed Jul 23, 2024
1 parent 69eb1ee commit f0ce7be
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/tasks/resque.rake
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ task 'resque:setup' => :environment
task 'resque:work' => :environment

MAX_WAIT_TIME_TO_KILL_WORKERS = 120
PIDSFILE_PATH = 'tmp/pids'
PIDFILE_PATH = 'tmp/pids'

namespace :resque do
desc 'Stop current workers and start new workers'
Expand All @@ -28,17 +28,27 @@ namespace :resque do
def store_pids(pids)
pids_to_store = pids
pids_to_store.each_with_index do |pid_to_store, index|
pid_storage_file = "#{PIDSFILE_PATH}/resque_work_#{index + 1}.pid"
pid_storage_file = "#{PIDFILE_PATH}/resque_work_#{index + 1}.pid"
File.write(File.expand_path(pid_storage_file, Rails.root), "#{pid_to_store}\n")
end
end

def clear_pid_files
pid_files = Dir.glob(File.join(PIDFILE_PATH, 'resque_work*.pid')).select { |path|
File.file?(path) && !File.zero?(path)
}
pid_files.each do |pidfile|
File.delete(pidfile)
end
end

def read_pids
pids_files = Dir.glob(File.join(PIDSFILE_PATH, 'resque_work*.pid')).select { |path|
pid_files = Dir.glob(File.join(PIDFILE_PATH, 'resque_work*.pid')).select { |path|
File.file?(path) && !File.zero?(path)
}
pids_files.map do |file_path|
File.open(file_path, &:gets).chomp!

pid_files.map do |file_path|
File.open(file_path, &:gets).chomp
end
end

Expand Down Expand Up @@ -71,7 +81,7 @@ namespace :resque do
syscmd = "kill -s QUIT #{pids.join(' ')}"
puts "$ #{syscmd}"
`#{syscmd}`
store_pids([])
clear_pid_files
puts "\n"
puts 'Workers have been shut down.'
end
Expand All @@ -93,16 +103,16 @@ namespace :resque do

ops = {
pgroup: true,
err: [Rails.root.join('log', 'resque_stderr').to_s, 'a'],
out: [Rails.root.join('log', 'resque_stdout').to_s, 'a']
err: [Rails.root.join('log', 'resque.log').to_s, 'a'],
out: [Rails.root.join('log', 'resque.log').to_s, 'a']
}

pids = []
worker_config.each do |queues, count|
env_vars = {
'QUEUES' => queues.to_s,
'RAILS_ENV' => Rails.env.to_s,
'INTERVAL' => polling_interval.to_s # jobs tend to run for a while, so a 5-second checking interval is fine
'INTERVAL' => polling_interval.to_s # jobs tend to run for a while, so a 5-second checking interval (the default) is fine
}
count.times do
# Using Kernel.spawn and Process.detach because regular system() call would
Expand Down

0 comments on commit f0ce7be

Please sign in to comment.