Sha256: ec40607dd330c22cd0cc50cd0653614dff5d1279efc776fa631735a0652d57a9

Contents?: true

Size: 1.83 KB

Versions: 2

Compression:

Stored size: 1.83 KB

Contents

# rabbit_carrots.rb

require 'puma/plugin'
require 'rabbit_carrots'

Puma::Plugin.create do
  attr_reader :puma_pid, :rabbit_carrots_pid, :log_writer, :core_service

  def start(launcher)
    @log_writer = launcher.log_writer
    @puma_pid = $$

    @core_service = RabbitCarrots::Core.new(logger: log_writer)

    in_background do
      monitor_rabbit_carrots
    end

    launcher.events.on_booted do
      @rabbit_carrots_pid = fork do
        Thread.new { monitor_puma }
        start_rabbit_carrots_consumer
      end
    end

    launcher.events.on_stopped { stop_rabbit_carrots }
    launcher.events.on_restart { stop_rabbit_carrots }
  end

  private

  def start_rabbit_carrots_consumer
    core_service.start(kill_to_restart_on_standard_error: true)
  rescue StandardError => e
    Rails.logger.error "Error starting Rabbit Carrots: #{e.message}"
  end

  def stop_rabbit_carrots
    return unless rabbit_carrots_pid

    log 'Stopping Rabbit Carrots...'
    core_service.request_shutdown
    Process.kill('TERM', rabbit_carrots_pid)
    Process.wait(rabbit_carrots_pid)
  rescue Errno::ECHILD, Errno::ESRCH
  end

  def monitor_puma
    monitor(:puma_dead?, 'Detected Puma has gone away, stopping Rabbit Carrots...')
  end

  def monitor_rabbit_carrots
    monitor(:rabbit_carrots_dead?, 'Rabbits Carrot is dead, stopping Puma...')
  end

  def monitor(process_dead, message)
    loop do
      if send(process_dead)
        log message
        Process.kill('TERM', $$)
        break
      end
      sleep 2
    end
  end

  def rabbit_carrots_dead?
    Process.waitpid(rabbit_carrots_pid, Process::WNOHANG) if rabbit_carrots_started?
    false
  rescue Errno::ECHILD, Errno::ESRCH
    true
  end

  def rabbit_carrots_started?
    rabbit_carrots_pid.present?
  end

  def puma_dead?
    Process.ppid != puma_pid
  end

  def log(...)
    log_writer.log(...)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rabbit_carrots-1.0.2 lib/puma/plugin/rabbit_carrots.rb
rabbit_carrots-1.0.1 lib/puma/plugin/rabbit_carrots.rb