Openwferu + Rails + Mongrel-cluster = multiple schedulers

3 views
Skip to first unread message

Dacht...@gmail.com

unread,
Jul 25, 2007, 9:39:58 AM7/25/07
to OpenWFEru users
Hello,

I am using the Openwferu scheduler in a Rails application.
It is currently being started from environment.rb.
I run the application using Mongrel-cluster.
Because this uses 3 different processes I get 3 schedulers and 3 x the
actions scheduled.

I don't know much about threads,
but is it possible to have only the first mongrel process startup the
scheduler?

Or is there another way of doing this?

I know this is more an Ruby / Rails question, but I am a bit lost
here.
I've looked into other schedulers but they don't work for me.

John Mettraux

unread,
Jul 25, 2007, 10:26:51 AM7/25/07
to openwfe...@googlegroups.com
On 7/25/07, Dacht...@gmail.com <Dacht...@gmail.com> wrote:
>
> Hello,
>
> I am using the Openwferu scheduler in a Rails application.
> It is currently being started from environment.rb.
> I run the application using Mongrel-cluster.
> Because this uses 3 different processes I get 3 schedulers and 3 x the
> actions scheduled.
>
> I don't know much about threads,
> but is it possible to have only the first mongrel process startup the
> scheduler?

Hi,

I don't know anything about Mongrel-cluster.

1). you could dig Mongrel-cluster documentation to see if there is a
way to communicate between the clustered instances and prevent
instances other than instance 0 from using the Scheduler.

2). you could use an empty 'semaphore' file. Each instance would not
start the scheduler if the file exists else it will immediately create
(touch) the file and the start the scheduler.

3). maybe there is a way for your instances to know that they are
instances in a cluster and maybe there is a way for them to know "yes,
I'm the first instance", "no, I'm not the first instance" and behave
accordingly.

4). avoid all of that but putting the scheduling logic outside of any
instance, maybe with code started once, alongside the Mongrel-cluster.


I hope this will help, best regards,

--
John Mettraux -///- http://jmettraux.openwfe.org

Dacht...@gmail.com

unread,
Jul 26, 2007, 8:41:50 AM7/26/07
to OpenWFEru users
ahh,
finally I fixed it.
Thx John.
I managed to get it working using a 'semaphore' file.
I hoped there was another way of communicating, but I didn't think of
using a file.
Now I hope we will be on one host for a while....otherwise I'll use
memcache.

For others figuring out this problem, this is what I use now:
##########################################
require 'fileutils'
require 'openwfe/util/scheduler'
include OpenWFE

semaphore = RAILS_ROOT + '/tmp/scheduler'
if File.exist?(semaphore)
logger.warn "scheduler already started"
else
begin
logger.warn "starting scheduler #{ Time.now }"

scheduler = Scheduler.new
scheduler.start

scheduler.schedule_every('4h') do
# do something
end

FileUtils.touch(semaphore)

rescue Exception => ex
logger.warn ex
end
end
##########################################

Very simple, but aren't all answers?

Thx again John!

On Jul 25, 4:26 pm, "John Mettraux" <jmettr...@openwfe.org> wrote:

Lee Fyock

unread,
Jul 26, 2007, 8:45:57 AM7/26/07
to openwfe...@googlegroups.com
How do you know when to delete the semaphore file, so that you can shut down the server, start it up, and have the scheduler run again?

Where did you place this code? In environment.rb (or a file that gets called from environment.rb)?

Thanks,
Lee

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "OpenWFEru users" group.
To post to this group, send email to openwfe...@googlegroups.com
To unsubscribe from this group, send email to openwferu-users-unsubscribe @googlegroups.com
For more options, visit this group at http://groups.google.com/group/openwferu-users?hl=en
-~----------~----~----~----~-- ----~----~------~--~---


Dacht...@gmail.com

unread,
Jul 26, 2007, 9:44:25 AM7/26/07
to OpenWFEru users
no, not from environment.rb.
That would give the same problem, because this also gets run multiple
times.
I use capistrano to update my app, and I made an hook that removes
this file when the application gets updated and before it restarts.

# part of my capistrano2 config-file
###############################################
desc "Run this before the restart"
before "deploy:restart", :remove_semaphore

