Communicate with a daemon

P

Paul A.

Hello everyone,

After daemonize a Ruby process (thanks Ruby version 1.9), it is possible
to communicate with on the same machine by using socket on localhost. Do
you know some other methods instead of socket?

Thanks.
 
J

James Gray

After daemonize a Ruby process (thanks Ruby version 1.9), it is
possible
to communicate with on the same machine by using socket on
localhost. Do
you know some other methods instead of socket?

Sure. In I daemon I've been working on, I just use SQLite as a queue
for messages. I use the Amalgalite extension, which is terrific and
works well on Ruby 1.9.

When you're going to write to the queue, make sure to grab an
"exclusive" lock (via the "transaction" SQL command). When reading,
grab an "immediate" lock first. Set a busy handler through Amalgalite
that retries for a reasonable time (I use 60 seconds) to grab locks.
Add some error handling code for if the lock wait does eventually
timeout, but that shouldn't happen under normal operations.

About the only challenges I ran into were deleting data from the
queue. Deleting counts as a write, so grab an "exclusive" lock. That
was easy of course. The downside is that the SQLite database needs to
have the "vacuum" command run on it every so often to reclaim wasted
disk space. You can't be holding a lock when you do that though.
Thus I locked on an external source about once a day, and vacuumed the
database. If it's just a simple queue you need (with no long term
storage), you could also just nuke the file and rebuild it periodically.

Anyway, I hope that gives you some fresh ideas.

James Edward Gray II
 
R

Robert Klemme

After daemonize a Ruby process (thanks Ruby version 1.9), it is possible
to communicate with on the same machine by using socket on localhost. Do
you know some other methods instead of socket?

You have got the whole range of IPC at your fingertips

- named FIFO,
- unnamed FIFO if the client is a child process of your daemon,
- Unix domain socket (only on the local machine),
- signals,
- shared memory (with proper locking, e.g. via semaphores),
....

Kind regards

robert
 
B

Brian Candler

Robert said:
You have got the whole range of IPC at your fingertips

- named FIFO,
- unnamed FIFO if the client is a child process of your daemon,
- Unix domain socket (only on the local machine),
- signals,
- shared memory (with proper locking, e.g. via semaphores),
...

And don't forget the simple DRb, if both ends are Ruby:

http://web.archive.org/web/20070217144414rn_1/wiki.rubygarden.org/Ruby/page/show/DrbTutorial

Also XMLRPC (included in the Ruby standard library, at least it was for
1.8, don't know if it's still in 1.9), and also plenty of ways to embed
a HTTP server into your application (e.g. Webrick, Sinatra)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,939
Members
47,484
Latest member
JackRichard

Latest Threads

Top