Timer Task?

Disclaimer: I'm relatively new to Ruby, but I love it. :slight_smile:

I'm writing my first real ruby utility on my own (using the snarl library
http://www.fullphat.net/ plus 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

I'm writing my first real ruby utility on my own (using the snarl library
http://www.fullphat.net/ plus 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.

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", "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

路路路

-----Original Message-----
From: Matt Todd [mailto:chiology@gmail.com]
Sent: Saturday, September 09, 2006 12:04 PM
To: ruby-talk ML
Subject: Re: Timer Task?

I'm writing my first real ruby utility on my own (using the snarl library
http://www.fullphat.net/ plus 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.

Jason Vinson wrote:

/...

I guess my real question is the preferred approach
something like this?

notifier = SnarlMail.new("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"?

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 Lutus
http://www.arachnoid.com

Jason Vinson wrote:

/...

I guess my real question is the preferred approach
something like this?

notifier = SnarlMail.new("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"?

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.

路路路

-----Original Message-----
From: Paul Lutus [mailto:nospam@nosite.zzz]
Sent: Saturday, September 09, 2006 1:20 PM
To: ruby-talk ML
Subject: Re: Timer Task?

--
Paul Lutus
http://www.arachnoid.com

......................................

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

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. :slight_smile: If you actually
need precise timing, you'll need to dig into the OS, system calls,
priorities, schedulers, etc.

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.

路路路

--
Paul Lutus
http://www.arachnoid.com

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.

:smiley:

M.T.