#!/usr/bin/env ruby $:.unshift("lib") require 'rubygems' require 'eventmachine' require 'em-jack' require 'pp' $stdout.sync = true class KeyboardHandler < EM::Connection include EM::Protocols::LineText2 def post_init @jack = EMJack::Connection.new @jack.on_error { |err| puts "ERROR: #{err}" } print "> " end def receive_line(line) line.chomp! line.gsub!(/^\s+/, '') df = case(line) when /^\s*$/ then # noop nil when /^use / then tube = line.gsub(/use /, '') @jack.use(tube) { |tube| puts "Using #{tube}" } when /^watch / then tube = line.gsub(/watch /, '') @jack.watch(tube) { |tube| puts "Watching #{tube}" } when /^ignore / then tube = line.gsub(/ignore /, '') @jack.ignore(tube) { |tube| puts "Ignoring #{tube}" } when /^put / then msg = line.gsub(/put /, '') @jack.put(msg) { |id| puts "Inserted job #{id}" } when /^delete / then id = line.gsub(/delete /, '').to_i job = EMJack::Job.new(@jack, id, "asdf") job.delete { puts "Deleted" } when 'reserve' then @jack.reserve { |job| puts "Reserved #{job}" } when 'list-tubes' then @jack.list { |tubes| pp tubes } when 'list-watched' then @jack.list(:watched) { |tubes| pp tubes } when 'list-used' then @jack.list(:used) { |tube| puts "Using #{tube}" } when /^peek\s+(\d+)\s*$/ then @jack.peek($1) { |job| puts "Peeked: #{job}" } when 'peek-ready' then @jack.peek(:ready) { |job| puts "Peeked ready: #{job}"} when 'peek-delayed' then @jack.peek(:delayed) { |job| puts "Peeked delayed: #{job}" } when 'peek-buried' then @jack.peek(:buried) { |job| puts "Peeked buried: #{job}" } when 'stats' then @jack.stats { |stats| pp stats } when /^stats-tube\s+(.*)$/ then @jack.stats(:tube, $1) { |stats| pp stats } when /^stats-job\s+(\d+)/ then j = EMJack::Job.new(@jack, $1, "blah") j.stats { |stats| pp stats } when 'help' then msg = "COMMANDS:\n" msg << " put - put message onto beanstalk\n" msg << " reserve - reserve a job on beanstalk\n" msg << " delete - delete message with ID \n" msg << "\n" msg << " use - use tube for messages\n" msg << " watch - add - display tube stats\n" msg << " stats-job - display job stats\n" msg << "\n" msg << " list-tubes - display beanstalk tubes\n" msg << " list-used - display the currently used tube\n" msg << " list-watched - display the currently watched tubes\n" msg << "\n" msg << " peek - peek at job with ID \n" msg << " peek-ready - peek at the first job in the ready queue\n" msg << " peek-delayed - peek at the delayed job with shortest delay\n" msg << " peek-buried - peek at the next buried job\n" msg << "\n" msg << " help - this help text\n" msg << " quit - quit application\n" puts msg nil when 'quit' then EM.stop_event_loop nil end unless df.nil? df.callback { print "> " } df.errback { print "> " } end print "> " end end EM.run do EM.open_keyboard(KeyboardHandler) end