views:

171

answers:

1

I need to perform a task every 5 seconds, but only when users are using the application.

As for now, I use cron that works every minute and activates a task that repeats itself every 5 seconds with sleeps between, for a minute. However, it works also when the application isn't being used.

Is there a gem that will do this kind of thing?

A: 

If you're using cron, couldn't you just use things particular to the OS to get it to work? As in, use the underlying filesystem or OS, don't rely on Rails.

For example, your Rails app could touch a file whenever a user hits the site at all, which the cron job would check for existing before running its task. It could also check the ctime/mtime of the file and delete it if the file is too long out of date.

In that case, you wouldn't need a gem... just make your cron task smarter.

Platinum Azure
Thanks.Actually, the solution as I saw it, was saving last date of run on a global variable on the app, and have every hit check if more than 5 seconds have passed since last run.I just wanted to know if there's any written code to perform a task like that.
elado