|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +=begin |
| 4 | + Copyright (c) 2010 David Andersen | davidx.org | davidx at davidx.org |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in |
| 14 | +all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +THE SOFTWARE. |
| 23 | +=end |
| 24 | + |
| 25 | + |
| 26 | +require 'rubygems' |
| 27 | +require 'carrot' |
| 28 | +require 'choice' |
| 29 | +require 'date' |
| 30 | +require 'fileutils' |
| 31 | + |
| 32 | +Choice.options do |
| 33 | + header '' |
| 34 | + header 'Specific options:' |
| 35 | + |
| 36 | + option :host, :required => true do |
| 37 | + short '-host' |
| 38 | + long '--host=HOST' |
| 39 | + desc 'The hostname or ip of the amqp server (required)' |
| 40 | + end |
| 41 | + |
| 42 | + option :queue, :required => true do |
| 43 | + short '-q' |
| 44 | + long '--queue=QUEUE' |
| 45 | + desc 'The queue' |
| 46 | + end |
| 47 | + option :logfile, :required => true do |
| 48 | + short '-f' |
| 49 | + long '--logfile=LOGFILE' |
| 50 | + desc 'The logfile to output to' |
| 51 | + end |
| 52 | + option :logdir do |
| 53 | + short '-d' |
| 54 | + long '--logdir=LOGDIR' |
| 55 | + desc 'The logdir to use' |
| 56 | + default '/var/log/logspray' |
| 57 | + end |
| 58 | + separator '' |
| 59 | + separator 'Common options: ' |
| 60 | + |
| 61 | + option :help do |
| 62 | + long '--help' |
| 63 | + desc 'Show this message' |
| 64 | + end |
| 65 | +end |
| 66 | + |
| 67 | +DATE=DateTime.now.strftime("%Y%m%d") |
| 68 | +HOST = Choice.choices[:host] |
| 69 | +QUEUE = Choice.choices[:queue] |
| 70 | +LOGDIR = Choice.choices[:logdir] |
| 71 | +LOGFILE = Choice.choices[:logfile] || "#{DEFAULT_LOGDIR}/#{QUEUE}/#{DATE}_#{QUEUE}.log" |
| 72 | + |
| 73 | +FileUtils.mkdir_p(File.dirname(LOGFILE)) |
| 74 | + |
| 75 | +client = Carrot.new(:host => HOST) |
| 76 | +q = client.queue(QUEUE) |
| 77 | + |
| 78 | +file = File.open(LOGFILE, "a") |
| 79 | +while msg = q.pop(:ack => true) |
| 80 | + file.syswrite(msg) |
| 81 | + q.ack |
| 82 | +end |
| 83 | +file.close |
| 84 | +Carrot.stop |
| 85 | + |
0 commit comments