Question

Y a-t-il de toute façon de configurer une planification de tâches simple à l'intérieur d'une application Rails? Il y a des pièces de code que je veux exécuter toutes les heures, ou tous les jours ou chaque semaine. Je ne veux pas les séparer en scripts distincts que je dois alors planifier via Cron Jobs. Si je faisais cela, je devrais me souvenir de sauvegarder les scripts, et si je reconstruis un serveur, je dois aller ajouter tous les travaux cron, cela semble un peu désordonné pour ce dont j'ai besoin.

J'aimerais pouvoir planifier un travail quelque part dans mon application Rails et les faire fonctionner par magie quand je le veux! Des idées?

Était-ce utile?

La solution

There are gems/plugins for this, like rufus-scheduler or the more popular 'whenever' gem. I do like @douglassellers' solution though, I hadn't heard about it.

Autres conseils

I use delayed job, a "database backed asynchronous priority queue" to accomplish something similar. Have a look at http://github.com/tobi/delayed_job

You will still need to have something that kicks off 'rake jobs:work' but it is better than having lots of different cron jobs. Notice in the documentation the :run_at attribute – that looks like it might solve the "schedule a the jobs somewhere in my rails app and have them magically run when I want them to" problem.

You can use something like Rails Cron (gem install rails_cron) to do this, which spins off another rails process dedicated to doing scheduled background tasks. I would recommend against doing it this way, as the extra rails instance will have to be monitored and stopped and started along with the rest of your app and, in my experience, is very prone to falling down.

A better solution is to just use Craken, http://github.com/latimes/craken, to manage crontabs through yaml files in your rails app. Each scheduled task gets defined in a yaml file and then on deployment Craken decides whether or not the System Level CronTab file needs to be updated or not. It plugs right into your capistrano deploy file and is reasonably turn-key. This gives the advantage of still managing your scheduled tasks in your rails app while actually executing them with the OS level cron.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top