Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Daemons and Paths

Reply
Thread Tools

Daemons and Paths

 
 
Davertron
Guest
Posts: n/a
 
      03-14-2009
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.
 
Reply With Quote
 
 
 
 
Michael Linfield
Guest
Posts: n/a
 
      03-14-2009
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/.

 
Reply With Quote
 
 
 
 
Michael Linfield
Guest
Posts: n/a
 
      03-14-2009
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/.

 
Reply With Quote
 
Davertron
Guest
Posts: n/a
 
      03-16-2009
On Mar 14, 6:33*pm, Michael Linfield <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
logging and daemons Fernando M. Maresca Python 1 02-16-2009 05:07 PM
Python, Daemons and D-Bus PurpleServerMonkey Python 3 05-25-2008 11:40 AM
Paths, gentleman, paths Ohad Lutzky Ruby 2 11-07-2006 02:15 AM
about daemons and IPC sdistefano@gmail.com Python 4 08-29-2006 05:36 PM
Convert between Windows style paths and POSIX style paths Noah Python 5 07-11-2003 09:25 PM



Advertisments