#!/usr/bin/env ruby $LOAD_PATH.unshift(File.expand_path('..', __dir__)) require 'bundler/setup' require 'app/firehose_stream' $stdout.sync = true if ENV['ARLOG'] == '1' ActiveRecord::Base.logger = Logger.new(STDOUT) else ActiveRecord::Base.logger = nil end def print_help puts "Usage: #{$0} [options...]" puts "Options:" puts puts " * Showing progress: [default: show in development]" puts " -p = show progress dots for each received message" puts " -np = don't show progress dots" puts puts " * Logging status changes: [default: log in any mode]" puts " -ns = don't log status changes" puts puts " * Logging post text: [default: -lm in development, -nl in production]" puts " -lm = log text of matching posts" puts " -la = log text of every post" puts " -nl = don't log posts" puts puts " * Saving posts to db: [default: -da in development, -dm in production]" puts " -da = save all posts to database" puts " -dm = save only matching posts to database" puts " -nd = don't save any posts" puts puts " * Replaying missed events: [default: -nr in development, -r in production]" puts " -r = pass a cursor param when connecting to replay any missed events" puts " -nr = don't replay missed events" puts " -r12345 = start from this specific cursor" end firehose = FirehoseStream.new(ENV['FIREHOSE']) ARGV.each do |arg| case arg when '-p' firehose.show_progress = true when '-np' firehose.show_progress = false when '-ns' firehose.log_status = false when '-lm' firehose.log_posts = :matching when '-la' firehose.log_posts = :all when '-nl' firehose.log_posts = false when '-dm' firehose.save_posts = :matching when '-da' firehose.save_posts = :all when '-nd' firehose.save_posts = false when '-r' firehose.replay_events = true when /^\-r(\d+)$/ firehose.replay_events = true firehose.start_cursor = $1.to_i when '-nr' firehose.replay_events = false when '-h', '--help' print_help exit 0 else puts "Unrecognized option: #{arg}" print_help exit 1 end end trap("SIGINT") { puts firehose.log "Stopping..." EM.add_timer(0) { firehose.stop } } trap("SIGTERM") { firehose.log "Shutting down the service..." EM.add_timer(0) { firehose.stop } } firehose.start