How to Handle Container Job Completion Reliably

Use a completion callback for prompt notification, make its effects safe to repeat, and read the job record when a notification is missing. In altengine Containers, the job result is recorded before the callback runs. A failed callback does not rerun the job, callbacks can arrive more than once, and a finished job can have an unknown exit code. Your application needs to handle all three cases.

Keep the work and its notification separate

Consider an export that produces a file and then tells your application it is ready. There are three outcomes to track: the command ended, the file was saved, and your application recorded that the user can retrieve it. Treating these as one event makes recovery harder. A notification failure should not automatically generate another export, and a successful command should not make a missing file downloadable.

POST /v1/container/{instance} returns a job id once the machine exists. Save that id alongside your application's export request. The launch response does not wait for the work to finish. The background job launch walkthrough covers the initial request; the next design decision is how to turn that id into a trustworthy application result.

Keep the association in durable application storage. It should let you answer which request started the job, which output it should produce, and whether the completion has already been applied. These are application responsibilities, not extra fields the Containers API creates for you.

Understand the callback contract

Set the instance's Call on completion to a function in the form {functions-instance}/{function}. That function receives a POST with x-ae-trigger: container. The documented payload looks like this:

{ "job_id": "j3f9c…", "instance": "6b0e…", "status": "done", "exit_code": 0,
  "image": "ghcr.io/me/transcode:v4", "size": "medium",
  "started_at": 1791561600000, "ended_at": 1791561912000, "cost_usd": 0.0123, "error": null }

The instance value is the container instance's id, not its name. The body is not signed. Check that x-ae-trigger equals container: the platform strips that header from inbound requests, so a caller hitting the function's public URL cannot impersonate this trigger. This is the documented function callback mechanism, not a signature scheme for an arbitrary external webhook receiver.

Any 2xx response counts as delivered. A different response, or no answer within 30 seconds, is retried about once a minute, for five attempts total. An attempt that timed out may still have run. Delivery therefore has both a duplicate risk and a finite retry budget; it is not a promise that your handler eventually succeeds.

Make completion safe to apply twice

Use job_id to recognize a completion you have already handled. Suppose the function saves the export's ready state but its response arrives too late. The next callback should observe the saved result and acknowledge it, without repeating the business action.

A check followed by an unrelated write is insufficient if two handler invocations overlap. Design the storage operation so that recording completion and changing the export's state happen atomically, or make the state transition itself safe to repeat. Only acknowledge after the required durable work succeeds. If you acknowledge first and then fail, the callback mechanism has no reason to deliver another attempt.

External effects need their own treatment. If completion sends a notification through another service, a crash can occur after that service accepts it but before your handler records success. Use a stable deduplication identifier where that service supports one, or explicitly accept and handle possible duplicates. Merely storing a processed flag does not make an external effect happen exactly once.

Do not equate done with a verified export

The job's running status is the only status that changes. Read the terminal result with more care than a single success comparison:

  • done with exit_code: 0: the command exited successfully. Your command should only do that after saving the required output.
  • done with exit_code: null: the machine ended without recording an exit code; error explains that. The command's success is unknown.
  • failed: a nonzero exit, timeout, or failure to start or watch the machine. Inspect exit_code and error.
  • canceled: cancellation through the API or console, or deletion of the instance while the job ran.

For an unknown exit, check the expected output and your application's completion criteria before deciding to rerun. A failed or canceled process may also have performed some work. Repeating it safely depends on the export or import you wrote, not just on the terminal status.

Reconcile when no callback arrives

Use GET /v1/container/{instance}/{id} to recover the recorded result. Because that result is stored before notification, exhausting the callback retries does not lose it or restart the job. Route a result found this way through the same completion logic as a callback, so the two paths cannot independently apply the same business action.

A useful application policy is to periodically inspect requests still waiting for completion, read their saved job ids, and reconcile terminal results. Choose the interval around how long users can reasonably wait and how quickly operators need to know about failures. Polling is an alternative delivery path you implement, not an automatic repair performed by Containers.

There are retention boundaries: finished job records remain for 30 days; running jobs are not removed because of age. A job ended by deleting its instance sends no callback. Preserve the application history you need and account for deliberate deletion in your own operations process, rather than expecting a notification afterward.

Save outputs before the process exits

A job has no disk that survives it. Write the result to Blob, Datastore, or another destination it can reach before exiting successfully. There is no inbound network address where a caller can fetch a file from the job afterward.

Logs are available while the job runs and for about a week afterward. If logs cannot be retrieved, the endpoint returns an empty response with a note; the job record is kept separately. Logs help diagnose an export, but they are not its durable output or a substitute for your application's history.

Retry the failed part, within the launch limits

A crashed job is not restarted automatically. Separate retrying the command from retrying completion processing: if the output is already valid, repair the application state without paying to generate it again.

If a new job is necessary, its launch still faces the instance's image allowlist, runtime limit, concurrency cap and cost ceiling. The default concurrency is two jobs, configurable up to 50; launches beyond it are refused, not queued. The maximum job time defaults to five minutes and can be set up to one hour. A retry strategy must handle those refusals instead of immediately submitting an unlimited stream of replacements.

Before rollout, test a duplicate callback, an unavailable handler, a missing exit code, and a failed output write. Those cases establish whether the application can recover from the gaps between execution, storage and notification. The Containers reference documents the status fields, callback rules and limits used here.

Start free in the console

← Back to the blog