定制 jenner/crontab 二次开发

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

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

jenner/crontab

Composer 安装命令:

composer require jenner/crontab

包简介

php crontab base on pcntl and libev

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License License

php crontab base on pcntl and react/event-loop

中文说明

Why use php_crontab?

When we have a handful of crontab tasks, crontab service is enough for us to manage them. If we have many crontab tasks, there will be some problems like:

  • The crontab tasks are managed in a text file. If there are no comment, it will be hard for fresh man to understand what they are.
  • If the crontab tasks are distributed in different servers, it will be hard to manage them.
  • If you want to collect the crontab tasks' logs, it will be not easy.
  • Tasks of different users must written in different files. Based on the above reasons, we need a crontab manager which can manage crontab tasks together and configure the tasks.

How to use php_crontab?

First composer require jenner/crontab.
There are two ways to use php_crontab to manage your crontab tasks. You can just write a php script and add it to the crontab config file with the command crontab -e. The php script should run every minute. For example tests/simple.php
Or you can write a php daemon script which will run as a service and will not exit until someone kill it. It will check the tasks every minute. For example tests/daemon.php

Import

composer require jenner/crontab

Properties

  • The crontab tasks can be stored in any way you what. For example, mysql, reids. What's more? You can develop a web application to manage them.
  • The tasks of different users can be managed together.
  • Multi-Process, every task is a process.
  • You can set the user and group of a crontab task
  • STDOUT can be redirected
  • Based on react/event-loop, it can run as a daemon.
  • A HTTP server which you can manage the crontab tasks through it.
  • Dynamic task loader, you can register a task loader by Daemon::registerTaskLoader, which will execute every 60 seconds and update the crontab tasks.

HTTP interfaces

HTTP METHOD: GET

  • add add new task to crontab server
  • get_by_name get task by name
  • remove_by_name remove task by name
  • clear clear all task
  • get get all tasks
  • start start crontab loop
  • stop stop crontab loop

Examples:

http://host:port/add?name=name&cmd=cmd&time=time&out=out&user=user&group=group&comment=comment
http://host:port/get_by_name?name=name
http://host:port/remove_by_name?name=name
http://host:port/clear
http://host:port/get
http://host:port/start
http://host:port/stop

TODO

  • add log handler interface.
  • add http log handler, socket log handler, file handler and so on.
  • separate stdout and stderr. use different handlers

run based on crontab service

* * * * * php demo.php
<?php
$missions = [
    [
        'name' => 'ls',
        'cmd' => "ls -al",
        'out' => '/tmp/php_crontab.log',
        'err' => '/tmp/php_crontab.log',
        'time' => '* * * * *',
        'user' => 'www',
        'group' => 'www'
    ],
    [
        'name' => 'ls',
        'cmd' => "ls -al",
        'out' => '/tmp/php_crontab.log',
        'err' => '/tmp/php_crontab.log',
        'time' => '* * * * *',
        'user' => 'www',
        'group' => 'www'
    ],
];

$tasks = array();
foreach($missions as $mission){
    $tasks[] = new \Jenner\Crontab\Mission($mission['name'], $mission['cmd'], $mission['time'], $mission['out']);
}

$crontab_server = new \Jenner\Crontab\Crontab(null, $tasks);
$crontab_server->start(time());

run as a daemon

it will check the task configs every minute.

$missions = [
    [
        'name' => 'ls',
        'cmd' => "ls -al",
        'out' => '/tmp/php_crontab.log',
        'err' => '/tmp/php_crontab.log',
        'time' => '* * * * *',
        'user' => 'www',
        'group' => 'www'
    ],
    [
        'name' => 'ls',
        'cmd' => "ls -al",
        'out' => '/tmp/php_crontab.log',
        'err' => '/tmp/php_crontab.log',
        'time' => '* * * * *',
        'user' => 'www',
        'group' => 'www'
    ],
];

$daemon = new \Jenner\Crontab\Daemon($missions);
$daemon->start();

Or use the task loader

function task_loader() {
    $missions = [
        [
            'name' => 'ls',
            'cmd' => "ls -al",
            'out' => '/tmp/php_crontab.log',
            'time' => '* * * * *',
            'user' => 'www',
            'group' => 'www'
        ],
        [
            'name' => 'ls',
            'cmd' => "ls -al",
            'out' => '/tmp/php_crontab.log',
            'time' => '* * * * *',
            'user' => 'www',
            'group' => 'www'
        ],
    ];

    return $missions;
}

$daemon = new \Jenner\Crontab\Daemon();
$daemon->registerTaskLoader("task_loader");
$daemon->start();

run as a daemon and start the http server

$missions = [
    [
        'name' => 'ls',
        'cmd' => "ls -al",
        'out' => '/tmp/php_crontab.log',
        'err' => '/tmp/php_crontab.log',
        'time' => '* * * * *',
        'user' => 'www',
        'group' => 'www'
    ],
    [
        'name' => 'ls',
        'cmd' => "ls -al",
        'out' => '/tmp/php_crontab.log',
        'err' => '/tmp/php_crontab.log',
        'time' => '* * * * *',
        'user' => 'www',
        'group' => 'www'
    ],
];

$http_daemon = new \Jenner\Crontab\HttpDaemon($missions, "php_crontab.log");
$http_daemon->start($port = 6364);

Then you can manage the crontab task by curl like:

curl http://127.0.0.1:6364/get_by_name?name=ls
curl http://127.0.0.1:6364/remove_by_name?name=hostname
curl http://127.0.0.1:6364/get

run the script

[root@jenner php_crontab]# ./bin/php_crontab 
php_crontab help:
-c  --config    crontab tasks config file
-p  --port      http server port
-f  --pid-file  daemon pid file
-l  --log       crontab log file
[root@jenner php_crontab]#nohup ./bin/php_crontab -c xxoo.php -p 8080 -f /var/php_crontab.pid -l /var/logs/php_crontab.log >/dev/null & 

blog:www.huyanping.cn

jenner/crontab 适用场景与选型建议

jenner/crontab 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 921 次下载、GitHub Stars 达 307, 最近一次更新时间为 2015 年 10 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「crontab」 「php crontab」 「crontab manager」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jenner/crontab 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jenner/crontab 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 921
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 307
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 307
  • Watchers: 31
  • Forks: 93
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-10-06