On Sat, 5 Aug 2006, [ISO-8859-1] Une bue wrote:

> i've an app with 2 daemons, i'd like to write all the log messages to
> the same file.
>
> then i suppose i need to lock the file when one source is writing and
> wait the file is unlocked for the other ?
>
> how this could be done in ruby, or is it automagic ))


mussel:~ > ri 'File#flock'
------------------------------------------------------------- File#flock
      file.flock (locking_constant ) =>  0 or false
------------------------------------------------------------------------
      Locks or unlocks a file according to _locking_constant_ (a logical
      _or_ of the values in the table below). Returns +false+ if
      +File::LOCK_NB+ is specified and the operation would otherwise have
      blocked. Not available on all platforms.

      Locking constants (in class File):

         LOCK_EX   | Exclusive lock. Only one process may hold an
                   | exclusive lock for a given file at a time.
         ----------+------------------------------------------------
         LOCK_NB   | Don't block when locking. May be combined
                   | with other lock options using logical or.
         ----------+------------------------------------------------
         LOCK_SH   | Shared lock. Multiple processes may each hold a
                   | shared lock for a given file at the same time.
         ----------+------------------------------------------------
         LOCK_UN   | Unlock.

      Example:

         File.new("testfile").flock(File::LOCK_UN)   #=> 0


you also need to make sure that buffers are flushed after each write or the
msgs will be interleaved.

if you're on windows you'll need one on these

   http://rubyforge.org/projects/win32utils/

but i forget which one provides locking - probably 'win32-file'

regards.

-a
-- 
happiness is not something ready-made.  it comes from your own actions.
- h.h. the 14th dali lama