Save rake background task to log? (Using Resque) - ruby-on-rails

This is very simple I guess, but still..
I have a background task with Resque that is failing and the output is too long to see in Terminal window.. I think it's time to log it. I execute it through
bundle exec env rake resque:work QUEUE='*'
Question is - how do I save that output to log file?
I looked at logging (development.log and it's not showing there of course, b/c it's happening on rake side)...
Thanks!

Take a look at this pull request:
https://github.com/sj26/resque/commit/05e4c5e6f92fe62b25db40984b20dad4b9f870d8
And read the readme. Have you tried to set VVERBOSE=1?
You could just send the output to a file:
bundle exec env rake resque:work QUEUE='*' >> log/resque.log
I run resque like that on my server
nohup bundle exec rake resque:work QUEUE=general PIDFILE=tmp/pids/resque_worker_QUEUE.pid & >> log/resque_worker_QUEUE.log 2>&1
Can't tell you it's the best way, but it works.

Related

Rails 'rake task' execute in ‘RAILS_ENV=production' with nohup

I have rake task which continuously need to be active. Whenever I ran it by command
RAILS_ENV=production rake jobs:abc
it works fine but when I close terminal rake job get stopped.So I found other solution on it by using nohup to run it in background.
I execute command:
nohup RAILS_ENV=production rake jobs:work &
but it gives error:
nohup: failed to run command ‘RAILS_ENV=production’: No such file or directory
Please suggest, to way execute rake task in a production environment.
Set the environment variable before the nohup command.
RAILS_ENV=production nohup rake jobs:work
Try this one
nohup rake jobs:work RAILS_ENV=production
I have commented the solution above as well
If you need nohup functionality, you should also consider screen.
RAILS_ENV=production screen -L rake jobs:work
It starts a new terminal which isn't bound to your current session.
To come back to your normal terminal, type Ctrl+a and then d. You can now log out safely without terminating the rake task.
As a bonus, you automatically get a log file in screenlog.0.
You can come back to your rake process by typing :
screen -r

How can I run a rake task via cron?

I have and rails application and a rake task which I'm going to execute by cron around once in an hour. But the thing is that the task uses rails environment and some classes of my rails application. If I run it as ruby script, I'll have to include all the dependencies it uses and I think it's not possible to do it correctly and in a simple way. So I'll have to run it as a rake task because it'll preserve all the dependencies, right? Then how can I run a rake task from cron?
Note that I prefer not to use any third-party solution when there's no necessity, in this case I don't want to use the gem whenever or the like.
You can add to your crontab something like
0 * * * * /bin/bash -l -c 'cd /path/to/your/project && bundle exec rake foo:bar >> log/cron.log 2>&1'
This will run foo:bar task every hour and write stdout and stderr to log/cron.log.
Please notice bundle exec before rake command.
Using bundler ensure you that task will fetch correct environment.
To specify RAILS_ENV you can do
... && RAILS_ENV=production bundle exec rake foo:bar
Use whenever to simplify your life https://github.com/javan/whenever ;)
Here's an example of a rake task:
task :foo => :environment do
puts "Running rake task in environment: #{Rails.env}"
# can access Models here or whatever
end
Note that the => :environment part is optional, but it what makes your Rails environment to the task block.
You can put rake run_my_task in your cron job.
You may need to use something like cd /home/$USER/my_rails app && rake run_my_task to ensure that the cron runs the task from the Rails root directory.

Running heroku CLI commands (rake db:migrate) from Resque worker

I'm trying to run heroku CLI commands like:
heroku run rake db:migrate --app app-name
heroku run rake db:seed --app app-name
from a Resque worker running in the background.
If I run the worker in the foreground with:
RAILS_ENV=production rake resque:work QUEUE="*"
the process completes successfully, and the rake tasks are run.
However, when the worker is started like so:
RAILS_ENV=production PIDFILE=./resque.pid BACKGROUND=yes QUEUE="*" rake resque:work >> worker1.log
the processes silently fail with no indication of what happened in the logs. Is there a way to run these tasks in the background?
Like max said I'm not sure why you would want to do this, but if you want to invoke a Rake task from a Ruby script, in your case it is better to actually load the Rake task into memory and run it from within your Resque worker. Here is a SO answer that explains how to run a Rake task from Ruby: https://stackoverflow.com/a/15259172/586983

Execute rake command inside bash

I am seeing many times the question about execution of bash files inside rake (task) files.
My question is, how to execute a rake command inside the bash file?
I have a migrate.sh file inside each rails app on my server and I'm using a general publish.sh. All of this runs ok.
Then, I have a command like rake tmp:clear assets:clean log:clear RAILS_ENV=production inside each migrate.sh that gives me a rake: command not found error.
Help?
Basically rake is not resolved as the PATH variable is not correct. You can try doing echo $PATH. Also you can create a bash script and provide some environment variables required by rake like this:
#!/bin/bash
GEM_HOME=/home/tuxdna/.gems
SHELL=/bin/bash
USER=tuxdna
PATH=/home/tuxdna/.gems/bin:/usr/lib/ruby/gems/1.8/bin/:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
GEM_PATH=/home/tuxdna/.gems:/usr/lib/ruby/gems/1.8
cd ~/somesite
export RAILS_ENV=production
bundle exec rake mytask:task1
klashxx's supposition was correct. It was a permissions/profile issue. I had change my user to root to be able to do other previous tasks and found out that my root was not able to run rake tasks.
This will not be an issue on production server though.
Thanks klashxx

How to run "rake resque:work QUEUE=*" when Rails server boots?

I have installed resque correctly, but to process all queues I need to run
rake resque:work QUEUE='*'
The problem is that I need to keep the terminal window opened, otherwise resque:work won't works.
Do you know a way to auto-run that rake command every time I run "rails server" ?
I'm on Localhost
lib/tasks/resque.rake
require 'resque/tasks'
task "resque:setup" => :environment do
ENV['QUEUE'] = "*"
end
Instead of calling the invoke function, you can use a gem like foreman that can invoke all the other tasks.
This is useful if you are looking to have a largely platform neutral solution and also while deploying into cloud.
Your Procfile can have the following contents:
web: bundle exec thin start -p $PORT
worker: bundle exec rake resque:work QUEUE=*
clock: bundle exec rake resque:scheduler
Source:introduction to foreman.
Now to start the server, you just have to issue foreman start command which forks off child threads to perform individual work.
Edit: Answer from 2012! Seems that this works just for Rails 2!
Add an initializer in config/initializers with something like this:
Rake::Task["resque:work QUEUE='*'"].invoke
Not tested!
The best way to do it is
ENV['QUEUE'] = "*"
Rake::Task["resque:work"].invoke

Resources