Hi I making a petition site. In my site i Have three types of petitions, every have self period. When period is passed it need to change petition status(integer value) automaticaly.
How to do this better? And wich time rails use on server, from my pc or from server?
I ask about because I need somehow to test it manually.
I'm noviece, so if it's possible help me with simpliest alghoritm. I don't need a pure safety and performance.
I would suggest implementing it as a rake task, that will run every day as a cron job and update the mentioned value if record matches the condition(exactly one year passed).
Using this way you avoid problems related to performance as the rake task launches in separate background and also you may run the task when your server isn't overloaded by other tasks, for example at night.
Following gem is useful to setup jobs by schedule https://github.com/javan/whenever
Related
I want to know if there is a way in RoR to run a rake task, or a ruby code, twice in different times. For example, when a user registers, run the task after three days, and then run the same task one week later, but no more('stop the process'). I was looking at crontab and gems like Resque, Stalker, Starling, etc. but I don't have a clear idea who this gems can help me. I'm thinking in run a daemon for each user and count the task executions.The problem is that the daemon would be active all that time "eating" resources. Is there a way to use as least resources as possible?. I want to run this in Heroku later.
I usually solve this type of thing with date columns on the record (eg. first_reminder_sent_at, user_responed_at, week_reminder_sent_at). In this case, a date for the first item, the user's response, and for the week-later item.
Create a cron task to call a rake task - look at all users that are > 3 days old, < 1 week, user hasn't responded, date not set.
Queue up the Background Job.
The Job will send the mail (or whatever the task is) and then set the date field on the record.
Cron task to call a rake task - looks at all users that are > 1 week old where user hasn't responded and date not set.
Send the reminder and set the date in a Background Job.
Try the whenever cron and create a rake task, which is doing your stuff.. and in the database make some trigger fields when and how the rake task should do his actions..
Posix systems have the at command which is a dead simple way to schedule a job to run once at a later time. Though there's nothing wrong with using an application based queueing mechanism to schedule later work as outlined in the other answers. at is host specific so probably unsuitable if you're running a cluster, especially if the hosts may be transient.
I am trying to find out the best way to run scripts in the background. I have been looking around and found plenty of options, but many/most seem to have become inactive in the past few years. Let me describe my needs.
The rails app is basically a front-end to configure when and how these scripts will be run. The scripts run and generate reports and send email alerts. So the user must be able to configure the start times and how often these scripts will run dynamically. The scripts themselves should have access to the rails environment in order to save the resulting reports in the DB.
Just trying to figure out the best method from the myriad of options.
I think you're looking for a background job queuing system.
For that, you're either looking for resque or delayed_job. Both support scheduling tasks at some point in the future -- delayed_job does this natively, whereas resque has a plugin for it called resque_scheduler.
You would enqueue jobs in the background with parameters that you specify, and then at the time you selected they'll be executed. You can set jobs to recur indefinitely or a fixed number of times (at least with resque-scheduler, not sure about delayed_job).
delayed_job is easier to set up since it saves everything in the database. resque is more robust but requires you to have redis in your stack -- but if you do already it's pretty much the ideal solution for your problem.
I recently learned about Sidekiq, and I think it is really great.
There's also a RailsCast about it - Sidekiq.
Take a look at the gem whenever at https://github.com/javan/whenever.
It allows you to schedule tasks like cron jobs.
Works very well under linux, and the last commit was 14 days ago. A friend of mine used it in a project and was pretty satisfied with it.
edit: take a look at the gem delayed_job as well, it is good for executing long tasks in the background. Useful when creating a cron job only to start other tasks.
I have a rails app with a Location model, which has a rating, and a rating_count field. Now, I need to reset every Location's rating and rating_countattributes to 0 at a specific time everyday, lets say 12:00:00 UTC.
How would I accomplish this? I'm using the default sqlite3 databases.
The best option is to use cron. You can find tons of documentation out there!Although if you are running a Rails app you should check out whenever a pretty neat gem for managing cron jobs for your app!
The easiest thing is to write a rake task that does that job, and then use whatever scheduling system your host uses (cron).
An alternative is to use delayed_job which allows to push work to a background process. While delayed-job is not exactly suited for something like this, it is perfectably capable of doing this. If your rails process starts, you add a new job, to run at 12:00. And the running of the job reschedules the job.
The nice thing of delayed-job is that your code runs in the context of a rails-process, so you can use methods you already have. Also nice: jobs are stored in the database, so you can have an overview.
If you're on a *nix box, write a script to do the updates (eg. in PHP, or Perl) and simply add it to crontab. Check out cron.
If I need to create recurring tasks in delayed jobs, what is a clean solution? I have an import task that I want to run every 5 minutes, but I don't want to fire up rails/rake in order to tell it to create a Delayed job that can be picked up. If Rails is already running on a system, perhaps I can just create an HTTP request that will make the rails app fire off a DJ? I could put that cron task in a ruby script which runs every 5 minutes, making requests to a server, but without firing up rails. What do you think?
This fork of delayed_job has recurrence built right in (and there may be other forks that do the same):
http://github.com/andrewroth/delayed_job
The cron approach still seems to be quite clean. It need only involve running a rake invocation, not a full Rails server. Rake doesn't need Rails to be running to work.
However if you really don't like that approach then you could arrange for the recurring jobs to be re-queue themselves when they are being processed setting a run_at time to 5 minutes (or whatever) in the future. Obviously you'd need to prime the queue the first time and make sure the delayed_job server stays running
About that... I have the same desire,
I'm planning to have a cron to fire up a curl request at a specific route at my site every 5 minutes, so it runs a action with the result, and I'm gonna be pretty sure it only ran once.
My purposes includes: Awarding Badges and compiling some Averages
I'm using Delayed Job to manage background work.
However I have some tasks that need to be executed at regular interval. Every hour, every day or every week for example.
For now, when I execute the task, I create a new one to be executed in one day/week/month.
However I don't really like it. If for any reason, the task isn't completely executed, we don't create the next one and we might lose the execution of the task.
How do you manage that kind of things (with delayed job) in your rails apps to be sure your regular tasks list remains correct ?
If you have access to Cron, I highly recommend Whenever
http://github.com/javan/whenever
You specify what you want to run and at what frequency in dead simple ruby, and whenever supplies rake tasks to convert this into a crontab and to update your system's crontab.
If you don't have access to frequent cron (like I don't, since we're on Heroku), then DJ is the way to go.
You have a couple options.
Do what you're doing. DJ will retry each task a certain number of times, so you have some leniency there
Put the code that creates the next DJ job in an ensure block, to make sure it gets created even after an exception or other bad event
Create another DJ that runs periodically, checks to make sure the appropriate DJs exist, and creates them if they don't. Of course, this is just as error prone as the other options, since the monitor and the actual DJ are both running in the same env, but it's something.
Is there any particular reason why you wouldn't use cron for this type of things?
Or maybe something more rubyish like rufus-scheduler, which is quite easy to use and very reliable.
If you don't need queuing, these tools are a way to go, I think.