LoginSignup
3
3

More than 5 years have passed since last update.

FluentdのOutputPluginから間隔空けて行儀よくWebのAPIを叩きたい

Last updated at Posted at 2014-01-20

emitではQueueにpushするだけにして、別スレッドでQueueからpopする形をとれば、行儀よく間隔を空けながらWebのAPIを叩けると考えた次第。

他にはどんな解があるだろう。

サンプルコード

    def configure(conf)
      super
      @q = Queue.new
    end

    def start
      super
      @thread = Thread.new(&method(:post))
    end

    def shutdown
      super
      Thread.kill(@thread)
    end

    def emit(tag, es, chain)
      es.each {|time, record|
        param = OpenStruct.new
        param.tag = tag
        param.time = time
        param.record = record

        @q.push param
      }

      chain.next
    end

    private

    def post
      loop do
        param = @q.pop
        tag = param.tag
        time = param.time
        record = param.record

        #WebのAPIを叩く
        HTTParty.post(@web_api_url, :body => "#{tag},#{param},#{time},#{record}")
        sleep(1)
      end

利用例

このやり方について

Inputプラグインでこのようなやり方をよく見ます。
上記の例は固定値でsleep(1)としましたが、実際にはpost_intervalとかの名前でconfigで間隔を指定できるといいでしょう。

3
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3