Run Delayed Job at heroku - ruby-on-rails

I am trying to run DelayedJob at Heroku but for some reason Jobs are not running I have followed this tutorial for setting the delayed job. It is working fine locally, and works fine at production and when I run heroku run rake jobs:work from terminal. But I want to run it in background it should not stick to my terminal.
I have "worker: bundle exec rake jobs:work" in my procfile but still its not working
Thanks
Regards

Related

How to run rake tasks on Heroku if am using Docker?

Right now I have a docker container running a rails 6 app in Heroku. Now I am wondering if there is a way to run some rake task. I have tried:
heroku run bash
heroku run rake -T
heroku run rails console
But all of these commands execute rails server for some reason. Is it possible to run rake tasks if I am using Docker?

Heroku rake command

I'm trying to push my Rails app to Heroku, and I'm at the point where I'm trying to create/migrate the database, but I cannot get the rake command to run. Here's the message I'm getting:
$ heroku run rake db:migrate
Running `rake db:migrate` attached to terminal... up, run.2439
bash: rake: command not found
I spent a lot of time getting Postgres set up on my local machine, and it's working fine (was able to run rake commands without issue, and the app is running locally), but I don't know why I'm getting this error when I try to migrate the heroku database.
Figured it out. Turns out I had an error when I tried to deploy the app to Heroku, so it was never deployed. I didn't realize this because I was trying to push a branch that was not the "master" branch to heroku, thinking it would be fine. I wasn't getting any errors on that push, but that's because heroku won't try to deploy anything other than the "master" branch. Once I pushed the "master" branch, I got a bunch of pre-compile errors. Once those were cleaned up, I the app was deployed properly and I was able to run the rake commands.
Long story short, make sure your app successfully deployed before trying to run rake commands.

If i close my terminal would a rake task started on Heroku continue to run

I have a script which in need to run on my data. I have made a rake task for that. If i start the rake task by using heroku run rake my_task:my_action and after a while my internet disconnects. What would happen. Will the task continue to run as it has been initiated on a remote machine. I think it will continue to run. Any ideas.
Processes started in a one-off dyno (the kind of dyno that is provisioned with heroku run command) run attached to your local terminal and will terminate if your internet disconnects or you cancel the command locally.
To execute a process in a one-off dyno that is not attached to your local terminal, use heroku run:detached:
$ heroku run:detached bundle exec rake my_task:my_action
Running `bundle exec rake my_task:my_action` detached... up, run.7562
Use `heroku logs -p run.7562` to view the output.
To introspect whether the one-off dyno is still running use heroku ps. One-off dynos are named run.X where X is some number.
Guys so after trying and exploring i have found that in normal circumstances it doesnt continue. When the terminal closes pipes breaks and it stops to continue.
You can run your rake in screen to prevent your script/rake from breaking if you get disconnected.
http://www.gnu.org/software/screen/manual/screen.html

Delay Job Worker keeps turning off?

New to Rails and very new to Delayed Jobs.
Got one that's supposed to be triggered after 5 minutes. I finally got it to work so that if I run
rake jobs:work
in my terminal, the job starts up and works correctly. If I CTRL-C and exit that action in my terminal, the delayed job stops working correctly. This is one thing on my local server and another on Heroku, where I have to start up the delayed job using
heroku run rake jobs:work
I looked into the new Heroku toolbelt and downloaded the gem they suggest for worker maintenance, foreman, but when I run "foreman start", I get this error
ERROR: procfile does not exist
I don't know what a procfile is, I'm afraid of breaking things after spending pretty much a day debugging my delayed_jobs actions, and I want to do this right to make sure it works instead of figuring out some hacky fix that breaks down later -- so I figured I should ask this question, however obnoxiously vague it may be.
Should I be using foreman for this? Or workless? (Saw that in another SO question). Where's my procfile? Should I do anything with it?
Thanks,
Sasha
You should be using a procfile to set up your Heroku processes, this is the standard method that Heroku uses to define and control the processes.
If you haven't utilised a procfile to this point everything will probably still work as Heroku adds some default processes when you push a Rails app, including both the web and worker processes. The default worker process is set to delayed job.
Foreman was developed in order to set up your local machine to use the same approach but, unlike the Heroku service, Foreman actually requires a procfile to be present to control the services that are started when Foreman is started as it doesn't know how to setup defaults.
I would suggest creating a procfile, placed in the root directory of your project, to ensure that your processes are set up and operating in the same manner on your local machine as on Heroku. If you want to mimic what Heroku sets up automatically you add the following to the procfile depending on whether you are using the Thin web server (which Heroku recommends) or not.
With Thin in your gemfile:
web: bundle exec thin start -R config.ru -e $RACK_ENV -p $PORT
worker: bundle exec rake jobs:work
Without a special web server (eg you are using webrick, the rails default):
web: bundle exec rails server -p $PORT
worker: bundle exec rake jobs:work
Once this file is in place you can run foreman on your local machine and it will start your web server and delayed_job workers automatically.
Running through this process will only impact starting delayed_job on the local machine. As you are running the exact same command bundle exec rake jobs:work as you are currently using there should be no impact on your dj actions in either locally or on Heroku. Obviously some testing is required to make suer this is actually the case.
Workless is designed to scale workers on Heroku so that you don't have to pay for them when there is no work available. It has no bearing on the procfile or defining how to actually start a dj worker process.
as far as I know, there are 2 version of delayed_job:
original(tobi's) https://github.com/tobi/delayed_job
collectiveidea's fork: https://github.com/collectiveidea/delayed_job
when using the collectiveidea version, you should start it as below:
# Runs two workers in separate processes.
$ RAILS_ENV=production script/delayed_job -n 2 start
I am not familiar with delayed_job on Heroku, please follow its instructions.

How to start delayed job workers in production mode

I was following railscast for delayed job. Things are working perfectly on my machine. How can start delayed_job workers in production mode?
I am using delayed_job gem,(2.1.4)
RAILS_ENV=production script/delayed_job start
For Rails 4
RAILS_ENV=production bin/delayed_job start
Solved my problem.
It may give you an error that tmp directory doesn't exists. Just create one and run previous command again..
You can try to run the following command:
RAILS_ENV=production cd ~/path_to_your_app/current && /usr/local/bin/ruby ./script/delayed_job start
where you should adjust /usr/local/bin/ruby based on your production server ruby configuration.

Resources