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
Related
I'm running a small demo site. I'd like to reset the database once an hour or so (automatically, not manually). I could write a rake task to destroy records for each of the models. Is there a simpler approach?
How best to accomplish this?
You can use Heroku Scheduler to schedule a rake task. If you rake task runs with heroku run then it works with scheduler too.
Please refer this link to how to automate rake task on heroku
http://kakimotonline.com/2014/04/27/using-rake-to-automate-heroku-tasks/
Your rake file will look something like this:
namespace :heroku do
def run_command(cmd, app_name)
Bundler.with_clean_env do
sh build_command(cmd, app_name)
end
end
def build_command(cmd, app_name)
"heroku #{cmd} --app #{app_name}"
end
end
Following the example linked by Akshay, I was able to get this working, although not exactly as I planned.
First, a clarification. To add the scheduler add-on on Heroku, you have to provide your credit card number. I'm still getting my bearings with the Heroku platform and am not ready to do that quite yet. So my solution works without the scheduler add-on. Instead it runs as a rake task from your local dev environment and uses the heroku toolbelt to run the reset command on your remote app.
If you are looking to run everything on Heroku, I don't think this will work as you can't run the heroku pg:reset command on Heroku itself. However, it looks like this solution here would work.
Now to my solution.
First, create the following rake file:
# lib/tasks/heroku.rake
namespace :heroku do
# bundle exec rake heroku:reset_db['my-app-name']
# Note: run locally with Heroku toolbelt to reset DB on app
desc 'Reset database with seed data'
task :reset_db, [:app_name] do |t, args|
run_command("pg:reset DATABASE_URL --confirm #{args.app_name}", args.app_name)
run_command("run rake db:migrate", args.app_name)
run_command("run rake db:seed", args.app_name)
end
# Helper Functions
# Source: http://kakimotonline.com/2014/04/27/using-rake-to-automate-heroku-tasks/
def run_command(cmd, app_name)
Bundler.with_clean_env do
sh build_command(cmd, app_name)
end
end
def run_command_with_output(cmd, app_name)
Bundler.with_clean_env do
`#{build_command(cmd, app_name)}`
end.gsub("\n", "")
end
def build_command(cmd, app_name)
"heroku #{cmd} --app #{app_name}"
end
end
Now you can reset your database by running the following rake command from your local environment with your app name (run heroku apps for your app name):
bundle exec rake heroku:reset_db['my-app-name']
And if you want to schedule it to run at regular intervals as I did, add a job to your local crontab (which is a little more complicated than just copying the line above):
# Reset Heroku database hourly
RBENV_SHIM=/home/klenwell/.rbenv/shims/
RBENV_BIN=/home/klenwell/.rbenv/bin
RAILS_APP=/home/klenwell/projects/my-app-name
HEROKU_LOG=/tmp/heroku-cron.log
0 * * * * export PATH=$RBENV_SHIM:$RBENV_BIN:$PATH; eval "$(rbenv init -)"; cd $RAILS_APP && bundle exec rake heroku:reset_db['my-app-name'] >> $HEROKU_LOG 2>&1
For more info on setting up a cron job to run a rake task with rbenv, see this article.
rake db:schema:load accomplishes what you want, though I've never tried running it on Heroku.
You could also try the database_cleaner gem, which is typically used in test environments.
Try heroku run rake db:setup.
But you will have to run it manually anytime you need to.
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
Ruby on rails + Capistrano + Whenever gem
I executed whenever --update-crontab but still cron job is not getting executed at production server. There are no logs in the log file. Though everything works well at development where capistrano is not required.
schedule.rb
set :output, "../dev/log/cron.log"
every 1.minute do
runner "SOME_TASK"
end
deploy.rb
set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" }
capfile
require "whenever/capistrano"
What's the issue? How to debug?
I've had similar issues when deploying our apps.
If you check the log files of crontab you'll see that it does execute but its being executed in the wrong context.
For example:
You think this code should execute but it doesn't:
every 1.minute do
runner "bundle exec rake db:seed"
end
Instead you should supply the absolute path tot the executable. Cron doesn't know in what kind of context it should be run, it just executes.
We use rbenv in our deployment and we use shims of gems. So I just supplied cron with the absolute path to the executable.
This code does run:
every 1.minute do
runner "/usr/bin/shims/bundle exec rake db:seed"
end
I frequently need to run the command "Rails.cache.clear" on Heroku and the only way I have found to do it is to first run "heroku run console" and then run the command. Any way to do it in one step?
This seems to work:
echo "Rails.cache.clear; exit" | heroku run console
Without the exit it seems to hang for some reason, at least for me.
I think this is what you're looking for:
heroku run rails runner -e production Rails.cache.clear
If you don't set the environment, development will be used.
If it's a common task like clearing the cache, make a rake task.
Create a rake task with the command. For example a file lib/tasks/cache.rake
namespace :cache do
desc 'Clear memcache'
task :clear => :environment do
Rails.cache.clear
end
end
Then you can run the command
heroku run rake cache:clear
I've added gem 'delayed_job' to my gem file and ran a bundle install.
After that I ran rails generate delayed_job
I've created a controller named Online with a method online.
In turn after the method declaration I added the following line:
handle_asynchronously :online
I start up my app, but the code in that method does not run.
What am I doing wrong?
I'd guess that you haven't done rake jobs:work anywhere. From the fine manual:
Running the jobs
You can invoke rake jobs:work which will start working off jobs. You can cancel the rake task with CTRL-C.
You might want to set up Foreman to start the Rails server and the Rake task at the same time in your development environment; there's even a Railscast about it:
http://railscasts.com/episodes/281-foreman