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 extendServerWorkerProcess 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.
View all ProcessType constants
View all ProcessType constants
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.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.