基于swoole的定时器程序,支持秒级处理

Overview

Swoole-Crontab(基于Swoole扩展)

1.概述

  • 基于swoole的定时器程序,支持秒级处理.
  • 异步多进程处理。
  • 完全兼容crontab语法,且支持秒的配置,可使用数组规定好精确操作时间
  • 单中心-多客户端模式,能够横向扩展
  • web界面管理,增删改查任务,完整的权限控制.
  • 请使用swoole扩展1.8.0+
  • v0.8版本入口

2.架构图

3.Crontab配置

介绍一下时间配置

0   1   2   3   4   5
|   |   |   |   |   |
|   |   |   |   |   +------ day of week (0 - 6) (Sunday=0)
|   |   |   |   +------ month (1 - 12)
|   |   |   +-------- day of month (1 - 31)
|   |   +---------- hour (0 - 23)
|   +------------ min (0 - 59)
+-------------- sec (0-59)[可省略,如果没有0位,则最小时间粒度是分钟]

4.开始使用

1.修改配置

1.1 中心服配置

/path/to/src/center/configs/dev/db.php 修改数据库配置
进入mysql数据库执行/path/to/doc/crontab.sql 的sql文件
src/center/_init.php   修改 PUBLIC_PATH   swoole framework框架所在的路径

1.2 admin管理后台的配置文件修改

src/admin/configs/dev/db.php  修改数据库配置
src/admin/configs/dev/service.php  中心服启动时候监听的ip端口,需要跟中心服通讯
src/public/index.php  
    修改 WEBROOT       域名
    修改 PUBLIC_PATH   swoole framework框架所在的路径

2.下载swoole framework框架到本地/data/www/public/ framework

3.配置nginx,列子如下:

server {
    listen       80;
    server_name  crontab.test.com;
    
    root /data/www/wwwroot/swoole-crontab/src/public;
    
    index index.php index.html;
    location / {
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php;
        }
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}

4.启动中心服

/path/to/php /path/to/src/center/center.php  start -d -h 127.0.0.1 -p 8901

5.启动客户端

-h 是指中心服地址 -p 中心服端口
/path/to/php /path/to/src/agent/agent.php start -d -h 127.0.0.1 -p 8901

6.web界面访问

输入nginx配置的地址访问web界面,默认用户名/密码是admin/admin

7.操作步骤

请按照以上步骤操作,因为admin后台是通过接口连接上中心服来进行管理.所以必须先启动中心服,再打开admin管理后台

4.使用交流

1.后台截图

2.更加清晰的架构图.感谢@xufei100

QQ群:560807006

5.TODO

  • 去除nginx依赖
  • 配置文件统一
  • 去除swoole framework框架的依赖
  • restful api接口
  • 通过命令行管理任务
  • 无中心服依赖
