2016-05-19 14 views
0

私はpumaサーバで動作するレールアプリケーションを持っています。何らかの方法がありますか?現在、アプリケーションで使用されているスレッドの数はいくつですか?レールで使用するスレッドの数puma

+0

[Puma、アクティブスレッドの数を知る方法](https://stackoverflow.com/questions/43862767/puma-how-to-know-the-number-of-active-threads)の可能な複製 – kvantour

答えて

0

私はずっと前と同じ事について疑問に思っていましたが、これはissueに来ました。著者は、彼らがそれらの統計情報を収集するために使用して終了コードが含まれていました。

module PumaThreadLogger 

    def initialize *args 
    ret = super *args 
    Thread.new do 
     while true 

     # Every X seconds, write out what the state of this dyno is in a format that Librato understands. 
     sleep 5 

     thread_count = 0 
     backlog = 0 
     waiting = 0 

     # I don't do the logging or string stuff inside of the mutex. I want to get out of there as fast as possible 
     @mutex.synchronize { 
      thread_count = @workers.size 
      backlog = @todo.size 
      waiting = @waiting 
     } 

     # For some reason, even a single Puma server (not clustered) has two booted ThreadPools. 
     # One of them is empty, and the other is actually doing work 
     # The check above ignores the empty one 
     if (thread_count > 0) 

      # It might be cool if we knew the Puma worker index for this worker, but that didn't look easy to me. 
      # The good news: By using the PID we can differentiate two different workers on two different dynos with the same name 
      # (which might happen if one is shutting down and the other is starting) 
      source_name = "#{Process.pid}" 

      # If we have a dyno name, prepend it to the source to make it easier to group in the log output 
      dyno_name = ENV['DYNO'] 
      if (dyno_name) 
      source_name="#{dyno_name}."+source_name 
      end 
      msg = "source=#{source_name} " 

      msg += "sample#puma.backlog=#{backlog} sample#puma.active_connections=#{thread_count - waiting} sample#puma.total_threads=#{thread_count}" 
      Rails.logger.info msg 
     end 
     end 
    end 
    ret 
    end 
end 

module Puma 
    class ThreadPool 
    prepend PumaThreadLogger 
    end 
end 

このコードは、Herokuのに固有のロジックが含まれていますが、@workers.sizeを収集し、それがどのような環境で動作するログインのコア。