ruby 打印美洲狮工人身份

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 打印美洲狮工人身份相关的知识,希望对你有一定的参考价值。

require 'puma'
require 'puma/control_cli'
require 'json'

class PumaWorkersPrinter
  attr_reader :format

  def initialize(opts={})
    @format = opts.fetch(:format){ :plain }
    @password = opts.fetch(:password){ '' }
    @control_url = opts.fetch(:control_url){ 'tcp://127.0.0.1:9293' }
  end

  def metadata
    {
      source: "puma"
    }
  end

  # fetch worker info from puma control
  # server and print thread utilization
  # info for each fork to STDOUT
  def run
    @workers = nil

    workers.each do |worker|
      puts formatter.new(metadata.merge(worker)).inspect
    end
  end

  def workers
    @workers ||= build_workers
  end

  protected

  def formatter
    case format.to_sym
    when :json
      JsonFormatter
    else
      SimpleFormatter
    end
  end

  def build_workers
    buffer = StringIO.new
    command = Puma::ControlCLI.new(
      [
        '-C', control_url,
        '-T', password,
        'stats'
      ],
      buffer,
      buffer)
    command.run

    JSON.parse(buffer.string.split("\n").last)["worker_status"]
  end

  def password
    @password
  end

  def control_url
    @control_url
  end

  class Formatter
    attr_accessor :hash
    # {
    #   "pid"=>2249,
    #   "index"=>0,
    #   "phase"=>0,
    #   "booted"=>true,
    #   "last_checkin"=>"2017-05-09T19:19:44Z",
    #   "last_status"=>{
    #                    "backlog"=>0,
    #                    "running"=>1
    #                  }
    # }
    def initialize(hash)
      @hash = hash.dup
    end
  end

  class SimpleFormatter < Formatter
    def inspect
      string =  "PID=#{hash['pid']} FORK=#{hash['index']} (\n"
      string += "  phase         #{hash['phase']}\n"
      string += "  booted        #{hash['booted']}\n"
      string += "  last_updated  #{ago(hash['last_checkin'])} ms ago\n"
      string += "  running       #{hash['last_status']['running']}\n"
      string += "  backlog       #{hash['last_status']['backlog']}\n"
    end

    def ago(iso_time)
      ((Time.now - Time.parse(iso_time)) * 1000).round(0)
    end
  end

  class JsonFormatter < Formatter
    def inspect
      flattened.to_json
    end

    def flattened
      hash.merge!(hash.delete('last_status'))
      hash.merge!({'fork' => hash.delete('index')})
      hash
    end
  end
end

opts = {format: :json, password: 'foo'}
PumaWorkersPrinter.new(opts).run

以上是关于ruby 打印美洲狮工人身份的主要内容,如果未能解决你的问题,请参考以下文章

ruby Whatsapp状态爬虫为中美洲移动电话号码。

ruby 清除卡住的resque工人

ruby 多个工人

ruby 工人采取一组记录并重新处理所有附件

基于技能公平分配任务给工人的算法

关于工人的界定,急!!