Skip to content
This repository has been archived by the owner on Oct 17, 2020. It is now read-only.

Commit

Permalink
* Added: Ping command:
Browse files Browse the repository at this point in the history
  $ pipe ping
  0.5.1
* Added: List command, returns list of all available commands.
  • Loading branch information
assaf committed Mar 1, 2010
1 parent 0cf6420 commit bd7644b
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
@@ -1,3 +1,9 @@
0.5.1 (2010-03-01)
* Added: Ping command:
$ pipe ping
0.5.1
* Added: List command, returns list of all available commands.

0.5.0 (2010-03-01)
This release adds background processes. The Pipefile can specify multiple
background activities. Pipemaster starts each activity by forking a new child
Expand Down
44 changes: 44 additions & 0 deletions README.rdoc
Expand Up @@ -46,6 +46,50 @@ Step 3: For a new shell, execute a command:
Stand upside down!


== Pipemaster, Resque and Rails

This example uses Resque to queue and process tasks asynchronously, where the
tasks are part of a larger Rails application (e.g. using ActiveRecord models,
ActiveMailer).

This Pipefile loads the Rails application once during setup. It starts one
Resque worker than polls for new jobs every 5 seconds.

#!/usr/bin/env ruby -S pipemaster
user "nobody"

require "syslog_logger"
syslog = SyslogLogger.new("pipemaster")
class << syslog ; def close ; end ; end
logger syslog

setup do
# Load RAILS. Diz will take a while.
require File.dirname(__FILE__) + '/config/environment'
ActiveRecord::Base.connection.disconnect!
end
after_fork do |server, worker|
ActiveRecord::Base.establish_connection
end

# Resque
background :resque do
resque = Resque::Worker.new("*")
resque.verbose = true
trap(:QUIT) { resque.shutdown } # graceful
resque.work(5)
end


== Tips && tricks

Add this at the top of your Pipefile for Ruby syntax highlighting:


#!ruby -S pipemaster



== License

Pipemaster is copyright of Assaf Arkin. It is heavily based on the awesome
Expand Down
7 changes: 6 additions & 1 deletion bin/pipe
Expand Up @@ -9,7 +9,12 @@ retcode = nil
tty = false

opts = OptionParser.new("", 24, ' ') do |opts|
opts.banner = "Usage: #{File.basename($0)} [options] command [args]\n"
opts.banner = "Usage: #{File.basename($0)} [options] command [args]\n" \
"\nCommon commands:\n" \
" list List all commands available on server (one name per line)\n" \
" ping Pings server (returns version number)\n"

opts.separator "\nOptions:"

opts.on("-t", "--tty", "read input from terminal (default: false)") do |t|
tty = t ? true : false
Expand Down
2 changes: 1 addition & 1 deletion lib/pipemaster/configurator.rb
Expand Up @@ -22,7 +22,7 @@ class Configurator < Struct.new(:set, :config_file)
},
:pid => nil,
:background => {},
:commands => {}
:commands => { }
}

def initialize(defaults = {}) #:nodoc:
Expand Down
14 changes: 12 additions & 2 deletions lib/pipemaster/server.rb
Expand Up @@ -387,6 +387,11 @@ def reexec
proc_name 'master (old)'
end

DEFAULT_COMMANDS = {
:list => lambda { $stdout << (DEFAULT_COMMANDS.keys | commands.keys).sort.join("\n") },
:ping => lambda { $stdout << VERSION }
}

def process_request(socket, worker)
trap(:QUIT) { exit }
[:TERM, :INT].each { |sig| trap(sig) { exit! } }
Expand All @@ -406,8 +411,13 @@ def process_request(socket, worker)
logger.info "#{Process.pid} #{name} #{args.join(' ')}"

ARGV.replace args
command = commands[name.to_sym] or raise ArgumentError, "No command #{name}"
command.call *args
if command = commands[name.to_sym]
command.call *args
elsif command = DEFAULT_COMMANDS[name.to_sym]
instance_eval &command
else
raise ArgumentError, "No command #{name}"
end
logger.info "#{Process.pid} exit"
socket.write 0.chr
rescue SystemExit => ex
Expand Down
2 changes: 1 addition & 1 deletion pipemaster.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = "pipemaster"
spec.version = "0.5.0"
spec.version = "0.5.1"
spec.author = "Assaf Arkin"
spec.email = "assaf@labnotes.org"
spec.homepage = "http://github.com/assaf/pipemaster"
Expand Down
10 changes: 10 additions & 0 deletions test/unit/test_server.rb
Expand Up @@ -148,4 +148,14 @@ def test_restarting_background
sync.close!
end

def test_ping_command
start
assert_equal Pipemaster::VERSION, hit("127.0.0.1:#@port", :ping).last
end

def test_list_command
start :commands => { :foo => lambda { } }
assert_equal "foo\nlist\nping", hit("127.0.0.1:#@port", :list).last
end

end

0 comments on commit bd7644b

Please sign in to comment.