Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a continuous running worker process on Heroku

What is the best way for running a continuous worker process on Heroku? My application needs this process running 24/7, so delayed_job or other queues seem like overkill. I can trigger the process manually through the Heroku console, and I could detach it would run by itself.

Preferably, I'd like to assign just this single method call to a dyno, such that when my app boots, it launches the extra worker-dyno and then calls my single method and starts humming away. But not sure how to assign that method call?

I could use delayed job and it would solve everything, but it seems my need is for a singularly activity that is immediate (when called) and not scheduled. But perhaps this is the only way.

like image 813
Carson Cole Avatar asked Oct 29 '25 11:10

Carson Cole


1 Answers

In your Procfile you could add a seperate process for your extra worker job

# Procfile
web: ...
worker: ...
collector: bundle exec ruby stats_collector.rb -p $PORT # or any other command

You could then scale this process with $ heroku ps:scale collector=1. It would run seperately and be stopped and restarted at every deploy. I'd say that what your app does thus has to be safe to be interrupted any time to use such a long running process.

If interruptions might lead to problem (e.g. stuff you want only done once is done twice if restarted) the a 'traditional' background worker using e.g. Sidekiq that queues messages might be a safer solution.

Update

If you want to execute long-running code non-interactively in the context of your Rails app, you can use the rails runner command.

# Procfile
web: ...
worker: ...
collector: bundle exec rails runner 'MyModel.my_long_running_job'
like image 176
Thomas Klemm Avatar answered Oct 31 '25 01:10

Thomas Klemm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!