Background jobs
Purge jobs, the job queue port, and what the in-memory driver does not do.
A delete removes the resource row and marks the asset. The bytes are removed later, by a job, and only if no other resource still points at the asset. That is why the server needs a job queue.
Wiring a queue
Section titled “Wiring a queue”import { createMemoryJobQueue } from "@softzweig/suitcase-job-queue";import { createSuitcaseServer } from "@softzweig/suitcase-server";
const server = createSuitcaseServer(definition, { ...createSqliteServerAdapters(db), createJobQueue: (handlers) => createMemoryJobQueue(handlers, { concurrency: 2 }),});The server defines the handlers, including assets.purge and uploads.purge. The queue only says
where they run, so a deployment can point the same handlers at a durable driver.
What a purge does
Section titled “What a purge does”- It rechecks the asset’s mark.
- If another resource points at the asset, it ends without removing anything.
- Otherwise it claims the asset and removes the object from its bucket.
The recheck is what makes shared uploads survive. Two resources can share one asset, and removing one must not remove the bytes under the other.
Upload cleanup
Section titled “Upload cleanup”When an upload finalizes, its bytes are copied into the kind’s bucket and the resource is
committed. By default the server then queues uploads.purge with the object key, and a worker
removes the source object from the upload bucket.
If the upload bucket config sets cleanupUploads: UploadCleanupPolicy.BUCKET_LIFECYCLE, the server
skips the job and expects the bucket’s own lifecycle rule to expire the source object. The job is
idempotent and best effort: a missing object is success, and a deployment without a queue can still
rely on a lifecycle rule.
The in-memory driver
Section titled “The in-memory driver”const jobs = createMemoryJobQueue<SuitcaseJobs>(handlers, { concurrency: 2, onError: (error, job) => logger.error({ error, job }, "job failed"),});| Option | Default | Meaning |
|---|---|---|
concurrency |
4 |
Jobs in flight at once |
attempts |
3 |
Attempts before a job is given up |
retryDelayMs |
1000 |
First retry delay, doubling after each attempt |
onError |
none | Where a job that ran out of attempts goes |
enqueue settles once the queue has accepted the job, not when the job ran. The work outlives the
caller, and a failure is the queue’s to report. onIdle() settles when nothing is waiting and
nothing is running, which is mainly useful in tests.
size(), pending(), isPaused(), pause(), and start() are available too.
What it is not
Section titled “What it is not”- Not durable. A job queued when the process ends is gone.
- Not a scheduler. There is no cron and no delay beyond retry backoff.
- Not a workflow engine. A job is one function with one payload.
A durable queue belongs behind the same JobQueue port. The handlers do not change.