Running heroku CLI commands (rake db:migrate) from Resque worker - ruby-on-rails

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

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 to start Delayed Job with Ubuntu?

I use Delayed Job as a queuing backend for Active Job on my Rails 5 app but I have no idea how to start the worker on Ubuntu 14.04 after startup. Should I wrap rails jobs:work into a Bash script? How would I have it start automatically? Or is it preferable to use bin/delayed_job?
How do I start delayed job on boot?
It does not really matter what OS you're on (as long it is not Windows :D).
To start the processing the command is:
bundle exec rake jobs:work
to restart the delayed_job the command is:
RAILS_ENV=production script/delayed_job restart
Check out gems README for more info.
EDIT
(according to the comment)
You can create some bash script in user's home start_delayed_jon.sh.
Something along the lines:
#!/bin/bash
cd /path/to/your/project/directory/
RAILS_ENV=development bundle exec rake jobs:work
and run it in /etc/rc.local:
su -s /bin/bash - deploy /path/to/your/project/directory/start_delayed_jon.sh
Using the Whenever Gem, you can setup a cronjob that runs it on reboot. In your schedule.rb file:
every :reboot do
rake 'start_delayed_jobs'
end
Then in your rake file:
desc 'Start delayed jobs'
task :start_delayed_jobs do
system("#{Rails.root}/bin/delayed_job start")
end
end
If you are using gem 'delayed_job_active_record'.
You start a delayed jobs on your local ubuntu system, simply run the below command to start
./bin/delayed_job start
and to restart
./bin/delayed_job restart
If we are in development mode, we would use the below rake task instead.
bundle exec rake jobs:work
for production:
RAILS_ENV=production script/delayed_job -n2 restart
or
RAILS_ENV=production bin/delayed_job -n2 restart
n2 is the number of delayed jobs servers you want to restart in case of start use command start instead or restart.
documentation: https://github.com/collectiveidea/delayed_job#restarting-delayed_job

Heroku worker dyno keeps crashing cannot start with Resque

My Heroku worker keeps on crashing whenver I do restart it crashes within 5 seconds.
Below is my Procfile:
resque: env TERM_CHILD=1 bundle exec rake resque:work
resque: env TERM_CHILD=1 RESQUE_TERM_TIMEOUT=7 bundle exec rake resque:work
I'm trying to get Resque work on the system but also unsure that if Resque process even started on the server. Locally I would do:
rake resque:work QUEUE=* and this work work perfectly.
Is there a reason why my Worker Dyno keeps on crashing also a way to check if resque is running successfully?
You can type:
heroku ps
Which will tell you what processes are running at any given time.
heroku logs
That will obviously give you detailed information about what's happening. Copy in your log file and I bet someone will be able to fix it for you. My guess is you're seeing a does not exist error in there somewhere, which is something I ran into :)

Jobs with Resque gives "Don't know how to build task 'jobs:work'" on Heroku

I followed the tutorial at https://devcenter.heroku.com/articles/queuing-ruby-resque to queue and run background jobs in a Rails app. After queueing the jobs, it doesn't seem to run any of the jobs since in the console I can see the job has not been processed
>Resque.info
=> {:pending=>1, :processed=>0, :queues=>1, :workers=>0, :working=>0, :failed=>0, :servers=>["redis://dory.redistogo.com:9826/0"], :environment=>"production"}
If I try to do (locally)
bundle exec rake jobs:work
I get
rake aborted!
Don't know how to build task 'jobs:work'
On heroku, if I try
heroku run rake jobs:work
I again get `Don't know how to build task'
In my Rakefile, I have require 'resque/tasks' and in my Procfile I have
resque: env TERM_CHILD=1 bundle exec rake jobs:work
resque: env TERM_CHILD=1 bundle exec rake jobs:work
I have the Resque and redis gems in my Gemfile, but not delayed_job.
Update:
Here is my Rakefile:
#!/usr/bin/env rake
require File.expand_path('../config/application', __FILE__)
Guard::Application.load_tasks
/lib/tasks is empty. I have a worker in app/workers that I am enqueueing in a controller.
This doesn't appear to have anything to do with Heroku, if it doesn't work locally. You'll have to reveal some of your source to help people help you. For example, what's your Rakefile look like? The demo app in that article defines one with a Rake task with the task. Have you defined your rake task and added the appropriate gem references?
Try resque:work instead of jobs:work and see if that beings the desired results.
You just need to add require 'resque/tasks' in your Rakefile, then on the command line:
QUEUE=file_serve rake environment resque:work
Where file_serve is the name of your job (the class would be FileServeJob).
See the docs here - I found them slightly confusing since the setup and run info comes after the job class creation.

Save rake background task to log? (Using Resque)

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.

Resources