Comments
  • Undefined index

    Undefined index

    2017/07/18 10:06:33 [error] 3082#0: *8 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined index: _gid in /opt/www/swoole-crontab/src/admin/classes/CommonController.php on line 37 PHP message: PHP Notice: Undefined index: _gname in /opt/www/swoole-crontab/src/admin/classes/CommonController.php on line 38" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /password/modifypassword/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost:8900", referrer: "http://localhost:8900/user/index/"

    2017/07/18 10:06:36 [error] 3082#0: *8 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined index: _gid in /opt/www/swoole-crontab/src/admin/classes/CommonController.php on line 37 PHP message: PHP Notice: Undefined index: _gname in /opt/www/swoole-crontab/src/admin/classes/CommonController.php on line 38 PHP message: PHP Notice: Undefined index: _gid in /opt/www/swoole-crontab/src/admin/controllers/Crontab.php on line 35 PHP message: PHP Warning: Invalid argument supplied for foreach() in /opt/www/swoole-crontab/src/admin/controllers/Crontab.php on line 50 PHP message: PHP Notice: Undefined index: _gid in /opt/www/swoole-crontab/src/admin/controllers/Crontab.php on line 41" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /crontab/index/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost:8900", referrer: "http://localhost:8900/password/modifypassword/"

    opened by mia0x75 5
  • mac 10.11.3 有  cli_set_process_title 错误提示呢

    mac 10.11.3 有 cli_set_process_title 错误提示呢

    PHP Warning: cli_set_process_title(): cli_set_process_title had an error: Not initialized correctly in /Users/liuyunfei/swoole-crontab/src/include/Process.class.php on line 50

    opened by lyf845990516 4
  • cronAgent /agent/index/ 管理报错

    cronAgent /agent/index/ 管理报错

    Warning: Invalid argument supplied for foreach() in swoole-crontab/src/admin/controllers/Agent.php on line 44 $ret = App\Service::getInstance()->call("Agent::getAgents",$gets,$page,$pagesize)->getResult(10);这个查询并没有返回["total"=>$pager->total,"rows"=>$list]而是返回空 agents表里有数据的。这是为什么呢?

    opened by kimistar 2
  • 无法正常使用

    无法正常使用

    你好,为什么我配置完了,只能显示权限和用户页面,其他页面都无法正常显示。 报错如下: __construct

    need a param of total


    #0 line:54 call:Swoole\Error::info file:/opt/tony/data/www/public/framework/libs/Swoole/Pager.php #1 line:34 call:Swoole\Pager->__construct file:/opt/tony/data/www/wwwroot/swoole-crontab-master/src/admin/controllers/Agent.php #2 line:761 call:App\Controller\Agent->index file:/opt/tony/data/www/public/framework/libs/Swoole/Swoole.php #3 line:50 call:Swoole->runMVC file:/opt/tony/data/www/wwwroot/swoole-crontab-master/src/public/index.php

    谁能帮忙看一下嘛 ? 谢谢

    opened by blocksky 2
  • 有错误

    有错误

    PHP Fatal error: Cannot use object of type swoole_http_response as array in swoole-crontabv0.2/src/include/Http.class.php on line 50 ,另外为什么 http://127.0.0.1:9501 ,第一次能访问,以后就不能访问 了

    opened by w3yyb 2
  • 几处修正

    几处修正

    1,说明文档 fix center为center.php 地点:/path/to/php /path/to/src/center/center start -d -h 127.0.0.1 -p 8901

    2, sql文件 swoole-crontab/doc/crontab.sql agents 表有四个字段,但是insert插入语句有五个字段值; 地点:INSERT INTO agents VALUES ('1', 'Crontab服务', '127.0.0.1', '8902', '0');

    opened by NaiBox 1
  • 安装好后的默认的测试任务没能执行

    安装好后的默认的测试任务没能执行

    任务日志显示“任务发送失败”;同时php_error.log中显示: PHP Warning: Swoole\Database\MySQLi SQL Error:Commands out of sync; you can't run this command now


    insert into term_logs (taskid,runid,explain,msg,createtime) values('3097968986801831941','3112807076871536715','任务开始','{"id":3097968986801831941,"execute":"\/bin\/echo 'hello swoole-crontab' >> \/home\/wwwlogs\/crontab.log","agents":"1","taskname":"\u6d4b\u8bd5\u4efb\u52a1","runuser":"nobody","runid":3112807076871536715}','2017-01-13 18:08:59')
    Server: localhost:3306.
    另:求该项目的架构图及实现流程 多谢

    opened by waynesun01 1
  • 命令行输入的命令长一些, agent 日志会出现长度warning

    命令行输入的命令长一些, agent 日志会出现长度warning

    [10-Nov-2016 23:24:35 Asia/Shanghai] PHP Warning: swoole_process::name(): process name is too long,the max len is 127 in /data/www/public/swoole-crontab/src/agent/Lib/Process.php on line 130

    opened by changxy726 1
  • 如何指定agent启动后绑定的网卡ip?

    如何指定agent启动后绑定的网卡ip?

    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.47.208.181 netmask 255.255.248.0 broadcast 10.47.215.255 ether 00:16:3e:10:06:16 txqueuelen 1000 (Ethernet) RX packets 2993139873 bytes 3668054774619 (3.3 TiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1745359630 bytes 343925852893 (320.3 GiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

    lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 0 (Local Loopback) RX packets 30935796 bytes 14148769302 (13.1 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 30935796 bytes 14148769302 (13.1 GiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

    virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:24:96:8b txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

    这台机器启动agent后, agent绑到192.168.122.1上面去了

    opened by changxy726 1
  • 运行任务时输出swReactorEpoll_del(:151): epoll remove fd

    运行任务时输出swReactorEpoll_del(:151): epoll remove fd

    2021-11-01 15:46:01 | 进程运行完成,输出值 [3430422174165368838] -- | -- cleanCustomersWishlist 3429870233995706749 | [2021-11-01 02:46:01 @85107.0] ERROR swReactorEpoll_del(:151): epoll remove fd[6#0] failed. Error: Bad file descriptor[9]. 连接关闭

    opened by blue110 0
  • Fatal error: Uncaught Swoole\Error: must be forked outside the coroutine

    Fatal error: Uncaught Swoole\Error: must be forked outside the coroutine

    执行php main.php -s start 提示:Fatal error: Uncaught Swoole\Error: must be forked outside the coroutine in Donkey-0.8/src/include/Process.class.php:21 Stack trace: Donkey-0.8/src/include/Process.class.php(21): Swoole\Process->start() Donkey-0.8/src/include/Crontab.class.php(216): Process->create_process('taskid1', Array) Donkey-0.8/src/include/Crontab.class.php(185): Crontab::do_something(2) #3 {main} Donkey-0.8/src/include/Process.class.php on line 21

    swoole 版本 swoole

    Swoole => enabled Author => Swoole Team [email protected] Version => 4.4.16 Built => Feb 27 2020 14:45:23 coroutine => enabled kqueue => enabled rwlock => enabled openssl => OpenSSL 1.0.2j 26 Sep 2016 pcre => enabled zlib => 1.2.8 async_redis => enabled

    Directive => Local Value => Master Value swoole.enable_coroutine => On => On swoole.enable_library => On => On swoole.enable_preemptive_scheduler => Off => Off swoole.display_errors => On => On swoole.use_shortname => On => On swoole.unixsock_buffer_size => 262144 => 262144

    操作系统 macOS Sierra 10.12.6

    后来我在Donkey-0.8/src/include/Crontab.class.php 注册定时任务register_timer() 添加 Swoole\Timer::set([ 'enable_coroutine' => false, ]); 禁用协程,再次启动才能正常运行demo,请问是要禁用协程吗

    opened by xingxing55 4
  • 遇到的问题

    遇到的问题

    老师您好,向您请教一下 现在我的情况是主机上运行中心服务端,中心服务端能收到信息,客户端显示连接成功,使用php center.php start -d -h 本机地址 -p 8901 无法进入客户端,只有使用127.0.0.1能够进入,如果想用其他机器就无法就无法连接了,请问应该怎么解决这个问题呢?

    opened by Leslack-hub 0
Owner
刘志铭
lucky
刘志铭