Sha256: 2544d9332bbf584ac0065761d3b2f5e31424fb08fa5d68e90ead27647b99b05a
Contents?: true
Size: 1.12 KB
Versions: 7
Compression:
Stored size: 1.12 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 = Redis.new(url: redis_url) 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)" % 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
7 entries across 7 versions & 1 rubygems