Class: Fluent::GreeCommunityInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_gree_community.rb

Instance Method Summary collapse

Instance Method Details

#configure(config) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fluent/plugin/in_gree_community.rb', line 28

def configure(config)
  super
  @thread_title_pattern = Regexp.new(@thread_title_pattern, {}, 'n')

   = Pit.get(@pit_id, require: {
    'email' => 'mail',
    'password' => 'password',
  })
  @fetcher = GREE::Community::Fetcher.new(
    ['email'],
    ['password']
  )

  @community = GREE::Community.new(@community_id)

  # {[community_id, thread_id] => last_comment_id}
  @last_comment_ids = {}

  $log.info("gree_community: user=#{['email']}")
  $log.info("gree_community: community_id=#{@community_id}")
  $log.info("gree_community: thread_title_pattern=#{@thread_title_pattern}")
  $log.info("gree_community: interval_sec=#{@interval_sec}")
end

#fetch_and_emitObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fluent/plugin/in_gree_community.rb', line 74

def fetch_and_emit
  @community.fetch(@fetcher)
  @community.recent_threads[0...@recent_threads_num].select{|th| th.title =~ @thread_title_pattern}.each do|th|
    th.fetch(@fetcher)
    th.recent_comments.each do|comment|
      last_comment_id = @last_comment_ids[[@community.id, th.id]]
      next if last_comment_id && comment.id <= last_comment_id
      @last_comment_ids[[@community.id, th.id]] = comment.id

      next if @silent_startup && @first_time

      Fluent::Engine.emit(@tag, Fluent::Engine.now, {
        'community' => {
          'id' => @community.id,
        },
        'thread' => {
          'id' => th.id,
          'title' => th.title,
        },
        'comment' => {
          'id' => comment.id,
          'user_name' => comment.user_name,
          'user_id' => comment.user_id,
          'body_text' => comment.body_text.strip,
        }
      })
    end
  end
end

#runObject



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fluent/plugin/in_gree_community.rb', line 60

def run
  @first_time = true
  loop do
    begin
      fetch_and_emit
    rescue StandardError, Timeout::Error
      $log.error("gree_community(community_id=#{@community_id}): #{$!.inspect}")
      $log.error_backtrace
    end
    @first_time = false
    sleep @interval_sec
  end
end

#shutdownObject



56
57
58
# File 'lib/fluent/plugin/in_gree_community.rb', line 56

def shutdown
  @thread.kill
end

#startObject



52
53
54
# File 'lib/fluent/plugin/in_gree_community.rb', line 52

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