Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What is going on with this article?
@bash0C7

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

More than 5 years have passed since last update.

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
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
3
Help us understand the problem. What is going on with this article?