定制 kohkimakimoto/workerphp 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kohkimakimoto/workerphp

Composer 安装命令:

composer require kohkimakimoto/workerphp

包简介

A PHP micro job scheduler framework like cron.

README 文档

README

Build Status Latest Stable Version License

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

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 kohkimakimoto/workerphp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.71k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 111
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 111
  • Watchers: 6
  • Forks: 18
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-12-04