kohkimakimoto/workerphp
Composer 安装命令:
composer require kohkimakimoto/workerphp
包简介
A PHP micro job scheduler framework like cron.
README 文档
README
A PHP micro job scheduler framework like cron.
<?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();
Requirements
- PHP5.4 or later
- pcntl extension
Installation
Create composer.json for installing via composer.
{
"require": {
"kohkimakimoto/workerphp": "0.*"
}
}
Run composer install command.
$ composer install
Usage
Bootstrap
To make a job scheduler application like cron, create worker.php file (or other name you want).
You need to load composer autoload.php file and create an instance of Kohkimakimoto\Worker\Worker.
// worker.php <?php require_once __DIR__.'/vendor/autoload.php'; $worker = new \Kohkimakimoto\Worker\Worker(); // ... job definitions $worker->start();
Run php worker.php. You will get the following messages and the process keep in your system. But it is not useful at this time. Stop the process using CONTROL-C.
$ php worker.php
Starting WorkerPHP.
Successfully booted. Quit working with CONTROL-C.
Learn about jobs at the next section.
Jobs
Define a job.
$worker->job("hello", ['cron_time' => '* * * * *', 'command' => function(){ echo "Hello world\n"; }]);
This $worker->job method has two arguments. The first argument is name of job. It must be unique in all jobs.
The second argument is an array that has some parameters. cron_time is a schedule when to run the job.
It is a "cron expressions" string. command is a closure that is executed by the worker.
You can run it. You will get messages like the below.
$ 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
The job you defined runs every minute.
Also you can define command a command string not a closure.
$worker->job("uptime", ['cron_time' => '* * * * *', 'command' => "uptime"]);
The worker runs command uptime every minute.
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
You can set a limit on running processes at the same time. Use 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 Server (Web APIs)
WorkerPHP has a built-in http server. It provides APIs that controls jobs using HTTP requests. Write the following code.
$worker = new \Kohkimakimoto\Worker\Worker(); $worker->httpServer->listen(); // ... $worker->start();
When WorkerPHP starts, It listens port 8080 (default).
You can modify listened port and host.
$worker->httpServer->listen(8888, 'localhost');
Get a worker info
If you started http server, you can get worker infomation using http request.
$ curl -XGET http://localhost:8080/?pretty=1
You will get below 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": []
}
]
}
Get a job info
You can get a job info by specifing job name.
$ curl -XGET http://localhost:8080/hello?pretty=1
You will get below json.
{
"number_of_running_jobs": 0
}
Run a job
You can run a job using POST request.
$ curl -XPOST http://localhost:8080/hello?pretty=1
You will get below json.
{
"status": "OK"
}
Service Providers
ServiceProvider allow the user to extend WorkerPHP. Please see the built-in Service Provider StatsServiceProvider.
Author
Kohki Makimoto kohki.makimoto@gmail.com
License
MIT license.
kohkimakimoto/workerphp 适用场景与选型建议
kohkimakimoto/workerphp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.71k 次下载、GitHub Stars 达 111, 最近一次更新时间为 2014 年 12 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 kohkimakimoto/workerphp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kohkimakimoto/workerphp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 3.71k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 111
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-12-04