I'm new to rails and want to run a batch file/schedule task daily once at midnight that checks which entries have expired. Every record in the table has a closing_date and after that time, such records must be inactive.(status active=false on DB). so basically it will run 2 SQL queries to fetch all records and then flag another field to inactive for records that are outdated.I'm working with Rails 5.
How should I go about this-gem(rufus,whatever,clockwork or any other gem) or simply some system tool for cronjob?I'm going to change my DB to PostgreSQL so will that impact? Any suggestions or sample code anyone can share to get an idea.
In brief: You can use whenever Gem for this purpose.
You need to create a rake task for where you will write your SQL.
Then schedule it as cron job using whenever
Detailed explanation
First step will be creating a rake task. You can do this using the console
rails g task my_namespace my_task1
This will create a file named my_namespace.rake in the lib/tasks folder with initial content like
namespace :my_namespace do
desc "TODO"
task task1: :environment do
# Your code will go here
end
end
You can check whether your task is running properly by running
rake my_namespace:task1
in the console.
Now you need to schedule the job using the whenever gem.
gem 'whenever', :require => false in Gemfile.
Run bundle install
run wheneverize command in terminal. This will create a schedule.rb file in config folder .
Add the following code to schedule your rake task:
every 1.day, at: '8:00 pm' do
rake "my_namespace:task1"
end
Run this command: whenever --update-crontab. In case your environment is development, use command: whenever --update-crontab --set environment='development' (see this question.
run crontab -l to check whether cron job has been added.
Related
I'm trying to execute a simple rake task using whenever gem but the code isn't being executed.
I already set the environment to development, I updated the cron using the whenever --update-crontab command and the rake task works well if I run the command on console. But, when I run the server the log file is not being generated.
I saw a question here too with the same problem but it was solved setting the environment to development, but didn't work out for me.
My rake task:
namespace :testando do
task :consulta => :environment do
produto = Produto.first
puts produto.nm_produto
end
end
My schedule.rb:
set :output, "#{path}/log/cron_log.log"
set :environment, 'development'
every 1.minute do
rake "testando:consulta"
end
I'm using rails 5.0.0.1 and I'm programing in Cloud9, so I think the OS is Ubuntu.
What's missing ?
Update:
I followed the instructions of the main answer in this topic Cron job not working in Whenever gem
And it worked! The task is running even with the server not being started (with "rails s" command).
please run crontab -l to see if you have updated the crontab successfully
Having a series of rake tasks that should be translated by the whenever gem into the cron file, I was wondering why the takes shows were pointing to an old release.
It cannot be asserted that whenever is active somehow, even though it is listed in the gem file (and associated lock file) and deployment refers to whenever in the deployment as follows:
tar: DEBUG [1f7d4e56] bin/whenever: time stamp 2016-01-08 15:01:20 is 88.787104175 s in the future
update Checking bundle exec whenever -v returns the proper version. Need bundle exec there...
Capfile includes require "whenever/capistrano" after calls to bundler and rails.
require 'capistrano/bundler'
require 'capistrano/rails'
require 'whenever/capistrano'
Note: this is being tested in development mode.
Functional answer. The instructions are misleading If you don't need different jobs running on different servers in your capistrano deployment, then you can safely stop reading now and everything should just work the same way it always has. Keep on reading.
The nugget is nested after this statement. Roles default to [:db]. Thus two sources of error are possible:
different job_roles on different machines are not specified in schedule.rb
Check your environment file. If "db" is not listed, whenever will not fire.
I had the same issues with using Capistrano whenever plugin, I solved it by making custom deploy shell scripts, cap production deploy being one command of many, and then inclding cap production cron:regen; inside this script I called deploy.sh, with the command inside the deploy.rb being:
namespace :cron do
desc "restart cron"
task :regen do
on roles(:app) do |host|
rails_env = fetch(:stage)
execute_interactively "crontab -r;bundle exec whenever --update-crontab;crontab -l;"
end
end
end
def execute_interactively(command)
port = fetch(:port) || 22
exec "ssh root##{fetch(:ip)} -t 'cd SERVER_PATH_OF_YOUR_APP && #{command}'"
end
I use these functions for all types of different commands, since Capistrano still gives me problems with a lot of native plugins it uses.
If you're not happy with the whenever/capistrano, you can create yourself a simple Capistrano task to update the cron jobs:
namespace :deploy do
desc "Update crontab with whenever"
task :update_cron do
on roles(:app) do
within current_path do
execute :bundle, :exec, "whenever --update-crontab #{fetch(:application)}"
end
end
end
after :finishing, 'deploy:update_cron'
end
The task will be called when the code deployment is finished.
Im scratching my head here wondering if I'm barking up the wrong tree. I have a server which I've deployed a Rails app onto using Capistrano. Recently I added a new data type to one of the models, and now I need to run a Rake task to update the existing records.
After a lot of Googling I'm starting to wonder if people use Rake tasks with Capistrano. Some forum posts from 2013 or so mention that Capistrano supports the .rake extension. Whereas other posts I've found indicate that Capistrano has its own task automation system, and is incompatible with rake.
I found Cape, but I'm unsure if this is what I'm looking for as it seems to convert Rake tasks into cap recipes. Its possible I'm mistaken on this point, I really don't have any experience working with Capistrano or even working in the full stack spectrum.
What I'm wondering is this: How do I run a simple Rake task on my remote server?
Some quick points for clarity, I've installed the app on the latest Ubuntu LTS, 14.10 if memory serves. I followed the tutorial found here. I have full sudo access and I can ssh into the server.
thanks in advance for helping a noob
If you need to update models, you can of course write a Rails migration - this will ensure that it's run if it hasn't been run yet.
The easiest way to execute a rake task on the server would be just via ssh if it's a one-time task. See the last paragraph in the tutorial you mentioned:
cd /opt/www/testapp/current ; bin/rake RAILS_ENV=production db:seed
To answer your original question about rake: you can execute rake tasks via capistrano similar to how you would execute it locally, only within the capistrano script. Here's an example:
deploy.rb:
namespace :rake do
desc "My task"
task :my_task do
on roles(:app) do
within "#{current_path}" do
with rails_env: :production do
execute :rake, "my_task"
# !!!see NOTE at end of answer!!!
end
end
end
end
end
You can view all your cap tasks via cap -T locally. The capistrano task I wrote above should show up as cap tasks:my_rake_task.
If you want to be ably to run any available rake task without configuring, do the following:
namespace :rake do
desc "Invoke rake task"
task :invoke do
on roles(:app) do
within "#{current_path}" do
with rails_env: :production do
execute :rake, ENV['task']
# !!!see NOTE at end of answer!!!
end
end
end
end
end
Then you can write:
cap production deploy:invoke task=my:rake:task
NOTE: you might want to replace the execution line with
run "bin/rake RAILS_ENV=#{rails_env} #{ENV['task']}"
to use the same syntax as the tutorial (without the binstubs you might need to configure capistrano/bundler and capistrano/rbenv first ...)
Check out capistrano-rake
Once installed, run any rake task on your production/staging servers without messy capistrano recipes by simply doing this:
$ cap production invoke:rake TASK=your:rake:task
Full Disclosure: I wrote it
I am using Ruby on Rails and Heroku.
I would like something like rake db:migrate, where it will only run the parts that have not been run before.
I can set a rake task that can run other rake task, but is there in way to only run those that did not run before?
What are any alternative, if I want rake tasks to be run automatically?
first of, running a rake task twice, ie rake db:migrate will neither duplicate nor overwrite your database columns if they already exist.
For example capistrano, used to deploy your rails app to basically anywhere, will rerun tasks like db:migrateor assets:precompile every time you deploy your app. So no need to worry about that really.
To run tasks automatically you will need something like a cron job. Whenever is a great ruby gem that allows you to install a job like this with the beloved ruby syntax.
After installing the gem and running wheneverize . in the root of your rails app, you may edit the scheduler.rb and add something like:
every :sunday, :at => '12pm' do
rake my:awesome:task
end
The Github page as well as the default scheduler.rb hold many useful examples of how these jobs are built.
with whenever --update-crontab you can write your rubyesque cronjobs into the crontab, which will run your tasks periodically at the time you want them to.
** edit
multiple tasks in one:
task :setup => [:a, :b, :c]
task :a do
%x(bash command)
end
task :b do
rake db:migrate
end
task :c do
rake whatever
end
I'm working with delayed job for active record gem https://github.com/collectiveidea/delayed_job I'm trying to set up a job to run five minutes after an event occurs in my application. After five minutes passes, I need to make some database updates. I've tried rake jobs:work and RAILS_ENV=development script/delayed_job start. Prior to this all, I have run bundle install, rails generate delayed_job:active_record, and rake db:migrate. I have a lottery website that needs to check winners every five minutes and update tokens for winning players.
I wait five minutes, but no updates are made in my local application.
Here's what I have so far:
Gem File:
gem 'delayed_job_active_record'
gem "daemons"
Job (located in lib)
class WinnersJob < Struct.new(:blast_id)
def perform
...
end
Controller
require 'winners'
Delayed::Job.enqueue(WinnersJob.new(blast.id), 1, 5.minutes.from_now)
end
I think you have to launch the background workers locally using foreman. The following worked on my Mac.
From Heroku docs:
You then need to tell your application to process jobs put into your job queue, you can do that by adding this to your Procfile:
worker: bundle exec rake jobs:work
Now when you start your application using Foreman it will start processing your job queue.
foreman start
Having said all that, unless you are deploying on a Mac, it doesn't really matter if they run locally. (I noticed this after I got it working.) It only matters if it works on your servers. If you are deploying on Heroku, then Delayed Job works well.
Reference:
https://devcenter.heroku.com/articles/delayed-job
https://devcenter.heroku.com/articles/procfile