Sha256: e29a47351c205df09d2b2189086286cba179add2043ccb8adb57a94bd996cd35

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

require 'socket'

module ::WorkerKiller
  module Killer
    class Puma < ::WorkerKiller::Killer::Base

      attr_accessor :type, :plugin_path, :num

      def initialize(type: :phased, path: nil, num: nil, **kwargs)
        super(**kwargs)
        @type = type
        @plugin_path = path
        @num = num
      end

      def do_kill(sig, pid, alive_sec, **params)
        if @type == :phased
          do_phased_kill(sig, pid, alive_sec, **params)
        elsif @type == :plugin
          do_plugin_kill(sig, pid, alive_sec, **params)
        end
      end

      def do_phased_kill(sig, pid, alive_sec, **_params)
        cmd = 'pumactl phased-restart'

        if sig == :KILL
          logger.error "#{self} force to kill self (pid: #{pid}) alive: #{alive_sec} sec (trial #{kill_attempts})"
          Process.kill sig, pid
          return
        end

        return if @already_detached

        logger.warn "#{self} run #{cmd.inspect} (pid: #{pid}) alive: #{alive_sec} sec (trial #{kill_attempts})"
        @already_detached = true

        Thread.new(cmd) do |command|
          unless Kernel.system(command)
            logger.warn "#{self} run #{command.inspect} failed: #{$?.inspect}"
          end
        end
      end

      def do_plugin_kill(sig, pid, alive_sec, **_params)
        if sig == :KILL
          logger.error "#{self} force to kill self (pid: #{pid}) alive: #{alive_sec} sec (trial #{kill_attempts})"
          Process.kill sig, pid
          return
        end

        logger.warn "#{self} send #{num} to Puma Plugin (pid: #{pid}) alive: #{alive_sec} sec (trial #{kill_attempts})"

        Socket.unix(plugin_path) do |sock|
          sock.puts num.to_s
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
worker_killer-1.0.5.213977 lib/worker_killer/killer/puma.rb