Daemons and Paths

Discussion in 'Ruby' started by Davertron, Mar 14, 2009.

  1. Davertron

    Davertron Guest

    So I've written a daemon using the 'daemons' gem. I have essentially
    3 pieces:

    daemon_controller => Controls startup/shutdown of daemon (in the style
    of the first example of the daemons documentation)

    daemonized_script => This script is the main program; it runs
    continuously in the background looking for work to do, and when it
    finds work, it forks itself and uses 'exec' to run a worker

    worker => processes the job given to it by the daemonized_script and
    exits

    The problem I'm having is that my worker program expects to be able to
    'require' a few other classes I've written. It expects them to be in
    the same directory as it (or in a lib directory one dir up,
    whatever). The obvious issue here is that when the daemon_controller
    executes the daemonized_script as a daemon, it changes the directory
    to '/' and executes from there. I temporarily fixed this issue by
    putting in absolute paths in my source code to test and verify that
    everything else works as expected, which it does, so my question is
    this; how do I get a dynamic path set so that the worker process
    doesn't error when it can't find the files it needs? I can't use
    absolute paths because my program won't always be installed to the
    same directory, and anyway that's not a very flexible solution. I
    should also mention that all of these files live in the same directory
    structure, so relative paths should work fine. In other words, I'm
    not putting daemon_controller somewhere else in the filesystem and
    then trying to call my other files from there; they'll all be either
    in the same directory, or in close proximity to each other so that I
    only have change a directory up or down to get to the other files.

    Any response will be much appreciated.
     
    Davertron, Mar 14, 2009
    #1
    1. Advertising

  2. I don't know if this is exactly what your looking for, but how about a
    call to locate?

    begin
    #worker code here

    rescue
    path = %x[locate <filename>].chomp! #the filename would be whatever the
    daemon is
    #looking for - you take the error
    code from
    #the daemon and pull the filename
    out of it

    #Here we are going to take the path output
    #then remove the last array element it's
    #split by, aka the filename, so that u will
    #have the directory

    directory = path.split("/")
    directory.pop
    directory.join("/")
    Dir.chdir(directory)

    retry #retries the begin block

    end
    --
    Posted via http://www.ruby-forum.com/.
     
    Michael Linfield, Mar 14, 2009
    #2
    1. Advertising

  3. Michael Linfield wrote:
    > I don't know if this is exactly what your looking for, but how about a
    > call to locate?
    >
    > begin
    > #worker code here
    >
    > rescue
    > path = %x[locate <filename>].chomp!


    > #the filename would be whatever the daemon is
    > #looking for - you take the error code from
    > #the daemon and pull the filename out of it
    >
    > #Here we are going to take the path output
    > #then remove the last array element it's
    > #split by, aka the filename, so that u will
    > #have the directory
    >
    > directory = path.split("/")
    > directory.pop
    > directory.join("/")
    > Dir.chdir(directory)
    >
    > retry #retries the begin block
    >
    > end


    My apologies, correct the line that says directory.join("/") to the
    following:

    directory = directory.join("/")

    Regards,

    - Mac
    --
    Posted via http://www.ruby-forum.com/.
     
    Michael Linfield, Mar 14, 2009
    #3
  4. Davertron

    Davertron Guest

    On Mar 14, 6:33 pm, Michael Linfield <> wrote:
    > Michael Linfield wrote:
    > > I don't know if this is exactly what your looking for, but how about a
    > > call to locate?

    >
    > > begin
    > > #worker code here

    >
    > > rescue
    > > path = %x[locate <filename>].chomp!
    > > #the filename would be whatever the daemon is
    > > #looking for - you take the error code from
    > > #the daemon and pull the filename out of it

    >
    > > #Here we are going to take the path output
    > > #then remove the last array element it's
    > > #split by, aka the filename, so that u will
    > > #have the directory

    >
    > > directory = path.split("/")
    > > directory.pop
    > > directory.join("/")
    > > Dir.chdir(directory)

    >
    > > retry #retries the begin block

    >
    > > end

    >
    > My apologies, correct the line that says directory.join("/") to the
    > following:
    >
    > directory = directory.join("/")
    >
    > Regards,
    >
    > - Mac
    > --
    > Posted viahttp://www.ruby-forum.com/.


    Thanks Michael. You're right, it's not exactly what I'm looking for,
    but it would probably work. I was looking more towards modifying my
    RUBYPATH environment variable to get this work; something that doesn't
    require putting in a bunch of special-case code inside of the source
    itself, especially above all of my "require" statements. I was
    thinking of writing a "locate_and_require" statement with your
    suggestions above if there really is no "right" way to do this. I've
    never written daemons before, so I don't really know how people
    generally handle this, and perhaps my expectations are out-of-whack.
    Anyway, I did find code that shows modifying of the $: variable inside
    a script to add/change your ruby path; however, it doesn't look like
    the changes survive daemonization. I'll keep looking around and post
    how I solved this once I get there.
     
    Davertron, Mar 16, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noah
    Replies:
    5
    Views:
    818
  2. about daemons and IPC

    , Aug 27, 2006, in forum: Python
    Replies:
    4
    Views:
    327
    Irmen de Jong
    Aug 29, 2006
  3. PurpleServerMonkey

    Python, Daemons and D-Bus

    PurpleServerMonkey, May 24, 2008, in forum: Python
    Replies:
    3
    Views:
    351
    Diez B. Roggisch
    May 25, 2008
  4. Fernando M. Maresca

    logging and daemons

    Fernando M. Maresca, Feb 16, 2009, in forum: Python
    Replies:
    1
    Views:
    304
    Diez B. Roggisch
    Feb 16, 2009
  5. Ohad Lutzky

    Paths, gentleman, paths

    Ohad Lutzky, Nov 6, 2006, in forum: Ruby
    Replies:
    2
    Views:
    210
    David Vallner
    Nov 7, 2006
Loading...

Share This Page