koikimakimoto/workerphp

一个类似于cron的PHP微任务调度框架。

v0.6.0 2014-12-16 01:14 UTC

This package is auto-updated.

Last update: 2024-09-07 00:02:44 UTC


README

Build Status Latest Stable Version License

一个类似于cron的PHP微任务调度框架。

<?php
require_once __DIR__.'/vendor/autoload.php';

$worker = 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();

需求

安装

创建composer.json以通过composer安装。

{
    "require": {
        "kohkimakimoto/workerphp": "0.*"
    }
}

运行composer install命令。

$ composer install

使用方法

引导

要创建一个像cron一样的作业调度应用程序,请创建worker.php文件(或您想要的任何其他名称)。您需要加载composer autoload.php文件并创建一个Kohkimakimoto\Worker\Worker实例。

// worker.php
<?php
require_once __DIR__.'/vendor/autoload.php';

$worker = new \Kohkimakimoto\Worker\Worker();

// ... job definitions

$worker->start();

运行php worker.php。您将收到以下消息,进程将保留在您的系统中。但在此阶段它并不实用。使用CONTROL-C停止进程。

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

在下节中了解有关任务的信息。

任务

定义一个任务。

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

$worker->job方法有两个参数。第一个参数是作业的名称。它必须在所有作业中是唯一的。第二个参数是一个包含一些参数的数组。cron_time是运行作业的调度。它是一个“cron表达式”字符串。command是由工作者执行的闭包。

您可以运行它。您将收到如下所示的消息。

$ php worker.php
Starting WorkerPHP.
Initializing job: hello (job_id: 0)
Successfully booted. Quit working with CONTROL-C.
Running job: hello (pid: 36643) at 2014-12-08 14:56:00
Hello world
Finished job: hello (pid: 36643) at 2014-12-08 14:56:00
Running job: hello (pid: 36646) at 2014-12-08 14:57:00
Hello world
Finished job: hello (pid: 36646) at 2014-12-08 14:57:00
Running job: hello (pid: 36647) at 2014-12-08 14:58:00
Hello world
Finished job: hello (pid: 36647) at 2014-12-08 14:58:00

您定义的作业每分钟运行一次。

您还可以定义command为一个命令字符串而不是闭包。

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

工作者每分钟运行uptime命令。

Running job: uptime (pid: 36650) at 2014-12-04 12:37:00
12:37  up 8 days, 16:06, 6 users, load averages: 1.82 1.74 1.83
Finished job: uptime (pid: 36650) at 2014-12-04 12:37:00

您可以使用max_processes设置同时运行的进程的限制。

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

Http服务器(Web API)

WorkerPHP具有内置的http服务器。它提供使用HTTP请求控制作业的API。编写以下代码。

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

// ...

$worker->start();

当WorkerPHP启动时,它监听端口8080(默认)。您可以修改监听端口和主机。

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

获取工作者信息

如果您已启动http服务器,您可以使用http请求获取工作者信息。

$ curl -XGET https://:8080/?pretty=1

您将收到以下json。

{
    "name": "WorkerPHP",
    "number_of_jobs": 2,
    "jobs": [
        {
            "id": 0,
            "name": "hello",
            "max_processes": 1,
            "last_runtime": "2014-12-15 15:55:38",
            "next_runtime": "2014-12-15 15:56:00",
            "arguments": []
        },
        {
            "id": 1,
            "name": "uptime",
            "max_processes": 1,
            "last_runtime": "2014-12-15 15:55:38",
            "next_runtime": "2014-12-15 15:56:00",
            "arguments": []
        }
    ]
}

获取作业信息

您可以通过指定作业名称来获取作业信息。

$ curl -XGET https://:8080/hello?pretty=1

您将收到以下json。

{
    "number_of_running_jobs": 0
}

运行作业

您可以使用POST请求运行作业。

$ curl -XPOST https://:8080/hello?pretty=1

您将收到以下json。

{
    "status": "OK"
}

服务提供者

服务提供者允许用户扩展WorkerPHP。请参阅内置服务提供者StatsServiceProvider

作者

Kohki Makimoto kohki.makimoto@gmail.com

许可证

MIT许可证。