PHP code example of kohkimakimoto / workerphp

1. Go to this page and download the library: Download kohkimakimoto/workerphp 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/ */

    

kohkimakimoto / workerphp example snippets



ker = new \Kohkimakimoto\Worker\Worker();

// job for every minute.
$worker->job("hello", ['cron_time' => '* * * * *', 'command' => function(){
    echo "Hello world\n";
}]);

// job runs a shell command.
$worker->job("uptime", ['cron_time' => '10 * * * *', 'command' => "uptime"]);

$worker->start();

// worker.php

kimakimoto\Worker\Worker();

// ... job definitions

$worker->start();

$worker->job("hello", ['cron_time' => '* * * * *', 'command' => function(){
    echo "Hello world\n";
}]);

$worker->job("uptime", ['cron_time' => '* * * * *', 'command' => "uptime"]);

$worker->job("hello", ['cron_time' => '* * * * *', 'max_processes' => 1, 'command' => function(){
    echo "Hello world\n";
    sleep(70);
;}]);

$worker = new \Kohkimakimoto\Worker\Worker();
$worker->httpServer->listen();

// ...

$worker->start();

$worker->httpServer->listen(8888, 'localhost');

$ php worker.php
Starting WorkerPHP.
Successfully booted. Quit working with CONTROL-C.

$ php worker.php
...
Runs job: hello (pid: 90621) at 2014-12-16 08:03:00
Hello world
Skip the job 'hello' due to limit of max processes: 1 at 2014-12-16 08:04:00