scheduler 'singleton'

72 views
Skip to first unread message

John Mettraux

unread,
Oct 19, 2008, 7:28:47 PM10/19/08
to rufus...@googlegroups.com
- Xianhang Zhang asked on the blog (
http://jmettraux.wordpress.com/2008/07/18/rufus-scheduler-109-released/#comments
) :
>
> I'm trying to integrate rufus-scheduler into Rails and I'm running into an
> annoyance that you have to keep track of the scheduler object across
> different Rails calls. Is there a correct way to do this?

Hello,

you could do something like :

---8<---
module Rufus
MyScheduler = scheduler.new
end

#...

Rufus::MyScheduler.schedule_every('10d') { puts 'hello' }
--->8---

> I would really like something like Rufus::Scheduler.all_schedulers which will
> return all the schedulers in the system so I don't have to worry about
> persistence from within Rails itself.

Are you thinking about a hash ?

---8<---
Rufus::Scheduler.all_schedulers[:main_scheduler] = Scheduler.new
Rufus::Scheduler.all_schedulers[:main_scheduler].schedule("* * * * *")
{ puts 'hello' }
--->8---

maybe :

---8<---
class Rufus::Scheduler
def self.all_schedulers
(@all_schedulers ||= {})
end
end
--->8---


I don't understand the 'persistence' part of your question.
Rufus-scheduler doesn't do anything about persistence.


Best regards,

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

Xianhang Zhang

unread,
Oct 20, 2008, 11:51:52 PM10/20/08
to Rufus Ruby


On Oct 19, 4:28 pm, "John Mettraux" <jmettr...@openwfe.org> wrote:
> - Xianhang Zhang asked on the blog (http://jmettraux.wordpress.com/2008/07/18/rufus-scheduler-109-release...
> ) :
>
>
>
> > I'm trying to integrate rufus-scheduler into Rails and I'm running into an
> > annoyance that you have to keep track of the scheduler object across
> > different Rails calls. Is there a correct way to do this?
>
> Hello,
>
> you could do something like :
>
> ---8<---
> module Rufus
>   MyScheduler = scheduler.new
> end
>
> #...
>
> Rufus::MyScheduler.schedule_every('10d') { puts 'hello' }
> --->8---

Ah thanks. Previously, I had been using Rails.cache but this way seems
more elegant.

>
> > I would really like something like Rufus::Scheduler.all_schedulers which will
> > return all the schedulers in the system so I don't have to worry about
> > persistence from within Rails itself.
>
> Are you thinking about a hash ?
>
> ---8<---
> Rufus::Scheduler.all_schedulers[:main_scheduler] = Scheduler.new
> Rufus::Scheduler.all_schedulers[:main_scheduler].schedule("* * * * *")
> { puts 'hello' }
> --->8---
>
> maybe :
>
> ---8<---
> class Rufus::Scheduler
>   def self.all_schedulers
>     (@all_schedulers ||= {})
>   end
> end
> --->8---
>
> I don't understand the 'persistence' part of your question.
> Rufus-scheduler doesn't do anything about persistence.
>
> Best regards,
>
> --
> John Mettraux   -  http://jmettraux.wordpress.com

I was more thinking all_schedulers would return an array of all
previously instantiated schedulers in the system so I could do
something like scheduler = Rufus::Scheduler.all_schedulers[0] ||
Rufus::Scheduler.start_new

But really, what I would like is just Rufus::Scheduler.schedule_at()
and not have to worry about creating a scheduler object at all for the
most simple use cases.

John Mettraux

unread,
Oct 21, 2008, 12:53:46 AM10/21/08
to rufus...@googlegroups.com
On Tue, Oct 21, 2008 at 12:51 PM, Xianhang Zhang <shalm...@gmail.com> wrote:
>
> I was more thinking all_schedulers would return an array of all
> previously instantiated schedulers in the system so I could do
> something like scheduler = Rufus::Scheduler.all_schedulers[0] ||
> Rufus::Scheduler.start_new
>
> But really, what I would like is just Rufus::Scheduler.schedule_at()
> and not have to worry about creating a scheduler object at all for the
> most simple use cases.

Makes lots of sense, I will think about it (initialization/starting
and stopping included).


Thanks and best regards,

Reply all
Reply to author
Forward
0 new messages