Skip to content

Commit

Permalink
Merge pull request #2 from osgochina/master
Browse files Browse the repository at this point in the history
update
  • Loading branch information
hakiu committed Dec 2, 2019
2 parents 2ee050c + c8a1888 commit 61ecbf6
Show file tree
Hide file tree
Showing 912 changed files with 226,031 additions and 1,748 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.js linguist-language=php
*.css linguist-language=php
*.html linguist-language=php
170 changes: 96 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ Swoole-Crontab(基于Swoole扩展)
+ 基于swoole的定时器程序,支持秒级处理.
+ 异步多进程处理。
+ 完全兼容crontab语法,且支持秒的配置,可使用数组规定好精确操作时间
+ 请使用swoole扩展1.7.9-stable及以上版本.[Swoole](https://github.com/swoole/swoole-src)
+ 支持worker处理redis队列任务
+ 单中心-多客户端模式,能够横向扩展
+ web界面管理,增删改查任务,完整的权限控制.
+ 请使用swoole扩展1.8.0+
+ [v0.8版本入口](https://github.com/osgochina/swoole-crontab/tree/v0.8)

2.配置的支持
2.架构图
--------------
![](https://raw.githubusercontent.com/osgochina/swoole-crontab/master/doc/x.png)

3.Crontab配置
--------------
具体配置文件请看 [src/config/crontab.php](https://github.com/osgochina/swoole-crontab/blob/master/src/config/crontab.php)
介绍一下时间配置

0 1 2 3 4 5
Expand All @@ -21,75 +26,92 @@ Swoole-Crontab(基于Swoole扩展)
| | +---------- hour (0 - 23)
| +------------ min (0 - 59)
+-------------- sec (0-59)[可省略,如果没有0位,则最小时间粒度是分钟]
3.帮助信息
----------
* Usage: /path/to/php main.php [options] -- [args...]

* -h [--help] 显示帮助信息
* -p [--pid] 指定pid文件位置(默认pid文件保存在当前目录)
* -s start 启动进程
* -s stop 停止进程
* -s restart 重启进程
* -l [--log] log文件夹的位置
* -c [--config] config文件的位置
* -d [--daemon] 是否后台运行
* -r [--reload] 重新载入配置文件
* -m [--monitor] 监控进程是否在运行,如果在运行则不管,未运行则启动进程
* --worker 开启worker 可以针对redis队列读取并编写处理逻辑
* --tasktype task任务获取类型,[file|mysql] 默认是file
* --checktime 默认精确对时(如果精确对时,程序则会延时到分钟开始0秒启动) 值为false则不精确对时


4.worker进程配置
-----------------
在src/config/worker.php 中写入配置,并且启动的时候加上 --worker选项就能启动worker工作进程
配置如下:

return array(
//key是要加载的worker类名
"ReadBook"=>array(
"name"=>"队列1", //备注名
"processNum"=>1, //启动的进程数量
"redis"=>array(
"host"=>"127.0.0.1", // redis ip
"port"=>6379, // redis端口
"timeout"=>30, // 链接超时时间
"db"=>0, // redis的db号
"queue"=>"abc" // redis队列名
)
)
);
具体的业务逻辑在src/worker/ 文件夹下。可以自己定义业务逻辑类,只需要继承WorkerBase.class.php中的WorkerBase基类就可以


5.例子

4.开始使用
-----------
你可以在配置文件中加上以下配置:

return array(
'taskid1' =>
array(
'taskname' => 'php -i', //任务名称
'rule' => '* * * * * *',//定时规则,可以使用数组精确设置时间 如:array("22:18","2015-11-11 00:00:00 ","10:20:39")
"unique" => 2, //排他数量,如果已经有这么多任务在执行,即使到了下一次执行时间,也不执行
'execute' => 'Cmd',//命令处理类
'args' =>
array(
'cmd' => 'php -i',//命令
"ext": ""
),
),
);
然后去到src目录下,执行

/path/to/php main.php -s start

执行完成以后你就可以在/tmp/test.log看到输出了,每秒输出一次

如果你需要写自己的代码逻辑,你也可以到plugin目录下,实现一个PluginBase.class.php接口的类.
在其中写自己的逻辑代码。

6.TODO
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](https://github.com/swoole/framework.git)

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.后台截图

![](https://raw.githubusercontent.com/osgochina/swoole-crontab/master/doc/demo.png)

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

![](https://raw.githubusercontent.com/osgochina/swoole-crontab/master/doc/xufei100.png)


QQ群:560807006

5.TODO
----------
+ 分布式运行
+ restful增删改任务

- [ ] 去除nginx依赖
- [ ] 配置文件统一
- [ ] 去除swoole framework框架的依赖
- [ ] restful api接口
- [ ] 通过命令行管理任务
- [ ] 无中心服依赖
54 changes: 54 additions & 0 deletions bin/build.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/local/bin/php
<?php
if (empty($argv[1]))
{
$dst = 'agent';
}
else
{
$dst = trim($argv[1]);
}

if ($dst == 'agent')
{
$pharFile = __DIR__ . '/agent.phar';
if (file_exists($pharFile)) unlink($pharFile);
$phar = new Phar($pharFile);
$phar->buildFromDirectory(dirname(__DIR__) . "/src/agent/");
//$phar->compressFiles(\Phar::GZ);
$phar->stopBuffering();
$phar->setStub($phar->createDefaultStub('agent.php'));
echo "agent.phar打包成功\n";
}
elseif ($dst == 'centre')
{
$pharFile = __DIR__ . '/crontab-centre.phar';
if (file_exists($pharFile)) unlink($pharFile);
$phar = new Phar($pharFile);
$phar->buildFromDirectory(dirname(__DIR__) . "/src/center/");
//$phar->compressFiles(\Phar::GZ);
$phar->stopBuffering();
$phar->setStub($phar->createDefaultStub('centre.php'));
echo "crontab-center.phar打包成功\n";
}
elseif ($dst == 'phar')
{
if (empty($argv[2]))
{
die("使用方法:php {$argv[0]} {$argv[1]} 源码目录\n");
}

if (!is_dir($argv[2]))
{
die("目录({$argv[2]})不存在\n");
}
$dirname = basename($argv[2]);
$filename = $dirname . '.phar';
$pharFile = __DIR__ . '/'.$filename;
$phar = new Phar($pharFile);
$phar->buildFromDirectory($argv[2]);
$phar->compressFiles(\Phar::GZ);
$phar->stopBuffering();
$phar->setStub($phar->createDefaultStub('main.php'));
echo "{$filename}打包成功\n";
}
Loading

0 comments on commit 61ecbf6

Please sign in to comment.