Topic: Running daemons - How & Where?

I'm new to RoR and have a project that requires a constant socket connection and all incoming messages to be saved to a file or db.

I thought having a daemon running would be the best way but so far I can't find how to set it up and where in the code structure they belong.

I have the daemon gem installed but can't find any information on how to integrate daemons into an existing project.

Re: Running daemons - How & Where?

Does the daemon even belong in a rails project?  By it's very nature a daemon is really separate from a web app.  Do you want to administer the daemon from your rails app?  start it, stop it, restart it, configure it?   Or do you just need to get a hold of the data that the daemon brings into the server and use it in your rails app?

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Running daemons - How & Where?

There is no need to administer the daemon from the app - It should be running all the time, saving the data and the app just displays the data that the daemon collects.

Re: Running daemons - How & Where?

If the daemon writes data to a file in the file system somewhere,  you can just read that data from within the rails application using the Ruby file primitives.

i.e.

def showdaemondata

  data = File.open('/path/to/daemon/data.txt', 'r') { |f| f.read }

  # do something with data

end

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Running daemons - How & Where?

That's fine, but WHERE do I put the file that contains the daemon code?? 

I've searched the web but can't work it out.

Re: Running daemons - How & Where?

It depends on your hosting provider,  but the fail safe answer is anywhere in Rails.root,  anywhere in the root of your rails app.

You could put it in Rails.root + '/public' ,but then it's open to perusal from the public.

Rails.root + '/lib'  is a typical place to put oddball homegrown ruby code,  but that's more for executable code.

You can make up your own directory in Rails.root, i.e. Rails.root + '/mydaemon',  Rails won't care if there is an extra directory it doesn't know about.  These are all possible:

/home/idals/railsapp/mydaemon
/home/idals/railsapp/lib/mydaemon
/home/idals/railsapp/public  # everyone can potentially see it
/home/idals/railsapp/public/mydaemon  # everyone can potentially see it

Or you could move the directory up one level,  say you had TWO rails apps that both needed access,  you could do this:

/home/idals/mydaemon
/home/idals/app1
/home/idals/app2

In that case you would read the file like this

data = File.open("#{Rails.root}/../mydaemon/output.txt",'r') { |f| f.read }

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.