Snapwake

DocsRunning your app

Runtime helpers for Node.js and Python

Most apps need nothing from this page: Snapwake fixes clocks, randomness and network addresses on every wake by itself. The helpers are for the few things only your app can do after a wake, such as reconnecting a pool that doesn't reconnect on its own, or refreshing a token that expired while the app slept.

Both helpers have no dependencies and do nothing outside Snapwake.

Node.js: @flare/runtime

JavaScript
import { onRestore, secret, secrets, generation } from "@flare/runtime";

onRestore(async () => {
  await pool.end();          // drop connections that belonged to the snapshot
  pool = createPool();
});

const apiKey = secret("API_KEY");
Function What it does
onRestore(fn) runs fn after every wake, before the request that woke the app is handled. fn may be async. Snapwake waits up to 1 second for all hooks, then lets the request through anyway. An error in a hook is logged and doesn't stop the others.
secret(name) the current value of a secret, read from /run/flare/secrets.env (rewritten on every wake), or undefined. The file doesn't exist yet on a VM that booted fresh, such as the first VM after a deploy: write secret("X") ?? process.env.X (see Secrets)
secrets() all secrets as an object
generation() a number that changes on every wake; 0 before the first wake and outside Snapwake

Availability. In apps built from a folder by framework detection, @flare/runtime is installed into node_modules for you. It is not yet published to npm, so apps built from a Dockerfile or an image can't import it, and neither can your local machine. Until it is published, load it defensively:

JavaScript
let onRestore = () => {};
try {
  ({ onRestore } = await import("@flare/runtime"));
} catch {}

Apps built from a Dockerfile or image still get the automatic reseeding described in Sleep and wake if the image has Node.js 18.18 or newer. They can read secrets from the file /run/flare/secrets.env directly (the path is also in FLARE_SECRETS).

Python: flare

Python
from flare import on_restore, secret, secrets, generation

@on_restore
def reconnect():
    db.dispose()             # SQLAlchemy: forget connections from the snapshot

api_key = secret("API_KEY", default=None)
Function What it does
@on_restore registers a function to run after every wake, before the request that woke the app is handled. Hooks run on a background thread; Snapwake waits up to 1 second for them.
secret(name, default=None) the current value of a secret from /run/flare/secrets.env; on a freshly booted VM the file doesn't exist yet, so pass default=os.environ.get(name)
secrets() all secrets as a dict
generation() a number that changes on every wake; 0 before the first wake and outside Snapwake

Availability. The flare module is on the PYTHONPATH of every framework-built Python app, and of every image or Dockerfile build that contains python3. It is not on PyPI yet; for local development:

Python
try:
    from flare import on_restore
except ImportError:
    def on_restore(fn):
        return fn

When you need a hook

Situation Hook
A connection pool that doesn't notice dead connections and hangs on them close the pool and create a new one
A token or session that expires (OAuth access tokens, signed URLs, short-lived credentials) refresh it
Your own random generator created before the snapshot (random.Random(), numpy's default_rng(), a seeded Math.random replacement) reseed it, or create it inside the request instead
A cache that must not outlive a deploy-time value (feature flags, config fetched at start) clear it or fetch again
Timers or intervals that should have fired while the app slept run the missed work

Most database drivers (pg, Prisma, SQLAlchemy with pool_pre_ping, Django) reconnect on their own: Snapwake closes the snapshot's connections on wake, so the driver sees a closed socket instead of hanging.

Other languages: the wake protocol

The helpers are thin wrappers around two files you can use from any language:

  • /run/flare/secrets.env (path in FLARE_SECRETS): current secrets as KEY=VALUE lines, rewritten on every wake.
  • /run/flare/events.sock (path in FLARE_EVENTS): a Unix socket. Connect, write sync\n, and keep the connection open. After every wake you receive restored <generation>\n. Do your work, then write ack <generation>\n. Snapwake holds the request that woke the app until every sync subscriber has acknowledged, or 1 second has passed. Connect without writing sync to be told about wakes without holding requests.
  • /run/flare/generation: the current wake number as text (it is the wake's time in Unix milliseconds).

There is no notification before sleep. A sleeping web app is frozen and its VM stopped; shutdown handlers (SIGTERM, process.on("exit"), atexit) do not run. Don't rely on them to flush data: write it before you answer the request. Queue workers are the exception and do get SIGTERM (see Background workers).