Forum: Ruby Communicate with a daemon

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Paul A. (Guest)
on 2009-05-11 01:22
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.
Bill K. (Guest)
on 2009-05-11 01:40
(Received via mailing list)
From: "Paul A." <removed_email_address@domain.invalid>
>
> 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?

Perhaps http://localmemcache.rubyforge.org/


Regards,

Bill
James G. (Guest)
on 2009-05-11 02:32
(Received via mailing list)
On May 10, 2009, at 4:22 PM, Paul A. wrote:

> 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 G. II
Robert K. (Guest)
on 2009-05-11 10:26
(Received via mailing list)
On 10.05.2009 23:22, Paul A. wrote:
> 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
Brian C. (Guest)
on 2009-05-11 13:16
Robert K. wrote:
> On 10.05.2009 23:22, Paul A. wrote:
>> 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),
> ...

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

http://web.archive.org/web/20070217144414rn_1/wiki...

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)
This topic is locked and can not be replied to.