I’m in the process of moving from the J2EE to the Ruby world (+ Apache,
Linux, MySQL, etc.), so this is likely to be a “stupid newbie” question.
I use mod_ruby and understand that FastCGI operates along similar lines:
there is one instance of the Ruby interpreter per Apache thread, but of
course there may be lots of Apache threads, so lots of interpreters.
I want to log an event either to a log file or a database. Since this
would be executed for every download from my site I think the log file
route may be more appropriate (=faster).
My problem: “How can I write (append) to the same log file from multiple
interpreter instances at least potentially simultaneously?”
In Java, I know that my application server has multiple threads and
multiple classloaders, EARs, WARs and so on, but a single virtual
machine; I’d create a singleton to open the file and synchronize the
calls to the write method. Mission accomplished.
As I understand it Ruby will only allow me to open a file in append mode
if nobody else currently has the file open for writing/appending. So
when two interperters try to open the file at the same time, all but
one will fail.
I could loop until the file is available but that would crash my app if
the file is kept open for any reason. I could just accept that I can’t
write to the log file and forget logging the line if I can’t open the
file immediately but of course I’d be losing data… or I could write a
daemon running its own single interpreter that serializes the writes…
but surely this already exists somewhere?
This must be frequent problem and I’m sure I’m missing something? What
Thanks for your help.