Snapwake

DocsRunning your app

Background workers

A worker is an app with no HTTP server: it takes jobs from a queue, sends emails, processes uploads. On Snapwake a worker can sleep while its queue is empty and wake when a job arrives, so a worker that is busy a few minutes a day costs a few minutes a day.

Deploying a worker

A worker is an app of its own. If your web app and your worker come from the same code, deploy the folder twice:

Terminal
snapwake deploy . shop --env-file .env.production
snapwake deploy . shop-worker --env-file .env.production \
  --worker "rq worker default" --queue rq:default

--worker "command" makes the app a worker that runs that command instead of a web server. The same works with --image.

Pass the Redis URL (REDIS_URL=...) with --env-file on the first deploy, so the worker has it from its first start. Secrets set later with snapwake secret reach the worker after snapwake redeploy.

Sleeping on a Redis queue

With --queue, the worker sleeps while its queue is empty. Snapwake watches the queue for it from outside the VM and wakes the worker when a job appears.

RQ:

Terminal
snapwake deploy . shop-worker --worker "rq worker default" --queue rq:default

--queue rq:NAME watches RQ's queue NAME and its in-progress keys. This is the setup we have tested: 300 jobs enqueued at random moments, each run exactly once, across two dozen sleeps and wakes, with no change to the RQ code.

Other libraries: name the Redis keys yourself.

Terminal
snapwake deploy . mailer --worker "node worker.js" \
  --queue-list bull:mail:wait --queue-busy bull:mail:active
Flag Meaning
--queue-list KEY a Redis key where new jobs appear; repeat for several (up to 16). A job here wakes the worker.
--queue-busy KEY a key that holds jobs in progress; repeat for several (up to 16). While any of these is not empty, the worker stays awake. Lists, sets, sorted sets and hashes are understood.
--queue-url NAME the secret that holds the Redis URL; default REDIS_URL. This name must be uppercase letters, digits and _, at most 128 characters

Only RQ has been tested so far. For BullMQ, Celery or Sidekiq, check your library's key names and try a few jobs before relying on it.

Your Redis must be reachable from the internet (Upstash, Redis Cloud, your own server with a public address and TLS). Snapwake's servers watch it on your behalf and will not connect to private or internal addresses. Snapwake doesn't offer a hosted Redis yet.

When a worker sleeps

A worker with a queue goes to sleep when all of this has been true for 30 seconds in a row:

  • the queue lists and the in-progress keys are empty;
  • the VM uses less than 10% of a CPU;
  • no network connection of the worker, other than to the queue, is waiting for an answer or receiving data.

A long job that waits on a slow API without using CPU keeps the worker awake, because its connection is waiting for an answer.

Going to sleep is a warm shutdown. Snapwake sends SIGTERM to the worker's processes and waits up to 10 minutes for them to exit. Most queue libraries finish the job they are on and stop taking new ones when they get SIGTERM. Then the VM stops. A job that arrives during the shutdown stays in the queue and wakes the worker again.

Waking

The worker wakes from the snapshot taken at deploy, while it was idle and connected to its queue. The job that woke it is waiting in the queue, and the worker takes it as if it had been running all along.

Your worker's Redis connection survives sleep. Inside a queue worker, REDIS_URL (or the secret named by --queue-url) is replaced by redis://127.0.0.1:16379/<db>. That is a local link: Snapwake's server holds the real connection to your Redis, with its password and TLS, and replays the connection's state after a wake. The worker never sees the password, and the library doesn't notice the sleep. Up to 32 such connections per VM, and up to 16 MB per command or reply.

On our development machine, the time from enqueueing a job to the job starting on a sleeping worker is about 1.4 seconds; most of that is the nested virtualization of that machine, and we expect much less on production servers. Use a queue worker for work that can wait a second.

Always-on workers

Without --queue, a worker runs all the time: it never sleeps, and if its VM stops it is started again. If only the worker's process exits inside the VM, nothing restarts it (When your process exits): run the command under a restart loop. Use this for workers that listen on something Snapwake can't watch (a WebSocket to a third party, a Kafka consumer, a Discord bot). Always-on workers are not available on the Hobby plan, and they are billed for memory all the time.

Settings of a worker

snapwake scale and snapwake health work only on web apps; for a worker they answer "has no web process". Change a worker's memory with snapwake deploy ... --mem N. snapwake status shows a worker's memory line as web None MB; that is a known display bug.

Scheduled jobs

Scheduled (cron) processes run on Snapwake's servers but can't be deployed from the CLI during the beta. Until then, use a scheduler outside Snapwake to call an endpoint of your web app, or enqueue a job for a queue worker.