running a tasking using :first_at date in the past causes problems?

221 views
Skip to first unread message

the_professional

unread,
Feb 7, 2011, 6:51:12 PM2/7/11
to Rufus Ruby
I'm trying to run a task every two weeks starting today, but in my
tests, its seems that when rufus scheduler starts up, it tries to run
the task however many times it has 'missed' until it catches up.

So for example, if I try something like this

require 'chronic'
scheduler = Rufus::Scheduler.start_new

scheduler.every '20m', :first_at => Chronic.parse('February 7 2011
at 5pm') do
User.send_emails if Rails.env.eql?('development')
end

if the time is 5:57 ... scheduler will try to send the emails twice to
make up for the times it 'missed' at 5:20 and 5:40 ... make sense?
Is there a way to get it to send as normal without trying to 'catch
up'?

I'm worried that if I put it on a production server, it will spam a
people each time I restart it.
PS: Apart from the issue I mentioned above it seems to be fine sending
out emails as scheduled


John Mettraux

unread,
Feb 7, 2011, 7:45:53 PM2/7/11
to rufus...@googlegroups.com

Hello,

---8<---
require 'chronic'

scheduler = Rufus::Scheduler.start_new

if Rails.env == 'development'

opts = {}


first_at = Chronic.parse('February 7 2011 at 5pm')

opts[:first_at] = first_at if first_at < Time.now

scheduler.every '20m', opts do
User.send_emails
end
end
--->8---

Best regards,

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

the_professional

unread,
Feb 7, 2011, 8:30:37 PM2/7/11
to Rufus Ruby
Thanks for the fast response John.
I will try this first thing tomorrow morning, but I if you don't mind
me asking ... why does yours work and mine doesn't?

the_professional

unread,
Feb 7, 2011, 8:36:09 PM2/7/11
to Rufus Ruby
And looking at it now ... that will run 20 minutes from the time the
server/passenger is restarted if the first_at time is in the past
right?
That is a problem, because I want this to be regular ... hence the
first_at date.
To elaborate ... if the server is restarted on a Tuesday (and say
instead of '20m' we had '2w'), then this would run every 2 weeks from
Tuesday because my start date would be in the past ... I want it to
run every other monday.

Let me know what you think.
And again, thanks for taking a look at it.

On Feb 7, 6:45 pm, John Mettraux <jmettr...@openwfe.org> wrote:

John Mettraux

unread,
Feb 7, 2011, 8:39:11 PM2/7/11
to rufus...@googlegroups.com

On Mon, Feb 07, 2011 at 05:30:37PM -0800, the_professional wrote:
>
> Thanks for the fast response John.
> I will try this first thing tomorrow morning, but I if you don't mind
> me asking ... why does yours work and mine doesn't?

Hello,

sorry for my terse response, I thought the code said it all.

I simply don't pass the :first_at if it points to the past.

Usually people will use first_at with some relative value

scheduler.every '2d', :first_at => Time.now + 3600 do
# ...
end

Your first_at is "hardcoded", simply put it away.

John Mettraux

unread,
Feb 7, 2011, 8:42:36 PM2/7/11
to rufus...@googlegroups.com

On Mon, Feb 07, 2011 at 05:36:09PM -0800, the_professional wrote:
>
> And looking at it now ... that will run 20 minutes from the time the
> server/passenger is restarted if the first_at time is in the past
> right?
> That is a problem, because I want this to be regular ... hence the
> first_at date.
> To elaborate ... if the server is restarted on a Tuesday (and say
> instead of '20m' we had '2w'), then this would run every 2 weeks from
> Tuesday because my start date would be in the past ... I want it to
> run every other monday.

Hello again,

why are you wasting your time with an "every" ?

---8<---
scheduler.cron '0 6 * * mon' do
# do it...
end
--->8---

will run every monday at 6am.

the_professional

unread,
Feb 8, 2011, 12:28:01 AM2/8/11
to Rufus Ruby
Thanks I got that far using cron when I tried to do it.
but I want it to run every other monday (every two weeks) ... do you
know how to do that.

Also, where would I look in the rufus gem to edit this behavior ... so
it doesn't run past or old scheduled jobs?

John Mettraux

unread,
Feb 8, 2011, 12:37:04 AM2/8/11
to rufus...@googlegroups.com

On Mon, Feb 07, 2011 at 09:28:01PM -0800, the_professional wrote:
> Thanks I got that far using cron when I tried to do it.
> but I want it to run every other monday (every two weeks) ... do you
> know how to do that.
>
> Also, where would I look in the rufus gem to edit this behavior ... so
> it doesn't run past or old scheduled jobs?

Hello,

why not something like

---8<---
alternate = true

scheduler.cron '0 6 * * mon' do

if alternate
# do it
end
alternate = ! alternate
end
--->8---

?

John Mettraux

unread,
Feb 8, 2011, 9:53:31 AM2/8/11
to rufus...@googlegroups.com

And what about leveraging your Chronic-fu ?

https://github.com/mojombo/chronic

---8<---
schedule.every '2w', :first_at => Chronic.parse('next tuesday') do
# just do it ...
end if Rails.env == 'development'
--->8---

Reply all
Reply to author
Forward
0 new messages