PHP code example of cruide / php-daemon-abstract

1. Go to this page and download the library: Download cruide/php-daemon-abstract library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cruide / php-daemon-abstract example snippets


use Cruide\PHPDaemon\AbstractDaemon;
use Cruide\PHPDaemon\DaemonConfig;
use Cruide\PHPDaemon\DaemonInterface;

$config = new DaemonConfig(workDir: '/var/app', sleepSeconds: 5);

$daemon = new class($config) extends AbstractDaemon implements DaemonInterface
{
    public function process(): void
    {
        file_put_contents(
            $this->config->workDir . '/data.log',
            date('c') . "\n",
            FILE_APPEND
        );
    }

    public function processException(\Throwable $e): void
    {
        error_log($e->getMessage());
    }

    public function onStart(): void {}
    public function onStop(): void {}
    public function reload(): void {}
};

$daemon->run();

$config = new DaemonConfig(
    workDir: '/my-app',
    sleepSeconds: 10,
    maxRepeats: 0,          // run forever
    maxMemoryMb: 128,
    errorStrategy: ErrorStrategy::STOP,
);

composer