how to start Delayed_Job automatically when the app start ? - ruby-on-rails

When I start my rails server, I was wondering which is the best way to run this command automatically
RAILS_ENV=development script/delayed_job start
so delayed_job daemon also starts ?
Thanks

You could consider using foreman and configure your Procfile lile:
server: rails server
delayed: bundle exec script/delayed_job start
Then all you need to do to start everything is:
foreman start

you can look at foreman which has an amazing railscast to start every service that you want to start when you start your rails server

Related

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

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 setup delayed_jobs in Linux?

I have a remote Ubuntu Linux for testing a Ruby on Rails application. I deployed delayed_jobs gem in the application.
In my local machine, I used rake jobs:work to start the worker process, which will run all delayed jobs automatically.
I would like to start this worker process in Linux and then quit the SSH connection.
What's the best practice in setting up delayed_job in linux? Thanks.
try to use & at the end of your command, in order to start rake as a background process:
rake jobs:work &
Now you can quit SSH
Use nohup rake jobs:work & can fix the problem. Solved :)

Setting up the Delay Jobs gem

I am using Ruby on Rails 3.0.9 and I am trying to setup the delay_job gem for my web application in order to send emails in this way:
Notifier.delay.send_email(#user)
As well as written in the official gem documentation, to start my "delayed jobs" I should use one of the following line of code
$ RAILS_ENV=production script/delayed_job start
$ RAILS_ENV=production script/delayed_job stop
# Runs two workers in separate processes.
$ RAILS_ENV=production script/delayed_job -n 2 start
$ RAILS_ENV=production script/delayed_job stop
or invoke the rake jobs:work task.
In production mode I prefer to use one of the RAILS_ENV=... statements, but I would like to know where (that is, in which file) I should add that code in order to start the workers on application start (BTW: at this time I am not using Capistrano to deploy my application).
More, I would like to know what exactly "workers" are and if my VPS hosting (running Ubuntu 10.04 LTS) can run multiple of those or how to know how many workers my server can run.
Finally, I would like to know what options can I add in the config/initializers/delayed_job.rb file and if there are some advices or tricks about the Delay Job gem.
To start your workers on application start I would just call the proper command from an initalizer. The code to do this would look like:
system "RAILS_ENV=production #{Rails.root.join('script','delayed_job')} stop"
system "RAILS_ENV=production #{Rails.root.join('script','delayed_job')} -n 2 start"
The path might be a little off and there most likely is a cleaner way to do it but I dont know of anything off of the top of my head.

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