hanami / hanami-2-application-template

Hanami 2 application starter template
107 stars 25 forks source link

Things required to deploy to Heroku #53

Open katafrakt opened 3 years ago

katafrakt commented 3 years ago

Recently I have deployed an app from the template to Heroku and I needed to tweak a few things. Listing them here, so we can discuss which of them should make it to the code (I will be happy to prepare PRs, once we decide) - or at least be a checklist for someone else facing similar problems.

  1. Commit Gemfile.lock and yarn.lock currently the are in .gitignore and Heroku does not allow that (I see now it's partly addressed by b38df3aff7e758bd765ccc8035d7887327d950b2)
  2. Serve assets from /public

Currently there's this in config.ru:

use Rack::Static,
  urls: ["/assets"],
  root: ".assets/tmp",
  header_rules: [
    ["/assets", {"Cache-Control" => "public, max-age=31536000"}]
  ]

I had to change to root to root: ENV['PRECOMPILED_ASSETS'] == 'true' ? "public" : ".assets/tmp", so it serves precompiled assets from /public. I'm not really sure what's the deal with .assets directory though, so there might be other solution for this.

  1. Disconnect Sequel databases in Puma before_fork (Sequel::DATABASES.each(&:disconnect)) - I'm not sure here if it's something Hanami.shutdown should do, but for now manual disconnect is required.
JuPlutonic commented 2 years ago

Not only for Heroku (starting November 28th free dynos will no longer be available). So for PaaS/VPS and for 12 factor app ideology all these three issues need to be implemented. So, I solve 3rd issue/"graceful shutdown" with:

# Shutdown the application before forking
before_fork do
  @_app.shutdown if instance_variable_defined?(:@_app)
end