desc "Removing semaphore file to start the scheduler"
task :remove_semaphore do
semaphore = "#{ release_path }/tmp/scheduler"
sudo "rm -f #{semaphore}"
end
###############################################

On Jul 26, 2:45 pm, "Lee Fyock" <lee.fy...@gmail.com> wrote:
> How do you know when to delete the semaphore file, so that you can shut down
> the server, start it up, and have the scheduler run again?
> Where did you place this code? In environment.rb (or a file that gets called
> from environment.rb)?
>
> Thanks,
> Lee
>

Lee Fyock

unread,
Jul 26, 2007, 9:55:01 AM7/26/07
to openwfe...@googlegroups.com
You could run it from environment.rb, though, since your semaphore file takes care of multiple app instances running the scheduler. That's the whole point, right? :-)

I'm not very familiar with capistrano. Is it responsible for launching your application as well? If so, couldn't you skip the whole semaphore thing and have capistrano start and stop the scheduler, rather than an application instance starting the scheduler?

Thanks,
Lee


no, not from environment.rb .

Dacht...@gmail.com

unread,
Jul 26, 2007, 10:36:44 AM7/26/07
to OpenWFEru users
You can start it from the environment.rb, yes, but you need to remove
the semaphore file before starting he mongrels.
This cannot be done from within the environment.rb because each
mongrel starts its own process.

I want to have the scheduler inside my Rails application.
I could run it using script/runner but then I have to maintain another
process.
This way the scheduler is stopped when I stop the application and vice-
versa.

On Jul 26, 3:55 pm, "Lee Fyock" <lee.fy...@gmail.com> wrote:
> You could run it from environment.rb, though, since your semaphore file
> takes care of multiple app instances running the scheduler. That's the whole
> point, right? :-)
> I'm not very familiar with capistrano. Is it responsible for launching your
> application as well? If so, couldn't you skip the whole semaphore thing and
> have capistrano start and stop the scheduler, rather than an application
> instance starting the scheduler?
>
> Thanks,
> Lee
>

> On 7/26/07, Dachtutn...@gmail.com <Dachtutn...@gmail.com> wrote:
>
>
>
> > no, not from environment.rb.

Lee Fyock

unread,
Jul 26, 2007, 10:40:27 AM7/26/07
to openwfe...@googlegroups.com
So, where inside your application do you start the scheduler? Inside application.rb?

In case you can't tell from my questions, we have a similar configuration (multiple mongrel instances) and I'm thinking of using OpenWFEru for some periodic tasks.

Thanks!
Lee

Dacht...@gmail.com

unread,
Jul 27, 2007, 3:27:54 AM7/27/07
to OpenWFEru users
It is currently being started from environment.rb.
I remove the 'metaphoore' file before (re)starting the mongrel
cluster.
Then, in the scheduler.rb I check for the 'metaphore' file.
If i doesn't exist, it starts the scheduler and creates the file.
The 'metaphore' file is just an empty file in my tmp dir.
Metaphore stands for a signalling file.
This setup works fine, when all mongrels are on one server.
Otherwise you should put the signal somewhere all mongrels can see it,
like memcached or the database.

On Jul 26, 4:40 pm, "Lee Fyock" <lee.fy...@gmail.com> wrote:
> So, where inside your application do you start the scheduler? Inside
> application.rb?
> In case you can't tell from my questions, we have a similar configuration
> (multiple mongrel instances) and I'm thinking of using OpenWFEru for some
> periodic tasks.
>
> Thanks!
> Lee
>

Anselmo

unread,
Jul 27, 2007, 5:55:04 AM7/27/07
to OpenWFEru users
I haven´t test this yet, but let me ask in advance?
Can't we use a Singleton on that Scheduler?

Cheers
Anselmo

Dacht...@gmail.com

unread,
Jul 27, 2007, 5:58:54 AM7/27/07
to OpenWFEru users
no, the mongrels are seperate processes.
Each mongrel will start it's own singleton. Only one!.
But with 3 mongrels that gives 3 schedulers.
Try my solution in an test application.
Setup an schedule that prints every 10 seconds or so.
Reply all
Reply to author
Forward
0 new messages