Sha256: 4d0b7c1fdb53e73a19b20ddf75381cf4d30327057fc3476252cefe91cd1b36bd

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

require_relative 'file_reopener'

module Delayed
  module Master
    class Forker
      def initialize(master)
        @master = master
        @config = master.config
        @callbacks = master.callbacks
      end

      def call(worker)
        around_fork(worker) do
          @callbacks.run(:before_fork, @master, worker)
          worker.pid = fork do
            @callbacks.run(:after_fork, @master, worker)
            after_fork_at_child(worker)
            worker.pid = Process.pid
            worker.instance = create_instance(worker)
            $0 = worker.process_title
            worker.instance.start
          end
        end
      end

      private

      def around_fork(worker)
        @master.logger.info { "forking #{worker.name}..." }
        yield
        @master.logger.info { "forked #{worker.name} with pid #{worker.pid}" }
      end

      def after_fork_at_child(worker)
        FileReopener.reopen
      end

      def create_instance(worker)
        require_relative 'worker/extension'

        instance = Delayed::Worker.new
        [:max_run_time, :max_attempts, :destroy_failed_jobs].each do |key|
          if (value = worker.setting.send(key))
            Delayed::Worker.send("#{key}=", value)
          end
        end
        [:min_priority, :max_priority, :sleep_delay, :read_ahead, :exit_on_complete, :queues,
         :max_threads, :max_memory].each do |key|
          if (value = worker.setting.send(key))
            instance.send("#{key}=", value)
          end
        end
        instance.master_logger = @master.logger
        instance
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
delayed_job_master-3.0.0 lib/delayed/master/forker.rb