Skip to content

feat: add support for running mocks in detached/background mode#125

Merged
outofcoffee merged 6 commits into
mainfrom
claude/add-detach-option-AP6Kn
May 21, 2026
Merged

feat: add support for running mocks in detached/background mode#125
outofcoffee merged 6 commits into
mainfrom
claude/add-detach-option-AP6Kn

Conversation

@outofcoffee
Copy link
Copy Markdown
Contributor

@outofcoffee outofcoffee commented May 21, 2026

Summary

This PR adds the ability to run Imposter mocks in the background (detached mode) via new --detach and --log-file flags on the up command. When detached, the CLI returns control to the terminal while the mock continues running as a managed process.

Key Changes

  • New CLI flags: Added --detach (with modes healthy and now) and --log-file options to the up command

    • healthy mode (default): waits for the healthcheck to pass before returning
    • now mode: returns immediately without waiting for health
    • Auto-restart is automatically disabled when detaching (incompatible with background operation)
  • Detach mode API: Introduced DetachMode enum and related types in engine/api.go

    • StartOptions now includes Detach and DetachLog fields
    • Added IsDetached() helper and DefaultDetachLogPath() function
  • Process engine support: Updated JVM and Golang engines to handle detached startup

    • Redirects stdout/stderr to log files instead of console
    • Uses platform-specific SysProcAttr to detach child processes from parent process group
    • Returns immediately without waiting for process completion in detached modes
  • Docker engine support: Updated Docker engine to skip log streaming and process reaping in detached modes

    • Container continues running in dockerd independently
  • Process utilities: Added new procutil package with platform-specific detach helpers

    • log.go: Opens log files with append mode to preserve prior output
    • detach_unix.go: Sets Setsid for Unix-like systems
    • detach_windows.go: Uses CREATE_NEW_PROCESS_GROUP and DETACHED_PROCESS flags
  • Engine interface: Added GetID() method to MockEngine interface to expose container/process identifiers

  • Comprehensive testing: Added unit tests and integration tests

    • cmd/up_test.go: Tests for applyDetachOptions() function
    • engine/detach_test.go: Tests for detach mode helpers
    • engine/procutil/log_test.go: Tests for log file handling
    • Engine-specific detached startup tests for JVM and Golang engines
    • New StartDetached() test helper in enginetests/common.go

Notable Implementation Details

  • Auto-restart is incompatible with detaching because the config-dir watcher lives in the foreground CLI process, which exits once the mock is backgrounded
  • For DetachHealthy mode, Ctrl+C during the healthcheck wait will abort and stop the mock; DetachNow returns immediately so there's nothing to interrupt
  • Log files are created with append mode to support mock restarts without losing prior output
  • Docker engine ignores --log-file flag (uses docker logs instead)
  • Process engines resolve relative log paths to absolute paths and create parent directories as needed

@outofcoffee outofcoffee added the enhancement New feature or request label May 21, 2026
claude added 3 commits May 21, 2026 12:47
Adds `--detach`/`-d` to background a running mock and return the
terminal. By default it waits for the healthcheck to pass before
detaching; `--no-await` returns immediately. Detached process-engine
logs go to ~/.imposter/logs/imposter-<port>.log (overridable with
`--log-file`). Auto-restart is disabled when detaching since the
config-dir watcher lives in the foreground process.

Process engines are started in their own session so they survive the
CLI exiting; docker containers already run independently in dockerd.
Existing `imposter ls` / `imposter down` rediscovery is unchanged.

https://claude.ai/code/session_0176yoDmpbo3wwm7i4z4BfPY
Replace the separate --detach/--no-await booleans with a single
--detach=MODE string flag (NoOptDefVal "healthy"), accepting
'healthy' (default) and 'now'.

https://claude.ai/code/session_0176yoDmpbo3wwm7i4z4BfPY
The detach test asserted ListAllManaged returned exactly one mock and
StopAllManaged returned exactly one. On CI runners with a system
imposter installed at /opt/imposter, the JVM matcher also picks up
that process, so the count is 2. Verify instead that our mock (by
port) is in the list and that at least one mock was stopped.

https://claude.ai/code/session_0176yoDmpbo3wwm7i4z4BfPY
@outofcoffee outofcoffee force-pushed the claude/add-detach-option-AP6Kn branch from 81efc47 to beca4bb Compare May 21, 2026 12:49
@outofcoffee outofcoffee changed the title Add support for running mocks in detached/background mode feat: add support for running mocks in detached/background mode May 21, 2026
claude and others added 3 commits May 21, 2026 13:13
GetID() returned the full 64-char container ID, which made the
backgrounded-mock log line in `imposter up --detach` unwieldy. Match
the 12-char short ID that `imposter ls` already uses for docker
containers; docker accepts the short form for rm/logs/etc.

https://claude.ai/code/session_0176yoDmpbo3wwm7i4z4BfPY
`imposter ls`:
- list mocks across all engine types by default (was opt-in via -a/--all)
- `-t/--engine-type` filters to a single engine
- drop the `-a/--all` flag and its mutual exclusivity rule

`imposter down`:
- accept a single mock ID as a positional argument (matching the ID
  shown by `imposter ls`); an unknown ID is fatal
- `-a/--all` stops every managed mock across all engine types
- the bare `imposter down` form is now an error
- drop `-t/--engine-type` — IDs identify the engine implicitly

To support per-ID stopping, add `StopManaged(id) (bool, error)` to the
MockEngine interface. Docker resolves via ContainerInspect and checks
the imposter-managed label before removing. Process engines (jvm,
native) use a new `procutil.StopManagedProcess` helper that kills only
the PID matching both the id and the engine's matcher.

https://claude.ai/code/session_0176yoDmpbo3wwm7i4z4BfPY
The previous down/ls rewrite touched the table for those rows; flag
the new background mode in the `imposter up` row to keep the front
page in line with the branch's user-visible changes.
@outofcoffee outofcoffee merged commit f7ae439 into main May 21, 2026
8 checks passed
@outofcoffee outofcoffee deleted the claude/add-detach-option-AP6Kn branch May 21, 2026 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants