Single scheduler for mongrel cluster. How to start?

115 views
Skip to first unread message

Georg Ledermann

unread,
Oct 8, 2008, 8:54:46 AM10/8/08
to Rufus Ruby
With Rails 2.1.1, Rufus 1.0.11 and a cluster of 3 Mongrels on a Linux
production sever I wonder how to start the scheduler so it's running
only ONCE. My current solution is to start it in the class like this:

# my_model.rb
class MyModel < ActiveRecord::Base
def self.do_some_regular_work
....
end

....
end

if RAILS_ENV == 'production'
ActiveRecord::Base.allow_concurrency = true

scheduler = Rufus::Scheduler.start_new
scheduler.schedule "5 0 * * *" do # Every night at 0:05
MyModel.do_some_regular_work
ActiveRecord::Base.verify_active_connections!
end
end

But with this, the scheduler is startet for every Mongrel instance,
which is wrong. How to ensure that it's startet only once? I have
experimented with using Rails environment, but it doesn't work...

Kind regards,
Georg

John Mettraux

unread,
Oct 9, 2008, 12:17:30 AM10/9/08
to rufus...@googlegroups.com
On Wed, Oct 8, 2008 at 9:54 PM, Georg Ledermann <lede...@gmail.com> wrote:
>
> With Rails 2.1.1, Rufus 1.0.11 and a cluster of 3 Mongrels on a Linux
> production sever I wonder how to start the scheduler so it's running
> only ONCE. My current solution is to start it in the class like this:

Hi Georg,

sorry for the late reply, I'm currently travelling.

Have you tried to use a lock file ?

---8<---
require 'FileUtils'

if RAILS_ENV == 'production' && (not File.exist?('tmp/my_scheduler.lock'))
FileUtils.touch('tmp/my_scheduler.lock')
scheduler = ...
end
--->8---

Note that it would then be necessary to remove that lock file when
Mongrel shuts down :

---8<---
if Module.constants.include?('Mongrel') then
class Mongrel::HttpServer
alias :old_graceful_shutdown :graceful_shutdown
def graceful_shutdown
FileUtils.rm('tmp/my_scheduler.lock', :force => true)
old_graceful_shutdown
end
end
end
--->8---


I hope this will help, best regards,

--
John Mettraux - http://jmettraux.wordpress.com

Georg Ledermann

unread,
Oct 9, 2008, 9:21:50 AM10/9/08
to Rufus Ruby
On 9 Okt., 06:17, "John Mettraux" <jmettr...@openwfe.org> wrote:

> sorry for the late reply, I'm currently travelling.
>
> Have you tried to use a lock file ?

Thank you very much, this works fine!

Kind regards,
Georg

tim

unread,
Nov 16, 2008, 6:30:12 PM11/16/08
to Rufus Ruby
>
> Note that it would then be necessary to remove that lock file when
> Mongrel shuts down :
>
> ---8<---
> if Module.constants.include?('Mongrel') then
>   class Mongrel::HttpServer
>     alias :old_graceful_shutdown :graceful_shutdown
>     def graceful_shutdown
>       FileUtils.rm('tmp/my_scheduler.lock', :force => true)
>       old_graceful_shutdown
>     end
>   end
> end


Could something similar be done with mod_rails (passenger phusion)?

John Mettraux

unread,
Nov 16, 2008, 11:51:10 PM11/16/08
to rufus...@googlegroups.com

Hi Tim,

I guess it's possible to do the same with passenger.

Creating the lock file would be the same.

For the shutdown part, you'd have to locate/identify the shutdown
hook/method for passenger.

Maybe this simple piece of code could work instead :

---8<---

at_exit do


FileUtils.rm('tmp/my_scheduler.lock', :force => true)

end

--->8---

(There were sometimes problem with it and Mongrel, hence the specific
Mongrel shutdown hook).

Reply all
Reply to author
Forward
0 new messages