Download the PHP package consik/yii2-daemons without Composer
On this page you can find all versions of the php package consik/yii2-daemons. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package yii2-daemons
Yii2 daemons by systemd
Introduction
There is no concrete realization for daemonizing PHP scripts by using pcntl_fork() or something else. We don't need it, when we have package like systemd that can make service from our PHP script. So, there is DaemonInterface in package that has only two methods startDaemon() and stopDaemon(), and one AbstractLoopDaemon that implements this interface.
You can use your own script, that working in background, just implement there DaemonInterface.
All functions that makes daemon from your PHP script will do systemd and ServiceController.
Installation
The preferred way to install this extension is through composer.
Either run
or add
Creating daemon.
As I say above all that you need is to implement DaemonInterface.
Simple looped daemon:
In this realizaton stopDaemon() never called and we lost result when daemon will be terminated;
Now let's make same daemon by extending AbstractLoopDaemon with extensions from this package as ServiceConfigInterface and LoopDaemonSignalsBehavior.
This daemon increment $i each 10 seconds by 1. Also there used LoopDaemonSignalsBehavior and we handle SIGTERM signal, that dispatch 'stopDaemon' method. When this daemon stopped it will output current value of $i.
For more information about handling signals see docs about pcntl_signal() For easy realization you can use HandleSignalsBehavior or LoopDaemonSignalsBehavior if you extends AbstractLoopDaemon. If you will use HandleSignalsBehavior don't forget calling or pcntl_signal_dispatch().
ServiceConfigInterface has one method getServiceConfig(). This method will be used by ServiceController for getting current daemon systemd unit params.
Using ServiceController
Use ServiceController in your app for easy making systemd units and controlling your daemons.
config\console.php:
Generating systemd units.
For generating systemd unit files use this command:
Registering service.
ServiceController can only generate systemd unit(*.service) file by action systemd-file There is a bash script in package for fast registering systemd units in system. Using example, after service controller is configured:
Controlling your daemon:
Checking your daemon status:
Starting service:
Restarting service
Stopping service:
also you can use ServiceController methods like status
and stop
, but better use systemctl functions:
Checking status by ServiceController:
Stopping daemon by sending SIGTERM signal for daemon process:
@see for more information about unit files configuration and controlling your daemons.
Configuring systemd units
There are three sources with params that used for generating systemd unit. Array structure for all sources below is:
It equals service unit file:
See available options in official docs
All of configuration sources below sorted by priority:
Concrete daemon service configuration. Override common services configuration.
It will be used if the daemon implements ServiceConfigInterface. ServiceController calls method for getting configuration. Example setting systemd unit params for each daemon
Implement ServiceConfigInterface in your daemon and declare function
Defining your daemon config params:
-
Easy way is to declare public variable, that you can change in controller daemons definition and return this variable in getServiceConfig();
Daemon code:
Yii2 console config file:
- Or implement your own function getServiceConfig in the daemon that will returns config params
Common services configuration. Override basic service configuration. Var ServiceController::$commonServiceConfig.
It can be changed in your controller configuration. Yii2 console config file:
Basic service configuration. Lowest priority params. Returns by method .
This method returns basic systemd unit configuration for all daemons(Service: ExecStart, Type; Unit: Description, After; Install: WantedBy).
As default all generated services starts after mysql.service. Override param After
in section Unit
if you don't need it or if your daemon have other dependencies(mongodb.service for example).
All of these configurations will be merged by array_replace_recursive() before generating each daemon config file;