feat(agent): Run deployment and service actions as streamed jobs by nfebe · Pull Request #163 · flatrun/agent · GitHub
Skip to content

feat(agent): Run deployment and service actions as streamed jobs#163

Merged
nfebe merged 1 commit into
mainfrom
feat/150/streamed-deployment-actions
Jun 29, 2026
Merged

feat(agent): Run deployment and service actions as streamed jobs#163
nfebe merged 1 commit into
mainfrom
feat/150/streamed-deployment-actions

Conversation

@nfebe

@nfebe nfebe commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Closes #150.

Start/stop/restart/rebuild for a whole deployment, and for a single service, were synchronous: compose output came back only on completion, so the UI could show nothing but a spinner and the result was lost on reload.

These actions now run as background jobs. The endpoint returns a job id immediately, compose output streams line by line over a websocket (authenticated with the same first-message handshake as the terminal endpoints), and the output is buffered so the job status stays queryable and survives a page reload. One action runs at a time per deployment, and per service within a deployment, so different services can act concurrently while the same target is rejected. Finished jobs are kept briefly in memory and are not retained across an agent restart, which the page-reload requirement does not need.

Service actions go through a single POST .../services/:service/job taking the action in the body, rather than a route per action.

Concurrency was checked under go test -race for the job and streaming paths; the e2e helpers now enqueue and poll to keep their existing synchronous behaviour.

Start, stop, restart and rebuild for both whole deployments and single
services previously ran as one synchronous request whose compose output
was returned only on completion, so the UI could show nothing but a
spinner and the result was lost on a page reload.

These actions now run as background jobs that return a job id
immediately. Compose output streams line by line over a websocket and is
also buffered so the status stays queryable, letting the result survive a
reload. One action runs at a time per deployment, and per service within
a deployment, so two different services can act concurrently while the
same target cannot. Finished jobs are kept briefly in memory and are not
retained across an agent restart.
@sourceant

sourceant Bot commented Jun 29, 2026

Copy link
Copy Markdown

@sourceant sourceant Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review complete. See the overview comment for a summary.

Comment thread internal/api/jobs.go
func (j *ActionJob) appendLine(line string) {
j.mu.Lock()
j.lines = append(j.lines, line)
for _, c := range j.subscribers {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a subscriber channel is full, the line is dropped silently. While acceptable for 'live' updates, it might be safer to use a slightly larger buffer or a non-blocking check with a counter to log if users are consistently falling behind.

Suggested change
for _, c := range j.subscribers {
+ for _, c := range j.subscribers {
+ select {
+ case c <- line:
+ default:
+ }
+ }

Comment on lines +357 to +362
scan := func(r io.Reader) {
defer wg.Done()
sc := bufio.NewScanner(r)
// Image pull/build progress lines can be long; raise the per-line cap.
sc.Buffer(make([]byte, 0, 64*1024), 1024*1024)
for sc.Scan() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a single scanner for both stdout and stderr with a shared mutex to update the combined buffer is correct, but there is a risk of a very long line (e.g., binary output or large build logs) causing the scanner to fail. The buffer increase to 1MB is good, but sc.Err() should be checked after the loop to detect if a line was skipped because it exceeded the buffer.

Suggested change

@nfebe nfebe merged commit 459e3f3 into main Jun 29, 2026
5 checks passed
@nfebe nfebe deleted the feat/150/streamed-deployment-actions branch June 29, 2026 15:34
@nfebe nfebe mentioned this pull request Jun 29, 2026
23 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Stream start/stop progress and run deployment actions as jobs

1 participant