zoukankan      html  css  js  c++  java
  • php 实现简单的自定义计划任务组件 crontabjobs

    2021年12月4日10:22:18

    gitee包地址:https://gitee.com/open-php/crontab-jobs

    两个包总共才几个文件,性能消耗小,包小,特别适合做一些服务组件

    支持composer

    composer require zx/crontab-jobs dev-master
    crontab-jobs
    介绍
    PHP定时任务组件
    
    后续需要新增的功能
    1,支持日志功能
    2,支持任务重试功能
    3,支持任务互斥锁
    
    使用
    use Zx\CrontabJobs\Command;
    use Zx\CrontabJobs\Kernel;
    use Cron\CronExpression;
    
    ini_set('date.timezone', 'Asia/Shanghai');
    
    class t1 extends Command
    {
        private string $contab;
        private string $retry;
    
        public function __construct(string $contab = '', int $retry = 0)
        {
            $this->contab = $contab;
            $this->retry = $retry;
        }
    
        public function handle()
        {
            print_r(self::class);
            echo PHP_EOL;
            print_r(date('Y-m-d H:i:s'));
            echo PHP_EOL;
        }
    
        public function getRetry()
        {
            return $this->retry;
        }
    
        public function getCrontab()
        {
            return $this->contab;
        }
    }
    
    class t2 extends Command
    {
        private string $contab;
        private string $retry;
    
        public function __construct(string $contab = '', int $retry = 0)
        {
            $this->contab = $contab;
            $this->retry = $retry;
        }
    
        public function handle()
        {
            print_r(self::class);
            echo PHP_EOL;
            print_r(date('Y-m-d H:i:s'));
            echo PHP_EOL;
        }
    
        public function getRetry()
        {
            return $this->retry;
        }
    
    
        public function getCrontab()
        {
            return $this->contab;
        }
    }
    
    $t1 = new t1('* * * * *', 0);
    $t2 = new t2('*/5 * * * *', 0);
    
    Kernel::register($t1);
    Kernel::register($t2);
    
    Kernel::run('now', new DateTimeZone('Asia/Shanghai'));
    业务脚本继承Command之后,就可以直接注册到Kernel 然后直接调用
    
    Kernel::run('now', new DateTimeZone('Asia/Shanghai'));
    支持时区切换
    
    添加计划任务
    * * * * * cd /data/code/crontab-jobs/tests && /usr/local/php80/bin/php /data/code/crontab-jobs/tests/test.php >> /data/crontab.log
    指定特定用户执行
    * * * * * sudo -u www && cd /data/code/crontab-jobs/tests && /usr/local/php80/bin/php /data/code/crontab-jobs/tests/test.php >> /data/crontab.log

     

     

    QQ一群 247823727
    QQ二群 166427999
    博客文件如果不能下载请进群下载
    如果公司项目有技术瓶颈问题,如有需要,技术服务QQ: 903464207
  • 相关阅读:
    数据库索引的作用和长处缺点
    ping不通的几种可能原因
    UVA
    strtok、strtok_s、strtok_r 字符串切割函数
    CheckBoxPreference组件
    EM算法原理
    Android中ExpandableListView控件基本使用
    拓扑排序的原理及事实上现
    DropdownList绑定的两种方法
    leetcode第一刷_Length of Last Word
  • 原文地址:https://www.cnblogs.com/zx-admin/p/15641124.html
Copyright © 2011-2022 走看看