PHPackages                             seankndy/daemon - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. seankndy/daemon

ActiveLibrary

seankndy/daemon
===============

PHP package for creating and managing daemons

v1.1.0(1y ago)025PHPPHP ^8.1

Since May 27Pushed 1y ago1 watchersCompare

[ Source](https://github.com/seankndy/Daemon)[ Packagist](https://packagist.org/packages/seankndy/daemon)[ RSS](/packages/seankndy-daemon/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (3)Versions (4)Used By (0)

Installing
----------

[](#installing)

```
$ composer require seankndy/daemon
```

Usage overview
--------------

[](#usage-overview)

On each iteration of the main loop, *producers* are called which should provide a *task* to fork/run.

Producers can be callables or objects that implement `SeanKndy\Daemon\Tasks\Producer`. Producers should return `null`if there is no task to perform otherwise a `SeanKndy\Daemon\Tasks\Task` or `callable`. `SeanKndy\Daemon\Tasks\Task`has 3 methods:

`init()` - Called from parent process just before child is forked.

`run()` - Called from within forked process (child work)

`finish(int $status)` - Called from parent when process exits

So if you need to perform any initialization or teardown of as children are spawned/exiting, then you'll want to make your own task classes implementing `SeanKndy\Daemon\Tasks\Task` rather than using a simple closure.

There are various events you can listen for (use `SeanKndy\Daemon\Daemon::addListener()`):

`SeanKndy\Daemon\DaemonEvent::START` - When daemon starts

`SeanKndy\Daemon\DaemonEvent::STOP` - When daemon stops

`SeanKndy\Daemon\DaemonEvent::DAEMONIZED` - When daemonized (backgrounded)

`SeanKndy\Daemon\DaemonEvent::LOOP_ITERATION` - Called at the end of each loop iteration

`SeanKndy\Daemon\Processes\Event::START` - New process started

`SeanKndy\Daemon\Processes\Event::EXIT` - New process exited

`SeanKndy\Daemon\Processes\Event::ITERATION` - Every main loop iteration this is fired for each running process

```
