Skip to main content
A Daemon Thread in Winter Boot is a long-running background process that starts with your application and keeps running for its entire lifetime. Despite the name, it is not an OS thread or a Java-style thread — it is a dedicated Swoole child process supervised by the framework. Daemon threads are the right tool for work that must always be running: polling a queue, tailing a log stream, monitoring external systems, or processing rows from a database table in a continuous loop. The framework automatically restarts a daemon if it exits unexpectedly due to an out-of-memory error or an unhandled exception, so you never need to wire up your own process supervisor for these use cases.
Daemon threads require the Swoole PHP extension. Install it with pecl install swoole and add extension=swoole.so to your php.ini before using #[DaemonThread].

The #[DaemonThread] Annotation

Apply #[DaemonThread] to a class that extends ServerWorkerProcess. The annotation accepts two optional parameters:
string
default:"Class name"
A human-readable label for the daemon process, used in logs and process tables.
int
default:"1"
Number of daemon process instances to start. Set to 0 (or any negative value) to disable the daemon entirely — useful for feature-flagging in different environments.
SomeBackendProcessing.php

Implementing a Daemon Thread

Every daemon class must extend ServerWorkerProcess and implement three members:

ProcessType Constants

getProcessType() should return one of the constants defined on the ProcessType interface. For custom daemon threads, use ProcessType::OTHER unless you are extending a built-in framework process type.

Complete Example

The following example polls a database table for pending rows, processes each one, and then sleeps for five seconds before repeating.
SomeBackendProcessing.php

Dependency Injection Inside Daemon Threads

The #[Autowired] annotation works inside daemon thread classes just as it does in regular service beans. The framework performs property injection before calling run(), so any container-managed bean — database templates, configuration properties, other services — is available as soon as your loop starts.
OrderProcessingDaemon.php

Automatic Restart on Failure

The Winter Boot framework monitors every registered daemon process. If a daemon exits — whether due to an uncaught exception, an out-of-memory error, or any other crash — the framework automatically restarts it. You do not need an external process manager (like Supervisor or systemd) to keep daemons alive.
Daemon threads run for the entire lifetime of the application. They start when the application boots and are only stopped when the application itself shuts down. There is no mechanism to start or stop individual daemons dynamically at runtime.

Common Use Cases

Monitoring & Alerting

Continuously poll metrics, health endpoints, or infrastructure telemetry. Fire alerts when thresholds are exceeded without adding latency to user-facing requests.

Stream Processing

Consume messages from a queue, Kafka topic, or event stream in a tight loop. Process and acknowledge messages entirely outside the HTTP worker pool.

Background Jobs

Pick up pending database rows, trigger batch reports, or run data-migration steps continuously in the background without blocking API responses.

Supervision & Coordination

Act as a coordinator for other subsystems — reaping stale sessions, refreshing distributed caches, or enforcing rate limits across worker processes.