Skip to content

Commit f5c6520

Browse files
committed
init complete
1 parent 86a5f75 commit f5c6520

14 files changed

+155
-113
lines changed

.travis.yml

Lines changed: 0 additions & 16 deletions
This file was deleted.

README.md

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
1-
# Swoft CronTask
1+
# Swoft 定时任务组件
22

3-
Swoft framework CronTask Component
3+
swoft定时任务组件,核心大部分基于1.0,在此基础上适配了2.0,实测可用。
44

5-
## Install
5+
## 安装
66

77
- composer command
88

99
```bash
1010
composer require banyancheung/cron-task
1111
```
1212

13-
## Document
14-
15-
Please see [document site](https://doc.swoft.org)
16-
17-
## Unit testing
13+
- composer.json
1814

1915
```bash
20-
phpunit
16+
"require": {
17+
...
18+
"banyancheung/cron-task": "dev-master",
19+
...
20+
}
2121
```
2222

23+
然后 `composer update` 即可。
24+
25+
## 使用
26+
27+
1.在`app/beans.php` 文件里的`server`一项,监听 `onPipeMessage` 事件。
28+
29+
![](https://img.yunjes.com/doc/cron-task/1.png)
30+
31+
2.在定义好的task注解里,增加 Scheduled 一项。
32+
33+
![](https://img.yunjes.com/doc/cron-task/2.png)
34+
35+
注意:Scheduled里的task属性是必须的,用于定位是哪个Task,也许会有更好的方法。:)
36+
2337
## LICENSE
2438

2539
The Component is open-sourced software licensed under the [Apache license](LICENSE).

composer.json

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"email": "i@zhangrong.cc"
1515
}
1616
],
17-
"description": "micro-service framework base on swoole",
17+
"description": "crontab task component base on swoft",
1818
"license": "Apache-2.0",
1919
"require": {
2020
"swoft/framework": "~2.0.0",
@@ -25,16 +25,6 @@
2525
"Swoft\\CronTask\\": "src/"
2626
}
2727
},
28-
"autoload-dev": {
29-
"psr-4": {
30-
"SwoftTest\\CronTask\\": "test/case"
31-
}
32-
},
33-
"require-dev": {
34-
},
35-
"scripts": {
36-
"test": "./vendor/bin/phpunit -c phpunit.xml"
37-
},
3828
"repositories": {
3929
"packagist": {
4030
"type": "composer",

phpunit.xml

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Crontab/Crontab.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Swoft\App;
66
use Swoft\Bean\Annotation\Mapping\Bean;
7+
use Swoft\CronTask\PipeMessage;
78
use Swoft\CronTask\TaskRegister;
89
use Swoole\Table as SwooleTable;
910

@@ -180,7 +181,7 @@ public function getRunTimeTable(): Table
180181
* 获取key值
181182
*
182183
* @param string $rule 规则
183-
* @param string $taskClass 任务类
184+
* @param string $task 任务名
184185
* @param string $taskMethod 任务方法
185186
* @param string $min 分
186187
* @param string $sec 时间戳
@@ -221,7 +222,6 @@ public function loadTableTask()
221222
private function initRunTimeTableData(array $task, array $parseResult): bool
222223
{
223224
$runTimeTable = $this->getRunTimeTable();
224-
225225
$min = date('YmdHi');
226226
$sec = strtotime(date('Y-m-d H:i'));
227227

@@ -309,6 +309,20 @@ public function startTask($key)
309309
return $this->getRunTimeTable()->set($key, ['runStatus' => self::START]);
310310
}
311311

312+
/**
313+
* @param string $task
314+
* @param string $method
315+
*/
316+
public function deliverTask(string $task, string $method): void
317+
{
318+
/* @var PipeMessageInterface $pipeMessage */
319+
$message = PipeMessage::pack(PipeMessage::MESSAGE_TYPE_TASK, [
320+
'name' => $task,
321+
'method' => $method
322+
]);
323+
\server()->getSwooleServer()->sendMessage($message, 0);
324+
}
325+
312326
/**
313327
* 完成任务
314328
*

src/Crontab/TableCrontab.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ class TableCrontab
6363
*/
6464
public static function init(int $taskCount = null, int $taskQueue = null)
6565
{
66-
echo 'init tableCrontab' . "\n";
6766
self::$taskCount = $taskCount == null ? self::$taskCount : $taskCount;
6867
self::$taskQueue = $taskQueue == null ? self::$taskQueue : $taskQueue;
6968
self::getInstance();

src/Listener/AttachCronProcessHandler.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,25 @@ class AttachCronProcessHandler implements EventHandlerInterface
3030
*/
3131
public function handle(EventInterface $event): void
3232
{
33-
echo "init event..."."\n";
3433
$setting = \config('cron');
3534
// Init crontab share memory table
3635
if (isset($setting['cronable']) && (int)$setting['cronable'] === 1) {
3736
$this->initCrontabMemoryTable();
3837
Crontab::init();
3938
$this->initProcessByEvent($event);
4039
}
41-
echo "init event succeed"."\n";
4240
}
4341

4442
/**
4543
* init table of crontab
4644
*/
4745
private function initCrontabMemoryTable(): void
4846
{
49-
echo "init memory table..."."\n";
5047
/** @var array[] $settings */
5148
$setting = \config('cron');
5249
$taskCount = isset($setting['task_count']) && $setting['task_count'] > 0 ? $setting['task_count'] : null;
5350
$taskQueue = isset($setting['task_queue']) && $setting['task_queue'] > 0 ? $setting['task_queue'] : null;
5451
TableCrontab::init($taskCount, $taskQueue);
55-
echo "init memory table succeed"."\n";
5652
}
5753

5854
/**
@@ -61,13 +57,11 @@ private function initCrontabMemoryTable(): void
6157
*/
6258
private function initProcessByEvent(EventInterface $event): void
6359
{
64-
echo "init process..."."\n";
6560
$swooleServer = $event->getTarget()->getSwooleServer();
6661
$execProcess = \bean(CronExecProcess::class)->create();
6762
$timerProcess = \bean(CronTimerProcess::class)->create();
6863
$swooleServer->addProcess($execProcess);
6964
$swooleServer->addProcess($timerProcess);
70-
echo "init process done."."\n";
7165
}
7266

7367
}

src/Listener/PipeMessageHandler.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Swoft\CronTask\Listener;
4+
5+
use Swoft\CronTask\PipeMessage;
6+
use Swoft\Event\Annotation\Mapping\Listener;
7+
use Swoft\Event\EventHandlerInterface;
8+
use Swoft\Server\Swoole\SwooleEvent;
9+
use Swoft\Event\EventInterface;
10+
use Swoft\Task\Task;
11+
12+
/**
13+
* The pipe message listener
14+
*
15+
* @Listener(event=SwooleEvent::PIPE_MESSAGE)
16+
*/
17+
class PipeMessageHandler implements EventHandlerInterface
18+
{
19+
20+
public function handle(EventInterface $event): void
21+
{
22+
$params = $event->getParams();
23+
if (count($params) < 3) {
24+
return;
25+
}
26+
list($server, $srcWorkerId, $data) = $params;
27+
$message = PipeMessage::unpack($data);
28+
$type = $message['type'];
29+
$taskName = $message['message']['name'];
30+
$methodName = $message['message']['method'];
31+
// delever task
32+
Task::async($taskName, $methodName);
33+
34+
}
35+
}

src/PipeMessage.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Swoft\CronTask;
4+
5+
6+
use Swoft\Stdlib\Helper\JsonHelper;
7+
8+
/**
9+
* Pipe message
10+
*/
11+
class PipeMessage
12+
{
13+
/**
14+
* Task message type
15+
*/
16+
const MESSAGE_TYPE_TASK = 'task';
17+
18+
19+
/**
20+
* @param string $type
21+
* @param array $data
22+
*
23+
* @return string
24+
*/
25+
public static function pack(string $type, array $data): string
26+
{
27+
$data = [
28+
'type' => $type,
29+
'message' => $data,
30+
];
31+
return JsonHelper::encode($data, JSON_UNESCAPED_UNICODE);
32+
}
33+
34+
/**
35+
* @param string $message
36+
*
37+
* @return array
38+
*/
39+
public static function unpack(string $message): array
40+
{
41+
$messageAry = json_decode($message, true);
42+
return $messageAry;
43+
}
44+
45+
}

src/Process/CronExecProcess.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function handle(Process $process)
3434
if (!empty($tasks)) {
3535
foreach ($tasks as $task) {
3636
// Diliver task
37-
Task::async($task['task'], $task['taskMethod']);
37+
$cron->deliverTask($task['task'], $task['taskMethod']);
3838
$cron->finishTask($task['key']);
3939
}
4040
}

src/Swoole/PipeMessageListener.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types=1);
2+
3+
4+
namespace Swoft\CronTask\Swoole;
5+
6+
7+
use function go;
8+
use Swoft;
9+
use Swoft\Server\Swoole\PipeMessageInterface;
10+
use Swoft\Bean\Annotation\Mapping\Bean;
11+
use Swoft\Server\Swoole\SwooleEvent;
12+
use Swoole\Server;
13+
14+
/**
15+
* Class PipeMessageListener
16+
*
17+
* @since 2.0
18+
*
19+
* @Bean()
20+
*/
21+
class PipeMessageListener implements PipeMessageInterface
22+
{
23+
/**
24+
* @param Server $server
25+
* @param int $taskId
26+
* @param string $data
27+
*/
28+
public function onPipeMessage(Server $server, int $srcWorkerId, $message): void
29+
{
30+
go(function () use ($server, $srcWorkerId, $message) {
31+
Swoft::trigger(SwooleEvent::PIPE_MESSAGE, null, $server, $srcWorkerId, $message);
32+
});
33+
}
34+
}

test/bootstrap.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

test/case/RendererTest.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

test/fixture/layout.php

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)