Class: Wrapbox::LogFetcher::Papertrail
- Inherits:
-
Object
- Object
- Wrapbox::LogFetcher::Papertrail
- Defined in:
- lib/wrapbox/log_fetcher/papertrail.rb
Constant Summary collapse
- STOP_WAIT_TIMELIMIT =
10
Instance Method Summary collapse
-
#initialize(query: nil, delay: 2, **options) ⇒ Papertrail
constructor
A new instance of Papertrail.
- #main_loop ⇒ Object
- #run(task:) ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(query: nil, delay: 2, **options) ⇒ Papertrail
Returns a new instance of Papertrail.
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/wrapbox/log_fetcher/papertrail.rb', line 6 def initialize(query: nil, delay: 2, **) begin require 'papertrail/cli' rescue LoadError $stderr.puts "Require papertrail gem" exit 1 end # see. https://github.com/papertrail/papertrail-cli/blob/master/lib/papertrail/cli.rb @query = query @delay = delay @options = .reject { |_, v| v.nil? } end |
Instance Method Details
#main_loop ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/wrapbox/log_fetcher/papertrail.rb', line 30 def main_loop papertrail = ::Papertrail::Cli.new = papertrail..merge(@options).merge(follow: true) connection = ::Papertrail::Connection.new() = {} if @options[:system] [:system_id] = connection.find_id_for_source(@options[:system]) unless [:system_id] $stderr.puts "System \"#{@options[:system]}\" not found" end end if @options[:group] [:group_id] = connection.find_id_for_group(@options[:group]) unless [:group_id] $stderr.puts "Group \"#{@options[:group]}\" not found" end end if @options[:search] search = connection.find_search(@options[:search], @query_options[:group_id]) unless search $stderr.puts "Search \"#{@options[:search]}\" not found" end [:group_id] ||= search['group_id'] @query = search['query'] end @query ||= '' search_query = ::Papertrail::SearchQuery.new(connection, @query, ) until @stop do search_query.next_results_page.events.each do |event| next if event.received_at < @started_at papertrail.display_result(event) end sleep @delay end end |
#run(task:) ⇒ Object
20 21 22 23 |
# File 'lib/wrapbox/log_fetcher/papertrail.rb', line 20 def run(task:) @started_at = Time.now @loop_thread = Thread.start(&method(:main_loop)) end |
#stop ⇒ Object
25 26 27 28 |
# File 'lib/wrapbox/log_fetcher/papertrail.rb', line 25 def stop @stop = true @loop_thread&.join(STOP_WAIT_TIMELIMIT) end |