Sha256: 7fbcaf4449bad0a0e14d073fb3d468ee0b9c264455f5720d34ffa78f3195c4e9
Contents?: true
Size: 1.38 KB
Versions: 18
Compression:
Stored size: 1.38 KB
Contents
#!/usr/bin/env ruby # vim: set ft=ruby et sw=2 ts=2: require 'betterlog' require 'excon' require 'redlock' lines = Integer(ENV.fetch('BETTERLOG_LINES', 1_000)) lock_time = Integer(ENV.fetch('BETTERLOG_LOCK_TIME', 60_000)) url = ENV.fetch('BETTERLOG_SERVER_URL') name = ENV['BETTERLOG_NAME'] redis_url = ENV.fetch('REDIS_URL') redis = if redis_sentinels = ENV['REDIS_SENTINELS']&.split(?,) redis_sentinels.map! { |s| h, p = s.split(?:, 2) { host: h, port: (p || 26379).to_i } } Redis.new(url: redis_url, sentinels: redis_sentinels, role: :master) else Redis.new(url: redis_url) end lm = Redlock::Client.new([ redis_url ]) logger = Betterlog::Logger.new(redis, name: name) quit = false [ :TERM, :INT, :QUIT ].each { |s| trap(s) { quit = true } } STDOUT.sync = true loop do count = 0 lm.lock!(File.basename($0), lock_time) do print ?… logger.each_slice(lines).with_index do |batch, i| count.zero? and print ?○ count += batch.sum(&:size) attempt(attempts: 10, sleep: -60, reraise: true) do print ?┄ Excon.post(url, body: batch.join) end end end quit and exit if count.zero? sleep 1 else print "→ %s sent.\n" % Tins::Unit.format(count, format: '%.2f %U', prefix: 1024, unit: ?b) end rescue Redlock::LockError => e STDERR.puts "Caught #{e.class}: #{e} => Retrying!" end
Version data entries
18 entries across 18 versions & 1 rubygems