Skip to content
Edit this page View source of this page

Deploy to production

This page shows you how to run a Hanami app that uses Mankai in production. You learn which processes to start, how to stop workers safely, how to size database connections, and how to configure WebSockets and the cache behind a proxy. It assumes that you installed the gems and their SQL backends, as described in Install Mankai.

Run the web and worker processes

An app that uses the jobs SQL backend runs two kinds of processes. The following table lists them:

Process Command Needed for
Web bundle exec hanami server (Puma) Every app; it also serves WebSocket connections
Jobs worker bundle exec hanami jobs start The jobs SQL backend

Run the worker from the same release, with the same environment and settings, as the web process. For example, in a Procfile:

web: bundle exec hanami server
worker: bundle exec hanami jobs start

If you use systemd or containers, run the same two commands as separate services.

The cache and the sockets SQL backend need no extra process. Their background work, such as cache expiry and broadcast polling, runs on threads inside the processes that use them.

Stop a worker gracefully

When a worker receives the TERM or INT signal, it stops taking new jobs. It then waits up to shutdown_timeout seconds (default: 5) for the jobs it's running to finish.

Jobs that are still running after that time go back to the queue, and another worker runs them again from the start. To make this safe, keep jobs idempotent, or make long jobs resumable so that they continue from their last step. For both techniques, see Make jobs reliable.

Set your process manager's stop timeout higher than shutdown_timeout. Otherwise, the process manager kills the worker before it can return its jobs to the queue.

Recover jobs from a lost worker

If a worker's host disappears without stopping the worker, the worker can't return its jobs to the queue. The supervisor, the process that hanami jobs start runs to watch over the workers, notices that the worker stopped and marks its jobs as failed with Mankai::Jobs::ProcessPrunedError or Mankai::Jobs::ProcessExitError. These jobs don't follow the job's retry rules.

To run the jobs again, pass their IDs to retry_failed:

Hanami.app["queue.backend"].retry_failed(ids)

Replace ids with the IDs of the failed jobs.

Run migrations

  1. Before you start a new release, run the migrations, as you would for any other migration:

    bundle exec hanami db migrate
    
  2. After you upgrade mankai-jobs-sql, run the installer in development to add any new migrations, and then commit them:

    bundle exec hanami jobs install
    

    The installer adds only the migrations that your app doesn't have yet.

Size database connection pools

Each process opens its own pool of database connections. Set max_connections in DATABASE_URL for your busiest process:

  • Jobs worker: one connection for each worker thread, plus two. By default (silence_polling: true), the worker polls for jobs through a second pool of the same size, which keeps polling queries out of your logs. Count both pools.
  • Web process with the sockets SQL backend: one connection for each Puma thread, plus one for each executor thread (executor_threads, default: 4), plus one. By default (silence_polling: true), the process also polls for broadcasts through its own connection, so count one more.

To check the total, multiply each process's connection count by the number of those processes, and add the results. Keep the total under your database's connection limit. The gems log a warning at boot when a pool looks too small.

For MySQL and MariaDB, keep fractional_seconds=true in DATABASE_URL in every environment.

Configure WebSockets

  1. Use the SQL backend. With the :memory backend, a broadcast reaches only browsers connected to the process that sent it. Broadcasts from job workers, the console, or another Puma process need mankai-sockets-sql.
  2. Authenticate connections. Remove allow_anonymous, and set an authenticator:

    • Session authentication needs config.actions.sessions with a secret of at least 64 bytes.
    • Token authentication needs token_secret with at least 32 bytes.

    Keep both secrets in your app's settings, not in code. 3. Configure allowed origins. The :same_origin setting compares the Origin header with the request's URL. If a proxy terminates TLS, configure it to pass the X-Forwarded-Proto and Host headers, so that the app sees the public URL. Otherwise, list the origins: config.allowed_origins = ["https://example.com"]. 4. Pass upgrade requests through the proxy. Configure the proxy to forward the Upgrade and Connection headers on the endpoint's path. Allow idle connections that last longer than the heartbeat interval (default: 15 seconds). The server also sends WebSocket pings, so that proxies don't close quiet connections. 5. Plan for connection limits. max_connections_per_identity limits the connections for each user in one process, not across processes. Each open connection uses a file descriptor in the web process, so check the process's open-file limit.

To serve WebSockets from their own processes, see Run and test WebSockets.

Configure the cache

  • If more than one process or server must share entries, use the SQL store. The file store belongs to one machine, and the memory store belongs to one process.
  • Limit the SQL store's table. By default, the store deletes entries older than two weeks (max_age). To also cap the table's size, set max_entries or max_size. For details, see Share the cache with the SQL store.
  • To share one table between apps, give each app its own namespace. Calling clear empties the whole table, including every other app's namespace.

Check before you ship

Before you deploy, confirm the following:

  • hanami db migrate runs before each release.
  • A jobs worker runs next to every web deployment that uses the jobs SQL backend.
  • Workers serve every queue that your jobs use, including mailers if you deliver mail with jobs.
  • Connection pools fit within the database's connection limit.
  • MySQL and MariaDB URLs have fractional_seconds=true.
  • WebSockets use the SQL backend and an authenticator, with origins that match the public URL.
  • The proxy forwards WebSocket upgrade requests.
  • Job failures reach you: a subscriber listens for mankai.jobs.fail events, or the queue provider's error_reporter setting is set.

What's next