# Timer Task?

**URL:** <https://www.ruby-forum.com/t/timer-task/68130>\
**Category:** Ruby\
**Created:** [September 9, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/timer-task/68130 "2006-09-09T16:28:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [September 9, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/timer-task/68130/1 "2006-09-09T16:28:27Z")

</div>

Disclaimer: I’m relatively new to Ruby, but I love it. 🙂

I’m writing my first real ruby utility on my own (using the snarl  
library  
[http://www.fullphat.net/](http://www.fullphat.net/) plus [Google Code Archive - Long-term storage for Google Code Project Hosting.](http://code.google.com/p/ruby-snarl/) and  
net/imap for new mail notification), and it’s working fine, other than  
the  
fact that I don’t know how to schedule a recurring job. I’m looking for  
something similar to the Timer class in java for scheduling jobs in  
ruby.  
Is there such a thing in the core api?

Thanks in advance,

Jason

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [September 9, 2006, 6:08pm UTC](https://www.ruby-forum.com/t/timer-task/68130/2 "2006-09-09T18:08:35Z")

</div>

> I’m writing my first real ruby utility on my own (using the snarl library  
> [http://www.fullphat.net/](http://www.fullphat.net/) plus [Google Code Archive - Long-term storage for Google Code Project Hosting.](http://code.google.com/p/ruby-snarl/) and  
> net/imap for new mail notification), and it’s working fine, other than the  
> fact that I don’t know how to schedule a recurring job. I’m looking for  
> something similar to the Timer class in java for scheduling jobs in ruby.  
> Is there such a thing in the core api?

This doesn’t answer your question, but, is your utility actually a  
full-fledged client? Or just what someone would use to send off  
emails/notifications once per session.

My point is, does this utility run constantly? Or just once when needed?

If you leave it running constantly, I think you already have a main  
loop going on, so why not have a little method to handle  
scheduled/recurring events at whatever interval you do have?

Maybe if you could clarify a little better… I must admit, though,  
that this is far from my expertise, and perhaps someone with a little  
more experience with this kind of thing would be able to help you far  
better! (But I’m trying!)

M.T.

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [September 9, 2006, 7:26pm UTC](https://www.ruby-forum.com/t/timer-task/68130/3 "2006-09-09T19:26:43Z")

</div>

Jason Vinson wrote:

/…

> Does sleep cost anything in terms of resources, and if so what is  
> “cheaper”?

AFAIK “sleep” is a perfectly adequate way to idle a thread, and it has  
the  
advantage of being trivial to implement. I have recently seen Ruby  
threads  
that used “sleep” be idled by the thread manager, never to be called  
again,  
but this has to do with Ruby threads within a Ruby process, not native  
threads represented by particular Ruby applications.

[Somewhat OT]

Have you considered using OS-specific facilities like “cron” instead of  
“sleep” for scheduling? I use cron for most of these periodic,  
relatively  
low-powered tasks because it is easy to add a task to cron’s list of  
jobs,  
and it burdens the system less than maintaining a bunch of sleeping  
processes – cron loads, runs, and unloads its tasks, while a sleeping  
process represents an ongoing use of resources.

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [September 9, 2006, 6:22pm UTC](https://www.ruby-forum.com/t/timer-task/68130/4 "2006-09-09T18:22:36Z")

</div>

Hi Matt,

Thanks for the reply. I am trying to schedule a recurring task, so it  
would  
run constantly. I guess my real question is the preferred approach  
something like this?

notifier = SnarlMail.new(“[imap.mailserver.com](http://imap.mailserver.com)”, “username”, “passwd”)

while(true) do  
notifier.check  
sleep 60  
end

Does sleep cost anything in terms of resources, and if so what is  
“cheaper”?

Thanks,  
Jason

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [September 9, 2006, 9:23pm UTC](https://www.ruby-forum.com/t/timer-task/68130/5 "2006-09-09T21:23:00Z")

</div>

-----Original Message-----  
From: Paul L. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
Sent: Saturday, September 09, 2006 1:20 PM  
To: ruby-talk ML  
Subject: Re: Timer Task?

Jason Vinson wrote:

/…

> Does sleep cost anything in terms of resources, and if so what is  
> “cheaper”?

AFAIK “sleep” is a perfectly adequate way to idle a thread, and it has  
the  
advantage of being trivial to implement. I have recently seen Ruby  
threads  
that used “sleep” be idled by the thread manager, never to be called  
again,  
but this has to do with Ruby threads within a Ruby process, not native  
threads represented by particular Ruby applications.

[Somewhat OT]

Have you considered using OS-specific facilities like “cron” instead of  
“sleep” for scheduling? I use cron for most of these periodic,  
relatively  
low-powered tasks because it is easy to add a task to cron’s list of  
jobs,  
and it burdens the system less than maintaining a bunch of sleeping  
processes – cron loads, runs, and unloads its tasks, while a sleeping  
process represents an ongoing use of resources.

–  
Paul L.

> **[\* arachnoid.com](https://arachnoid.com/)**
>
> A site for people who think. Intellectual resources, programming, astronomy, science, mathematics, Java/JavaScript applets, programming instruction, home of Arachnophilia.

…

Thanks for the reply Paul,

The main reason for maintaining a Thread is because there’s a tiny bit  
of  
state involved (I only want to report new mail, not the same new mail  
over  
and over again). I’m open to other alternatives, but writing out to the  
file system seems like the only other option, which I’d prefer not to  
do.

Jason

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [September 9, 2006, 9:45pm UTC](https://www.ruby-forum.com/t/timer-task/68130/6 "2006-09-09T21:45:08Z")

</div>

Jason Vinson wrote:

> [Somewhat OT]
> 
> Have you considered using OS-specific facilities like “cron” instead of  
> “sleep” for scheduling? I use cron for most of these periodic, relatively  
> low-powered tasks because it is easy to add a task to cron’s list of jobs,  
> and it burdens the system less than maintaining a bunch of sleeping  
> processes – cron loads, runs, and unloads its tasks, while a sleeping  
> process represents an ongoing use of resources.

Amen to that! Anything that is done once a minute or less frequently  
should be done with “cron” or the Windows equivalent, whose name I’ve  
forgotten. However, if you need to do something more often than once a  
minute, in most cases “sleep” is a perfectly acceptable way to do it.

One caution: most languages and operating systems can’t guarantee that  
the actual time elapsed between the call to “sleep 10” and the next  
operation will be anything even close to 10 seconds. Usually, the  
guarantee is that it will be _at least_ 10 seconds. 🙂 If you actually  
need precise timing, you’ll need to dig into the OS, system calls,  
priorities, schedulers, etc.

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [September 9, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/timer-task/68130/7 "2006-09-09T22:13:13Z")

</div>

Jason Vinson wrote:

> The main reason for maintaining a Thread is because there’s a tiny bit of  
> state involved (I only want to report new mail, not the same new mail over  
> and over again). Â I’m open to other alternatives, but writing out to the  
> file system seems like the only other option, which I’d prefer not to do.

First, there’s something misconfigured about your newsreader. Look at my  
earlier post, and your reply (to which I am replying). You will see that  
my  
text isn’t “quoted” in your reply, but it appears as though it is the  
new  
content.

Second, writing to a file in /tmp could be a reasonable way to get the  
state  
you need. You could purge the file on each reboot. Nevertheless, I see  
your  
point about statefulness.

Another way to do this is to have state information in the user’s home  
directory (I meant to suggest the user-level cron facility, not the root  
cron), under the name of the application, that can be updated without  
requiring systemwide resources.

It’s a tradeoff worth thinking about, because a continuous, sleeping  
Ruby  
app requires a Ruby interpreter to provide for it, whereas a Ruby app  
executed by cron only requires transient resources.

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [September 10, 2006, 1:21am UTC](https://www.ruby-forum.com/t/timer-task/68130/8 "2006-09-10T01:21:41Z")

</div>

> The main reason for maintaining a Thread is because there’s a tiny bit of  
> state involved (I only want to report new mail, not the same new mail over  
> and over again). I’m open to other alternatives, but writing out to the  
> file system seems like the only other option, which I’d prefer not to do.

I think not using this fantastic resource available to us is a bit  
silly. That’s the whole reason the filesystem exists: persistent  
storage! So, store the state and the emails, or just the state if you  
prefer. Whichever. But I think it’s a bit silly not to use the  
filesystem.

Also, I’m definitely in favor of cronning something like this as well.

😃

M.T.
