Sha256: ac0d254d65d8f5f8c16a32dda33d870307bc8bafa33f89726a7ef3ab400897c7

Contents?: true

Size: 1.65 KB

Versions: 4

Compression:

Stored size: 1.65 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)
            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)
        $0 = worker.process_title
        Thread.current.name = 'delayed_job'
        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

4 entries across 4 versions & 1 rubygems

Version Path
delayed_job_master-3.1.2 lib/delayed/master/forker.rb
delayed_job_master-3.1.1 lib/delayed/master/forker.rb
delayed_job_master-3.1.0 lib/delayed/master/forker.rb
delayed_job_master-3.0.1 lib/delayed/master/forker.rb