delayed_job icon indicating copy to clipboard operation
delayed_job copied to clipboard

Question: Why not parallelize worker start|stop|restart

Open philister opened this issue 6 years ago • 1 comments

I wonder why daemonize is not parallelized (via Thread). It would at least stop worker much faster. Thanks for your opinion.

Regards, Phil

philister avatar Oct 24 '18 07:10 philister

For what it's worth, I've been doing this via an awful hack:

#!/usr/bin/env ruby
# frozen_string_literal: true

require File.expand_path(File.join(File.dirname(__FILE__), "..", "config", "environment"))
require "delayed/command"

module DJParallelStartAndStop
  # By default, DJ stops its worker processes one at a time, which can take
  # a long time given a big pool or with busy queues.
  # Move the Daemons calls into their own threads, so we can ask all our
  # workers to start/stop in parallel.
  def daemonize
    @process_threads = []
    # run_process calls before_fork, which isn't thread safe (it non-atomically sets @files_to_reopen).
    # Call it once on the main thread here to try & avoid race conditions.
    Delayed::Worker.before_fork
    super
    @process_threads.each(&:join)
  end

  def run_process(process_name, options = {})
    @process_threads << Thread.new {
      super
    }
  end
end
Delayed::Command.prepend(DJParallelStartAndStop)

Delayed::Command.new(ARGV).daemonize

which has cut our restart times on a 8-pool worker from 45 seconds to 10.

jdelStrother avatar Feb 25 '20 17:02 jdelStrother

Closing stale

albus522 avatar Jul 26 '24 19:07 albus522