-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseTask.php
More file actions
61 lines (51 loc) · 1.38 KB
/
BaseTask.php
File metadata and controls
61 lines (51 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace WebmanTech\CrontabTask;
use Throwable;
use WebmanTech\CrontabTask\Exceptions\TaskException;
use WebmanTech\CrontabTask\Exceptions\TaskExceptionInterface;
use WebmanTech\CrontabTask\Traits\LogTrait;
use WebmanTech\CrontabTask\Traits\TaskAutoFreeMemoryTrait;
use WebmanTech\CrontabTask\Traits\TaskEventTrait;
abstract class BaseTask
{
use LogTrait;
use TaskEventTrait;
use TaskAutoFreeMemoryTrait;
final public function __construct()
{
$this->initEvents();
}
/**
* 定时任务的执行入口
* @return void
*/
public static function taskExec()
{
$self = new static();
try {
$self->log('start');
$self->fireBeforeEvent();
$self->handle();
} catch (Throwable $e) {
if ($e instanceof TaskExceptionInterface) {
$self->log('TaskException:' . $e->getMessage() . $e->getDataAsString(), 'warning');
return;
}
$self->log($e, 'error');
return;
} finally {
$self->fireAfterEvent();
if ($self->isAutoFreeMemory()) {
$self->freeMemory();
}
$self->log('end');
}
}
/**
* 真实业务
* @return void
* @throws TaskException
* @throws Throwable
*/
abstract public function handle();
}