What's the best way to run scheduled tasks in a Rails environment? Script/runner? Rake?
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
- Is there a way to remove IDV Tags from an AIFF fil
- Rails how to handle error and exceptions in model
相关文章
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- form_for wrong number of arguments in rails 4
- Rspec controller error expecting <“index”> but
- Factory_girl has_one relation with validates_prese
In our project we first used whenever gem, but confronted some problems.
We then switched to RUFUS SCHEDULER gem, which turned out to be very easy and reliable for scheduling tasks in Rails.
We have used it for sending weekly & daily mails, and even for running some periodic rake tasks or any method.
The code used in this is like:
To learn more: https://github.com/jmettraux/rufus-scheduler
The problem with whenever (and cron) is that it reloads the rails environment every time it's executed, which is a real problem when your tasks are frequent or have a lot of initialization work to do. I have had issues in production because of this and must warn you.
Rufus scheduler does it for me ( https://github.com/jmettraux/rufus-scheduler )
When I have long jobs to run, I use it with delayed_job ( https://github.com/collectiveidea/delayed_job )
I hope this helps!
Both will work fine. I usually use script/runner.
Here's an example:
0 6 * * * cd /var/www/apps/your_app/current; ./script/runner --environment production 'EmailSubscription.send_email_subscriptions' >> /var/www/apps/your_app/shared/log/send_email_subscriptions.log 2>&1
You can also write a pure-Ruby script to do this if you load the right config files to connect to your database.
One thing to keep in mind if memory is precious is that script/runner (or a Rake task that depends on 'environment') will load the entire Rails environment. If you only need to insert some records into the database, this will use memory you don't really have to. If you write your own script, you can avoid this. I haven't actually needed to do this yet, but I am considering it.
Here's how I have setup my cron tasks. I have one to make daily backups of SQL database (using rake) and another to expire cache once a month. Any output is logged in a file log/cron_log. My crontab looks like this:
The first cron task makes daily db backups. The contents of cron_tasks are the following:
The second task was setup later and uses script/runner to expire cache once a month (lib/monthly_cron.rb):
I guess I could backup database some other way but so far it works for me :)
The paths to rake and ruby can vary on different servers. You can see where they are by using:
I Use script to run cron, that is the best way to run a cron. Here is some example for cron,
Open CronTab —> sudo crontab -e
And Paste Bellow lines:
00 00 * * * wget https://your_host/some_API_end_point
Here is some cron format, will help you
Hope this will help you :)
Use Craken (rake centric cron jobs)