From: Joel VanderWerf Date: 2006-03-19T04:05:53+09:00 Subject: Re: How to write a Ruby service(?)? Pat Maddox wrote: > On 3/18/06, Randy Kramer wrote: >> On Saturday 18 March 2006 08:19 am, Randy Kramer wrote: >>> I need to write what might be called a Ruby service or server(?)--a Ruby >>> program that: >>> * runs only one instance even if "invoked" multiple times >>> * can be sent additional "commands" via CLI commands, e.g., if the name >>> of the program is, for example, "program" I can issue multiple CLI commands >>> like: >> Oops, wait--I'm starting to wake up now--that would be a daemon, wouldn't it? >> Off to Google [Ruby daemon]. >> >> But, any hints appreciated! >> >> Randy Kramer >> >> > > Well fwiw, usually you would write two apps - a client and a server. > The server will be invoked once and daemonized, and you use the client > to issue commands. You can connect to the client either via TCP or > unix domain sockets. That approach has been working really nicely for me. Some suggestions: - look on raa for daemon or daemonize, it's got the 5 or 6 basic steps you need to do to make a process a daemon on unix/linux (maybe use Daniel Berger's win32service on windows--I haven't tried that). - when the server starts, it reads its URI from a config file, and sets up drb (or other protocol) at that URI. If the service is already running there, bail out (so the server is unique). (I like drbunix for this, since you can chmod the socket.) - the client has a command line option to read from a server config file, so you just have to know the filename, not URI (assuming you are on the same host). - the client uses the return code to indicate server up/down, to make shell scripting easy ("client --remote server.config || server --start --file server.config") - the client can have an additional command line option (or set of them) to send drb requests to the server I can send you a link to the code that I use for this, but it's mixed in with a lot of other stuff... (the command line doc is at http://path.berkeley.edu/vii/viicatl/doc/ruby-api/files/doc/cli_txt.html). -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407