一个针对 Ruby 的小型守护进程框架,具有日志记录、错误处理、调度等功能。
Small daemon framework for ruby, with logging, error handler, scheduling and much more.
My inspiration was forever for node.js written by Charlie Robbins. My scheduling inspiration was taken from clockwork written by Adam Wiggins.
There are some alternatives, one of the best is resque, so why another daemons framework? In my servers I've several daemons and what I need is:
As like sinatra and padrino I need a thin framework to do these jobs in few seconds. This mean that:
So, if you have my needs, Forever can be the right choice for you.
$ gem install foreverbPlace your script under your standard directory, generally on my env is bin or scripts.
In that case is: bin/foo
…Assign right permission:
$ chmod +x bin/foostart the daemon:
$ bin/fooyou should see an output like:
$ bin/foo
=> Process demonized with pid 19538you can stop it:
$ bin/foo stop
=> Found pid 19538...
=> Killing process 19538...You can use every method to schedule repetitive tasks.
Every allow the option :at to specify hour or minute and the option :last to specify when the every must start to loop.
:last: can be nil or a Time class. Default is 0.
:at: can be nil, a string or an array of formatted strings. Default is nil.
every 1.second, :at => '19:30' # => every second since 19:30
every 1.minute, :at => ':30' # => every minute but first call wait xx:30
every 5.minutes, :at => '18:' # => every five minutes but first call was at 18:xx
every 1.day, :at => ['18:30', '20:30'] # => every day only at 18:30 and 20:30
every 60.seconds, :last => Time.now # => will be fired 60 seconds after you launch the appRemember that :at:
:So looking our example:
…Running the example with the following code:
$ examples/sample; tail -f -n 150 examples/log/sample.log; examples/sample stopyou should see:
…In foreverb we have a couple of filters, before and after, like rspec you should be able to filter before :all or before :each.
before :all do
puts "This will be ran only at start"
end
before :each do
puts "Do that before each job"
end
# ... here jobs ...
after :all do
puts "This will be ran only at shutdown"
end
after :each do
puts "Do that after each job"
end…$ foreverb list
RUNNING /Developer/src/Extras/githubwatcher/bin/githubwatcher
RUNNING /Developer/src/Extras/foreverb/examples/sample
Reading config from: /Users/DAddYE/.foreverb$ foreverb list -m
PID RSS CPU CMD
5528 168 Mb 0.1 % Forever: /Developer/src/Extras/githubwatcher/bin/githubwatcher
5541 18 Mb 0.0 % Forever: /Developer/src/Extras/foreverb/examples/sample$ foreverb stop foo
Do you want really stop Forever: bin/foo with pid 19538? y
Killing process Forever: bin/foo with pid 19538...
$ foreverb stop --all -y
Killing process Forever: /usr/bin/githubwatcher with pid 2824
Killing process Forever: examples/sample with pid 2836$ foreverb start github
Do you want really start /Developer/src/Extras/githubwatcher/bin/githubwatcher? y
=> Found pid 5528...
=> Killing process 5528...
=> Process demonized with pid 14925 with Forever v.0.2.2as for stop we allow --all and -y
$ foreverb restart github
Do you want really restart /Developer/src/Extras/githubwatcher/bin/githubwatcher? y
=> Found pid 5528...
=> Killing process 5528...
=> Process demonized with pid 14925 with Forever v.0.2.2as for stop we allow --all and -y
$ foreverb tail github
[22/07 11:22:17] Quering git://github.com/DAddYE/lipsiadmin.git...
[22/07 11:22:17] Quering git://github.com/DAddYE/lightbox.git...
[22/07 11:22:17] Quering git://github.com/DAddYE/exception-notifier.git...
[22/07 11:22:17] Quering git://github.com/DAddYE/lipsiablog.git...
[22/07 11:22:17] Quering git://github.com/DAddYE/purple_ruby.git...you can specify how many lines show with option -n, default is 150
This command would be helpful if you change pid log path, in this way the global config file ~/.foreverb will be update
using latest informations from yours deamons
Note that you can personalize the config file setting FOREVER_PATH matching your needs.
$ foreverb update github
Do you want really update config from /Developer/src/Extras/githubwatcher/bin/githubwatcher? yas for stop we allow --all and -y
Bundler has the bad behavior to load Gemfile from your current path, so if your daemons (ex: githubwatcher)
is shipped with their own Gemfile to prevent errors you must insert that line:
ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', __FILE__) # edit matching your Gemfile pathI highly suggest to use fork and before filters when you are using forever with frameworks, this since running same job on our ruby will eat a lot of
ram, so the better way that I found is that:
Forever.run :fork => true do
before :each do
require '/config/boot' # here the rails/padrino environment
end
every 10.seconds, :at => ['12:00', '00:00'] do
Project.all(&:perform_long_task)
end
every 1.minute do
Account.all.map(&:send_emails)
end
endThis is similar to create a new process i.e.:
Process.fork do
require '/config/boot'
my_long_jobs
Project.all(&:perform_long_task)
end
Process.waitallUse the following script if you want foreverb to fire up all of your daemons at boot time in Linux:
…You'll have to create one script per each user foreverb runs on. After creating the file, make it executable:
chmod +x /etc/init.d/foreverb-usernameand add it to the system's boot:
RedHat:
sudo /sbin/chkconfig --level 345 foreverb-username onDebian/Ubuntu:
sudo /usr/sbin/update-rc.d -f foreverb-username defaultsGentoo:
sudo rc-update add foreverb-username defaultTo see a most comprensive app running foreverb + growl see githubwatcher gem
DAddYE, you can follow me on twitter @daddye or take a look at my site daddye.it
Copyright (C) 2011 Davide D'Agostino - @daddye
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
暂无开放 Issues,或尚未同步最近议题。