Skip to content

push container state changes via docker events - #181

Open
DrDead0 wants to merge 3 commits into
Open-Source-Kigali:developfrom
DrDead0:issue-162-Container-state
Open

push container state changes via docker events#181
DrDead0 wants to merge 3 commits into
Open-Source-Kigali:developfrom
DrDead0:issue-162-Container-state

Conversation

@DrDead0

@DrDead0 DrDead0 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR transitions container state management from a purely poll-based model to an event-driven push model.

Previously, the server forced the agent to run docker ps via socket request on every dashboard poll, which was expensive and resulted in stale data. Now, the Go agent taps into the Docker event stream (type=container), uses a 500ms debouncer to safely batch event storms, and proactively pushes the new container list to the server. The HostsService has been decoupled from AgentsGateway and now instantly serves the /hosts/:id/containers route from its authoritative memory cache, allowing the web dashboard to poll the server cache very cheaply at a 2s interval.

Type of change

  • Bug fix
  • New feature
  • Refactor / cleanup
  • Docs
  • Infrastructure / CI
  • Breaking change

Areas touched

  • apps/server (NestJS)
  • apps/web (React / Vite)
  • apps/agent (Go)
  • packages/protocol (shared WebSocket contracts)
  • infrastructure / .github/workflows
  • docs

Related issues

Closes #162

How to test

  1. Boot up the server, web app, and agent locally.
  2. Open the dashboard and navigate to the container list for your host.
  3. In a separate terminal, manually start or stop a container (e.g., docker run -d --name test-container alpine sleep 30).
  4. Observe the dashboard—the new container state should appear almost instantly (within 2 seconds) without requiring a page refresh, and without triggering a requestContainerList socket round-trip.

Checklist

  • Change is focused and stays within the existing modular monolith boundaries.
  • Ran the relevant checks locally (see below).
  • Updated docs under docs/ if module boundaries or the protocol changed.
  • Added an ADR under docs/decisions/ if this is a larger architectural change.
Local checks
# packages/protocol must be built first — apps/server and apps/web
# import it from its dist output.
npm run build --workspace=@docksight/protocol
npm run test  --workspace=@docksight/protocol

npm run build --workspace=@docksight/server
npm run lint  --workspace=@docksight/server
npm run test  --workspace=@docksight/server

npm run build --workspace=@docksight/web
npm run lint  --workspace=@docksight/web

Copilot AI lite review requested due to automatic review settings September 3, 2026 22:08

Copilot AI 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.

🟡 Changes recommended

The server-side container cache path lacks a clear fallback refresh mechanism and currently reports a misleading “1970” updatedAt before the first real snapshot arrives.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR shifts container state updates from “pull on every UI poll” to “agent pushes updates,” using Docker’s container event stream to keep the server’s in-memory container inventory current and letting the web UI poll the server cache cheaply.

Changes:

  • Web now polls /hosts/:id/containers every 2s instead of 20s.
  • Server /hosts/:id/containers now serves from ContainerInventoryService cache rather than requesting a fresh list from the agent per request.
  • Agent subscribes to Docker container events and debounces bursts to push a refreshed container list to the server.
File summaries
File Description
apps/web/tsconfig.app.json Removes a TypeScript compiler option (ignoreDeprecations).
apps/web/src/hooks/useContainers.ts Reduces refetch interval to 2s to reflect faster server-side cache freshness.
apps/server/src/hosts/hosts.service.ts Decouples container listing from AgentsGateway and serves from in-memory inventory cache.
apps/server/src/hosts/hosts.service.spec.ts Updates unit test wiring after HostsService constructor dependency changes.
apps/agent/internal/docker/service.go Adds a Docker events API wrapper filtered to container lifecycle events.
apps/agent/internal/communication/client.go Consumes Docker events, debounces, and pushes container lists proactively.
Review details

Suppressed comments (1)

apps/server/src/hosts/hosts.service.spec.ts:35

  • The ContainerInventoryService mock in makeService only stubs rememberHost, but HostsService now depends on inventory.getByHostId() for listContainers(). Adding a stub here prevents future tests from failing with a runtime getByHostId is not a function if coverage is expanded.
    return new HostsService(
      agents as AgentsService,
      { rememberHost: jest.fn() } as unknown as ContainerInventoryService,
      {
        rememberHost: jest.fn(),
        getByHostId: jest.fn().mockReturnValue(null),
      } as unknown as HostMetricsService,
  • Files reviewed: 6/6 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

hostId: agent.id,
containers,
containers: snapshot?.containers ?? [],
updatedAt: snapshot?.updatedAt ? snapshot.updatedAt.toISOString() : null,
Comment on lines 105 to 113
this.inventory.rememberHost(agent.id, agent.uuid);

const containers = await this.agentsGateway.requestContainerList(
agent.uuid,
agent.id,
);
const snapshot = this.inventory.getByHostId(agent.id);

return {
hostId: agent.id,
containers,
containers: snapshot?.containers ?? [],
updatedAt: snapshot?.updatedAt ? snapshot.updatedAt.toISOString() : null,
};
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.

Container state is only refreshed by a 20s poll; the agent never pushes Docker events

2 participants