lib/logstash/inputs/irc.rb in logstash-input-irc-1.0.0 vs lib/logstash/inputs/irc.rb in logstash-input-irc-2.0.0

- old
+ new

@@ -1,10 +1,11 @@ # encoding: utf-8 require "logstash/inputs/base" require "logstash/namespace" require "thread" - +require "stud/task" +require "stud/interval" # Read events from an IRC Server. # class LogStash::Inputs::Irc < LogStash::Inputs::Base config_name "irc" @@ -50,18 +51,28 @@ # "#logstash password". # config :channels, :validate => :array, :required => true public + + def inject_bot(bot) + @bot = bot + self + end + + def bot + @bot + end + def register require "cinch" @user_stats = Hash.new @irc_queue = Queue.new @catch_all = true if @get_stats @logger.info("Connecting to irc server", :host => @host, :port => @port, :nick => @nick, :channels => @channels) - @bot = Cinch::Bot.new + @bot ||= Cinch::Bot.new @bot.loggers.clear @bot.configure do |c| c.server = @host c.port = @port c.nick = @nick @@ -75,33 +86,38 @@ if @catch_all @bot.on :catchall do |m| queue << m end else - @bot.on :channel do |m| - queue << m - end + @bot.on :channel do |m| + queue << m + end end - end # def register public def run(output_queue) @bot_thread = Stud::Task.new(@bot) do |bot| - bot.start + bot.start end if @get_stats @request_names_thread = Stud::Task.new do - loop do - sleep (@stats_interval * 60) + while !stop? + Stud.stoppable_sleep (@stats_interval * 60) do + stop? + end request_names end end end - loop do - msg = @irc_queue.pop - handle_response(msg, output_queue) + while !stop? + begin + msg = @irc_queue.pop(true) + handle_response(msg, output_queue) + rescue ThreadError + # Empty queue + end end end # def run RPL_NAMREPLY = "353" RPL_ENDOFNAMES = "366" @@ -148,10 +164,10 @@ @user_stats[channel] = 0 @bot.irc.send("NAMES #{channel}") end end - def teardown - @request_names_thread#stop! - @bot_thread#stop! + def stop + @request_names_thread.stop! if @request_names_thread + @bot_thread.stop! end end # class LogStash::Inputs::Irc