refactor(api,runner): make snapshot pull/build async and poll for result#3798
Open
refactor(api,runner): make snapshot pull/build async and poll for result#3798
Conversation
Signed-off-by: MDzaja <mirkodzaja0@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR refactors snapshot pull and build operations to be asynchronous, preventing timeout failures for large Docker images that exceed the 1-hour Axios timeout. The runner now returns HTTP 202 Accepted immediately and processes operations in background goroutines, while the API polls for completion using getSnapshotInfo.
Changes:
- Runner-side
PullSnapshotandBuildSnapshotendpoints launch Docker operations asynchronously and return 202 immediately, storing errors in an in-memory concurrent map - API polling logic uses
getSnapshotInfoto detect completion (200), failure (422 with error reason), or in-progress (404) states - Sandbox start action implements 1-hour polling with 5-second intervals instead of 10 retries with connection reset checks
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| apps/runner/pkg/runner/runner.go | Added SnapshotErrors concurrent map field to store async operation errors |
| libs/common-go/pkg/errors/http.go | Added UnprocessableEntityError type for 422 status code handling |
| libs/common-go/pkg/errors/middleware.go | Added middleware case for UnprocessableEntityError with 422 response |
| apps/runner/pkg/api/controllers/snapshot.go | Made PullSnapshot and BuildSnapshot async with goroutines; GetSnapshotInfo returns 422 for failed operations; error cleanup on snapshot removal |
| apps/runner/pkg/api/docs/swagger.yaml | Updated API documentation to reflect async behavior and 202 responses |
| apps/runner/pkg/api/docs/swagger.json | Updated API documentation (JSON format) |
| apps/runner/pkg/api/docs/docs.go | Updated embedded API documentation |
| apps/api/src/sandbox/errors/snapshot-state-error.ts | New error class for snapshot operation failures |
| apps/api/src/sandbox/errors/runner-api-error.ts | New error class preserving HTTP status codes from runner API |
| apps/api/src/sandbox/runner-adapter/runnerAdapter.v0.ts | Added error interceptor to preserve status codes; getSnapshotInfo converts 422 to SnapshotStateError |
| apps/api/src/sandbox/runner-adapter/runnerAdapter.v2.ts | Throws SnapshotStateError for failed jobs with error message |
| apps/api/src/sandbox/managers/snapshot.manager.ts | Replaced snapshotExists with getSnapshotInfo for polling; handles SnapshotStateError by setting runner state to ERROR; removed retry logic for ECONNRESET |
| apps/api/src/sandbox/managers/sandbox-actions/sandbox-start.action.ts | Implements 1-hour polling with 5-second intervals for pull/build operations |
| libs/runner-api-client/src/api/snapshots-api.ts | Updated generated documentation for async endpoints |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…use runner context for pull/build goroutines for graceful shutdown Signed-off-by: MDzaja <mirkodzaja0@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Snapshot pull and build operations on the runner were synchronous — the API waited for the full Docker operation to complete before getting a response. These operations can exceed the 1-hour Axios timeout, causing failures for large snapshots.
Solution
Runner-side
POST /snapshots/pullandPOST /snapshots/buildendpoints now launch the Docker operation in a background goroutine and return202 Acceptedimmediately. Failed operations store the error reason in an in-memory concurrent map (cmap) on the runner instance.The API polls for completion by calling
getSnapshotInfowhich returns:Changes
snapshot.go):PullSnapshotandBuildSnapshotrun asynchronously via goroutines withcontext.Background(). Errors are stored inrunner.SnapshotErrors.GetSnapshotInforeturns 422 when a stored error is found.runner.go): AddedSnapshotErrorsfield usingcmap.ConcurrentMap[string, string]for thread-safe error storage.http.go,middleware.go): AddedUnprocessableEntityErrortype with 422 status handling in the error middleware.runnerAdapter.v0.ts): Axios error interceptor now throwsRunnerApiErrorpreservingstatusCodeandcode, enabling callers to inspect HTTP status.runnerAdapter.v0.ts,runnerAdapter.v2.ts):getSnapshotInfoconverts 422 responses intoSnapshotStateError. V2 adapter throwsSnapshotStateErrorfor failed jobs.snapshot.manager.ts): Polling handlers usegetSnapshotInfoinstead ofsnapshotExiststo detect both completion and failure.SnapshotStateErrorsets runner state toERRORwith the reason.sandbox-start.action.ts): Pull/build polling uses a 1-hour timeout with 5-second intervals instead of 10 retries withECONNRESETchecks.