After writing rails_cron, and working with backgroundrb, I realized that I want something simpler. Much simpler. So I wrote the daemon_generator plugin. I think the whole thing is about 50 lines of code.
"./script/generate daemon <name>" gives you a simple stub that you can dump code in that will run in the rails environment as a background process. You're responsible for making it do something useful (try accessing stored tasks in a memcache or DB-based queue). If you want messaging, try backgroundrb.
Features:
- Capistrano-friendly i.e.:
task :after_restart do
run "#{current_path}/script/daemons restart"
end - Per-daemon start scripts
- Per-app start scripts
- Graceful stop and restart
- Per-daemon logging in your log folder
- Automatic restart of a crashed daemon
- Not thread-based
Go get the plugin:
daemon_generator 0.5.0 | README
A bug I've found with rails_cron is that using threads and running two Rails tasks simultaneously just doesn't work - one of the two jobs just exits quietly.
I know that things like Mongrel and WEBrick look at the ActiveRecord concurrency setting and wrap their own protection around Rails calls, but rails_cron doesn't.
For my use, I changed the code to run my jobs on the main thread, which makes them sequential, but I no longer get strange results.
Posted by: Tom | September 01, 2006 at 12:00 AM
Tom: That's one of the reasons that I wanted to go simpler.
Daemon_generator lets you run Rails processes in the background, without concern for threads, databases, and messaging. Bring your own, debug your own.
Posted by: Kyle | September 03, 2006 at 05:08 PM
I really like the sound of this library. I tried installing it, but I tried to run the generator, it said that there wasn't any generators of that name. Do you have any idea of what could be going on?
Posted by: Jordan Arentsen | September 18, 2006 at 10:29 PM
Hi Kyle.
Great piece of code, I've made good use of this week.
I've written a small how-to guide on setup, as it took me a while to get it worked out (thanks to other comments here).
It's available at: http://douglasfshearer.dyndns.org/blog/cron-jobs-in-ruby-on-rails Let me know what you think, I'm open to additions and corrections.
Cheers.
Posted by: Douglas F Shearer | November 08, 2006 at 04:52 PM
Is there an explanation of the options in daemons.yml anywhere?
Posted by: Brent | December 13, 2006 at 01:01 PM
great plugin - working very well for me. Thanks!
One very,very small issue I ran into...
the ctl files generated in lib/daemons/
do a require on active_support - I think to
get access to Hash with_indiffent_access on the
options initialized from config/daemons.yml...
The problem is that the require for active_support
by passes your vendor directory.
Most people will have rails installed in their
ruby installation so they won't notice it...
In my case, I don't have root access - so I can't
install rails to my system ruby. I have installed in my vendor directory..
So I just got rid of the active_support require
and did a hack on the options hash to create
symbol based keys from the string based keys that get loaded out of the daemons.yml file...
Anyway, thanks for the plugin..
cheers,tom
Posted by: Tom Fowler | December 29, 2006 at 06:25 PM
How does this compare to dctl? http://dctl.rubyforge.org/
Posted by: non | February 28, 2007 at 08:23 AM
Anyone having issues with stop/start/restart on capistrano deployment? I've added a after_deply hook to call "./script/daemons restart" but this will not start the daemons on an initial deployment. So I changed it to do a stop and a start, but the daemons are not being stopped and are instead just piling up. 2 deployments (or cap restarts) and I have 2 daemons running. Hope I'm missing something.
Posted by: Travis Hume | September 04, 2007 at 03:10 PM
I can't get my daemons to stop. I call the stop command, but it looks as though they are automatically restarted somehow.
Posted by: Micah | September 04, 2007 at 06:43 PM
The SVN account doesn't appear to be working ... anyone know where to get this code?
Posted by: Toby Hede | September 10, 2007 at 11:41 PM
Kyle, I'm a bit new to Rails, not to mention daemon_generator -- I have a library, lib/etsmon/etsmon.rb that I want to make use of from inside my daemon (the .rb file), but I can't seem to get a require to this library to work.
Any tips?
Thanks,
Ron
Posted by: Ron | November 28, 2007 at 10:35 PM
I've also been having some troubles. I keep getting the following series of errors message. I've been googling around, but can't seem to find much documentation, other than the a cached thread on a similar error occurring on a previous version.
failed to allocate memory
stack level too deep
no such file to load -- daemons
Is there something that I'm doing wrong?
Thanks!
-Mario
Posted by: Mario | November 30, 2007 at 03:18 PM
Kyle, I noticed after I start my daemon, there are two ruby processes running. One has no CPU load and no run time. The other looks like its the one actually doing the processing, is this normal or did i mess something up here?
-scott
Posted by: Scott | December 04, 2007 at 01:34 PM
Ron,
I got rid of the stack error by replacing
ActiveRecord::Base.logger << "somestr"
with
logger = ActiveRecord::Base.logger
logger.info("somestr")
Posted by: Jon | January 15, 2008 at 09:40 AM
I also have troubles with stopping daemons using capistrano...
Daemons are sometimes stopped. And sometimes just continue running..
I think this could happen when they are busy. The daemon stop process would wait for its work to be finished before actually stopping the process. And I suppose it gets timed out when this is too long.
This could be the reason for the randomness of the stop..
Would be great if you could give us any update around that. No much support and documentation can be found for this.
Anyway, except from that,
great plugin :)
Thanks
Posted by: Fred | January 18, 2008 at 01:57 AM
Hi again guys,
I am just gonna reply to myself here... and all the guys having the same problem as I had.
I just realized the daemons would not stop using capistrano if they where restarted before they would actually get stopped.
I'll explain.
I noticed, as explained in former mail, that the daemons would wait for the current work (in while $running loop) to be finished before they stop.
When using capistrano, the execution of the deployment code has good chance to be faster than that.
Now the problem is when you store your daemons Pid files in a shared folder, with the same name for different version of daemons.
Starting a new daemon would overwrite the old Pid file. If that happens before the daemon actually got stopped. This daemon will never actually get stopped.
To fix this problem, just us a none shared folder for your daemon pids. Somewhere in the current version of your ruby project is just fine...
Personally, I am using the root of my ruby project. As my daemons are located in lib/daemons/, I have set this parameters in daemons.yml:
dir_mode: script
dir: ../..
Now, to answer people asking what options are allowed in this daemons.yml file, you can find all your answers if you take a quick look inside daemons.rb file, into the daemon gem. Everything is here, in comment.
Here is the exemple provided (not in yml format):
# === Example:
# options = {
# :app_name => "my_app",
# :ARGV => ['start', '-f']
# :dir_mode => :script,
# :dir => 'pids',
# :multiple => true,
# :ontop => true,
# :mode => :exec,
# :backtrace => true,
# :monitor => true,
# :script => "path/to/script.rb"
# }
Once again...
Great work on this gem !
Posted by: Fred | January 18, 2008 at 06:52 AM
One last thing...
Sorry for monologue..
You would have to disable monitoring using:
monitor: false
from your daemons.yml file.
Because the monitor was actually restarting former daemons... when they would finally get stopped -_-'
I am not proud of that, to be honest, but this is the only solution I found right now.
Fred
Posted by: Fred | January 18, 2008 at 07:12 AM
Hiya Kyle!
Looks like your repo is down, and has been for a few days. Have you retired it or will it become accessible again? After a thousand issues with BackgroundRB I'm trying to move to something more lightweight... like Daemons!
Cheers
Mike
Posted by: Mike Stenhouse | February 27, 2008 at 06:31 AM
hi kyle
I'm trying to set up daemon generator in my project. I've followed instruction from the following link
http://douglasfshearer.com/blog/cron-jobs-in-ruby-on-rails
But after starting daemon i've noticed that production.log is blank :( I followed the test.rb.log file where it says
*** below you find the most recent exception thrown, this will be likely (but not certainly) the exception that made the application exit abnormally ***
#= 0.0.0)
>
*** below you find all exception objects found in memory, some of them may have been thrown in your application, others may just be in memory because they are standard exceptions ***
#
#
#
#
#= 0.0.0)
>
Mysql is working fine with my other applications. I've recent version of daemon 1.0.10 and ruby gem.
What else i should do? Please help....
Posted by: Arman | August 11, 2008 at 02:46 AM
Hi,
any one know how i can stop a running daemons.
I tried all this command.
./lib/daemons/_ctl stop
./script/daemons stop
but none is working.
i'm getting this error on my live server and i can't restart it.
Pls any one, can help me.
Thanks,
Posted by: Dino | November 17, 2009 at 10:41 PM
Hi I liked your note, add your site to your bookmarks.
Posted by: RamonGustav | August 28, 2010 at 10:59 AM
I think you have a thorough understanding in this matter. You describe in detail all here.
Posted by: RamonGustav | August 31, 2010 at 10:00 PM
Are you eager to secure funds for that dream project of yours?
Posted by: Buy_Viagra | September 17, 2010 at 06:16 AM
Congratulations! You have so much useful information, write more.
Posted by: Buy_Viagra | September 17, 2010 at 05:01 PM
Have you been turned down by other lenders?
Posted by: Buy_Viagra | September 18, 2010 at 03:09 AM