comodojo /
daemon
| 1 | <?php namespace Comodojo\Daemon; |
||
| 2 | |||
| 3 | use \Comodojo\Daemon\Events\PosixEvent; |
||
| 4 | use \Comodojo\Daemon\Utils\ProcessTools; |
||
| 5 | use \Comodojo\Daemon\Utils\Checks; |
||
| 6 | use \Comodojo\Daemon\Utils\PosixSignals; |
||
| 7 | use \Comodojo\Daemon\Traits\PidTrait; |
||
| 8 | use \Comodojo\Daemon\Traits\SignalsTrait; |
||
| 9 | use \Comodojo\Foundation\Events\Manager as EventsManager; |
||
| 10 | use \Comodojo\Foundation\Events\EventsTrait; |
||
| 11 | use \Comodojo\Foundation\Logging\Manager as LogManager; |
||
| 12 | use \Comodojo\Foundation\Logging\LoggerTrait; |
||
| 13 | use \Psr\Log\LoggerInterface; |
||
| 14 | use \RuntimeException; |
||
| 15 | use \Exception; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @package Comodojo Daemon |
||
| 19 | * @author Marco Giovinazzi <[email protected]> |
||
| 20 | * @license MIT |
||
| 21 | * |
||
| 22 | * LICENSE: |
||
| 23 | * |
||
| 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
| 25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
| 26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||
| 27 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
| 28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||
| 29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
||
| 30 | * THE SOFTWARE. |
||
| 31 | */ |
||
| 32 | |||
| 33 | abstract class Process { |
||
| 34 | |||
| 35 | use PidTrait; |
||
| 36 | use EventsTrait; |
||
| 37 | use LoggerTrait; |
||
| 38 | use SignalsTrait; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Build the process |
||
| 42 | * |
||
| 43 | * @param int $niceness |
||
| 44 | * @param LoggerInterface $logger; |
||
| 45 | * @param EventsManager $events; |
||
| 46 | */ |
||
| 47 | 1 | public function __construct($niceness = null, LoggerInterface $logger = null, EventsManager $events = null) { |
|
| 48 | |||
| 49 | 1 | if ( !Checks::cli() ) { |
|
| 50 | throw new RuntimeException("Process can run only in cli SAPI"); |
||
| 51 | } |
||
| 52 | |||
| 53 | 1 | if ( !Checks::signals() ) { |
|
| 54 | throw new Exception("Missing pcntl signaling"); |
||
| 55 | } |
||
| 56 | |||
| 57 | 1 | $this->logger = is_null($logger) ? LogManager::create('daemon', false)->getLogger() : $logger; |
|
| 58 | 1 | $this->events = is_null($events) ? EventsManager::create($this->logger) : $events; |
|
| 59 | |||
| 60 | // get current PID |
||
| 61 | 1 | $this->pid = ProcessTools::getPid(); |
|
| 62 | |||
| 63 | 1 | if ( ProcessTools::setNiceness($niceness) === false ) { |
|
| 64 | $this->logger->warning("Unable to set process niceness to $niceness"); |
||
| 65 | } |
||
| 66 | |||
| 67 | // register signals |
||
| 68 | 1 | $this->signals = new PosixSignals(); |
|
| 69 | 1 | $this->signals->any()->call(array($this, 'signalToEvent')); |
|
| 70 | |||
| 71 | 1 | } |
|
| 72 | |||
| 73 | /** |
||
| 74 | * The generic signal handler. |
||
| 75 | * |
||
| 76 | * It transforms a signal into framework catchable event. |
||
| 77 | * |
||
| 78 | * @param int $signal |
||
| 79 | * @return self |
||
| 80 | */ |
||
| 81 | public function signalToEvent($signal) { |
||
| 82 | |||
| 83 | if ( $this->pid == ProcessTools::getPid() ) { |
||
| 84 | |||
| 85 | $signame = $this->signals->signame($signal); |
||
| 86 | |||
| 87 | $this->logger->debug("Received $signame ($signal) signal, firing associated event(s)"); |
||
| 88 | |||
| 89 | $this->events->emit(new PosixEvent($signal, $this)); |
||
| 90 | $this->events->emit(new PosixEvent($signame, $this)); |
||
| 91 | |||
| 92 | } |
||
| 93 | |||
| 94 | return $this; |
||
| 95 | |||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Stop current process execution. |
||
| 100 | * |
||
| 101 | * @param integer $return_code |
||
| 102 | */ |
||
| 103 | public function end($return_code) { |
||
| 104 | |||
| 105 | exit($return_code); |
||
|
0 ignored issues
–
show
|
|||
| 106 | |||
| 107 | } |
||
| 108 | |||
| 109 | } |
||
| 110 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.