ruby-on-railsherokurakeruby-on-rails-6procfile

Automate a rake task to run on boot on heroku?


Suppose there's a task

rake startupscript

that should run whenever the app boots, how can we automate that on heroku?

I know there's a heroku scheduler but that will run the task every 10 minutes instead of just once at boot. I also know of the Procfile and believe this can be a solution, although I do not yet know how to implement (and probably more importantly, I don't want to risk breaking anything else that can be configured via a Procfile, e.g. webserver etc). A lot of the Procfile docs focus on using it to alter web servers rather than app level rake tasks.

How can a rake task be made to run at boot?


Solution

  • You can add something like this to Procfile before you start your application services

    # Run pre-release-tasks here
    release: bundle exec rails db:migrate
    # Then run your application
    web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}
    

    Anything tagged as release will run before the startup script runs

    https://devcenter.heroku.com/articles/release-phase