The simple daemon extension for the Yii 2 framework
, (*1)
Author: Inpassor inpassor@yandex.com, (*2)
GitHub repository: https://github.com/Inpassor/yii2-daemon, (*3)
This daemon is a console application of Yii2, implementing multitasking
processes on PHP.
When started, it stays in memory and launches workers.
Every worker process has an individual number of max processes running at once.
Inside the worker you can access any of Yii2 resources., (*4)
Note that for the normal operation of the daemon you need the PHP extensions pcntl
and posix.
If the daemon is running on a Windows system, forking is not available.
Also, the main process of the daemon remains in the console until it is interrupted (Ctrl-C)., (*5)
Installation
1) Add package to your project using composer:, (*6)
composer require inpassor/yii2-daemon
2) Add the daemon command to the console config file in the "controllerMap" section:, (*7)
'controllerMap' => [
...
'daemon' => [
'class' => 'inpassor\daemon\Controller',
'uid' => 'daemon', // The daemon UID. Giving daemons different UIDs makes possible to run several daemons.
'pidDir' => '@runtime/daemon', // PID file directory.
'logsDir' => '@runtime/logs', // Log files directory.
'clearLogs' => false, // Clear log files on start.
'workersMap' => [
'watcher' => [
'class' => 'inpassor\daemon\workers\Watcher',
'active' => true, // If set to false, worker is disabled.
'maxProcesses' => 1, // The number of maximum processes of the daemon worker running at once.
'delay' => 60, // The time, in seconds, the timer should delay in between executions of the daemon worker.
],
...
],
],
],
A workers of the daemon sould be listed in the "workersMap" section. Parameter "class"
is the only one that required. It is possible to set a worker config such way:, (*8)
'workersMap' => [
'watcher' => 'inpassor\daemon\workers\Watcher',
...
],
In this case all the parameters will be taken from a worker class., (*9)
Note that config variables of a worker defined in the "workersMap" config section
of the daemon have priority over the corresponding properties of a worker class., (*10)
The daemon contains the worker "inpassor\daemon\workers\Watcher".
This worker run once per minute and checks if workers of the daemon alive
and removes a dead ones from the memory. It is not required., (*11)
3) Create workers of the daemon. All the worker classes should extend
inpassor\daemon\Worker :, (*12)
class MyWorker extends \inpassor\daemon\Worker
{
public $active = true;
public $maxProcesses = 1;
public $delay = 60;
public function run()
{
$this->log('I live... again!');
// The daemon worker's job goes here.
}
}
The public method run() of the worker should be overridden in the derivative class.
Don't forget to add your workers to the "workersMap" section of the config of the daemon., (*13)
Run as system service for Ubuntu / Debian
1) Make sure that in your project directory there is the console application "yii".
Check if the "yii" file is executable., (*14)
2) Check if the "vendor/inpassor/yii2-daemon/yiid" file is executable., (*15)
3) Run in root console:, (*16)
ln -s /path_to_your_project/vendor/inpassor/yii2-daemon/yiid /etc/init.d/yiid
4) Create the file /lib/systemd/system/yiid.service :, (*17)
[Unit]
Description=yiid
[Service]
User=www-data
PIDFile=/path_to_your_project/runtime/daemon/daemon.pid
Type=forking
KillMode=process
ExecStart=/path_to_your_project/vendor/inpassor/yii2-daemon/yiid start
ExecStop=/path_to_your_project/vendor/inpassor/yii2-daemon/yiid stop
[Install]
WantedBy=multi-user.target
5) Run in a root console:, (*18)
systemctl enable yiid.service
service yiid start