Daemons
A base daemon has 3 methods:
1. initModule()
2. runModule()
3. stopModule(), (*1)
It's located in package jet-snowman\daemon\BaseDaemonController., (*2)
For each method you can add your logic. For example inside initModule can be initialization of some connection and in the method stopModule can be closing connection.
The method runModule() executes in a loop. For development the method runs once and you don't need to stop a daemon every time. When one iteration is done a daemon updates pid file that allows to track a status of the daemon.
A task inside runModule() must be light, for example if an iteration takes 1 minute it means to stop a daemon takes at least 1 minute and in case an exception you will lose your progress so a task must be small and should take less than 5 seconds to finish it., (*3)
If pid file wasn't updated for X time the Watcher will kill and start the daemon again.
The watcher is always running, it monitors all daemons in the server so before to stop your daemon you have to make sure you stopped the Watcher first., (*4)
The configuration of watcher can be found here - /console/controllers/WatcherDaemonController.php, (*5)
protected function getDaemonsList() {
return [
['daemon' => 'metric/metric-license-daemon', 'enabled' => TRUE, 'debug' => TRUE, 'kill' => TRUE, 'maxTime' => 60, 'count' => 2],
['daemon' => 'metric/metric-facility-daemon', 'enabled' => TRUE, 'debug' => TRUE, 'kill' => TRUE, 'maxTime' => 60, 'count' => 3],
['daemon' => 'metric/metric-wordpress-daemon', 'enabled' => TRUE, 'debug' => TRUE, 'kill' => TRUE, 'maxTime' => 60, 'count' => 2],
['daemon' => 'metric/metric-delivery-daemon', 'enabled' => TRUE, 'debug' => TRUE, 'kill' => TRUE, 'maxTime' => 60, 'count' => 1],
['daemon' => 'greenbits/greenbits-product-daemon', 'enabled' => TRUE, 'debug' => TRUE, 'kill' => TRUE, 'maxTime' => 60, 'count' => 1],
];
}
daemon - it's the name of daemon that can be found when you run ./yii, (*6)
enabled - you can enable or disable some daemon, (*7)
debug - if it's enabled then daemon will be ran with debug flag, (*8)
kill - if it's enabled then the watcher will kill a daemon after X time, (*9)
maxTime - the max time between iterations, (*10)
Daemon Bin folder
The patch is ./bin. To create a runnable file you can copy some existed daemon and you need to define the following constants:, (*11)
#!/usr/bin/env php
<?php
define('DAEMON_NAME', 'Metric Delivery Daemon');
define('DAEMON_BIN_NAME', 'metric/metric-delivery-daemon');
define('DAEMON_PID_NAME', 'metric-metric-delivery-daemon');
require_once (__DIR__.'/init.php');
DAEMON_NAME - can be any name, (*12)
DAEMON_BIN_NAME - must be taken from ./yii and the same name must be used in the watcher configuration, (*13)
DAEMON_PID_NAME - can be any name but without slashes, (*14)
Rabbit Base Daemon
The base class is located - jet-snowman\daemon\controllers\RabbitMQBaseDaemonController and you have the following methods, (*15)
- initModule() - you have to set your queue name.
- stopModule()
- getChannel() - you have to return a channel
- handleMessage($message) - yo have to handle your task
The message is an instance of \PhpAmqpLib\Message\AMQPMessage. The handleMessage calls every time when rabbit sends a task to a worker. Only one task can be handled in one iteration., (*16)
Sqs Base Daemon, (*17)
The base class is located - jet-snowman\daemon\controllers\SqsBaseDaemonController and you have the following methods
1. initModule() - you have to set your queue name.
2. handleMessage($message) - yo have to handle your task, (*18)
The message is an instance of Array. The handleMessage calls every time when a worker gets a new response. If response has more than 1 message then it will be called multiple times for one iteration., (*19)