Hay un montón de cambios para hacer, cosas por mejorar, etc, pero básicamente anda :)
Created
August 13, 2012 20:00
-
-
Save inkel/3343720 to your computer and use it in GitHub Desktop.
Dæmons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Daemon | |
@running = false | |
@pidfile = nil | |
@stdin = nil | |
@stdout = nil | |
@stderr = nil | |
def daemonize! | |
Process.daemon true | |
end | |
def chdir! path | |
Dir.chdir File.absolute_path(path) | |
end | |
def pid_file path | |
@pid_file = File.absolute_path(path) | |
end | |
def stdin path | |
@stdin = File.absolute_path(path) | |
STDIN.reopen @stdin | |
end | |
def stdout path | |
@stdout = File.absolute_path(path) | |
STDOUT.reopen @stdout, "a" | |
end | |
def stderr path | |
@stderr = File.absolute_path(path) | |
STDERR.reopen @stderr, "a" | |
end | |
def running? | |
@running | |
end | |
def start! | |
if @pid_file && File.exists?(@pid_file) | |
pid = File.read(@pid_file).strip | |
begin | |
Process.kill 0, pid.to_i | |
STDERR.puts "Daemon is already running with PID #{pid}" | |
exit 2 | |
rescue Errno::ESRCH | |
run! | |
end | |
else | |
run! | |
end | |
end | |
def run! | |
save_pid_file | |
@running = true | |
trap(:INT) { @running = false; stop } | |
run | |
end | |
def stop! | |
if @pid_file && File.exists?(@pid_file) | |
pid = File.read(@pid_file).strip | |
begin | |
Process.kill :INT, pid.to_i | |
rescue Errno::ESRCH | |
STDERR.puts "No daemon is running with PID #{pid}" | |
exit 3 | |
end | |
else | |
STDERR.puts "Couldn't find a PID file" | |
exit 1 | |
end | |
end | |
def stop; end | |
def cli | |
{ | |
"-D" => self.method(:daemonize!), | |
"-C" => self.method(:chdir!), | |
"-P" => self.method(:pid_file), | |
"-out" => self.method(:stdout), | |
"-err" => self.method(:stderr), | |
"-in" => self.method(:stdin) | |
} | |
end | |
def daemon_usage | |
<<-USAGE | |
Daemon introduced command line arguments: | |
-D Daemonize this process | |
-C Change directory. | |
-P Path to PID file. Only used in daemonized process. | |
-out Path to redirect STDOUT. | |
-err Path to redirect STDERR. | |
-in Path to redirect STDIN. | |
USAGE | |
end | |
private | |
def save_pid_file | |
File.open(@pid_file, "w") do |fp| | |
fp.write(Process.pid) | |
end if @pid_file | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
require "clap" | |
require_relative "daemon" | |
module DW | |
extend Daemon | |
def self.run | |
while running? | |
puts Process.pid | |
puts Dir.pwd | |
sleep 1 | |
end | |
end | |
def self.usage | |
puts <<-USAGE | |
#{$0} [options] (start|stop) | |
start Starts the daemon | |
stop Stop a running daemon | |
#{daemon_usage} | |
USAGE | |
exit | |
end | |
def self.cli | |
super.merge("-h" => DW.method(:usage)) | |
end | |
end | |
case Clap.run(ARGV, DW.cli).first | |
when "start" | |
DW.start! | |
when "stop" | |
DW.stop! | |
else | |
DW.usage | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment