1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* TaskScheduler |
7
|
|
|
* |
8
|
|
|
* @author Raffael Sahli <[email protected]> |
9
|
|
|
* @copyright Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com) |
10
|
|
|
* @license MIT https://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace TaskScheduler; |
14
|
|
|
|
15
|
|
|
use Closure; |
16
|
|
|
use League\Event\Emitter; |
17
|
|
|
|
18
|
|
|
trait EventsTrait |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Emitter |
22
|
|
|
* |
23
|
|
|
* @var Emitter |
24
|
|
|
*/ |
25
|
|
|
protected $emitter; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Bind event listener |
29
|
|
|
*/ |
30
|
|
|
public function on(string $event, Closure $handler) |
31
|
|
|
{ |
32
|
|
|
if(!in_array($event, Scheduler::VALID_EVENTS)) { |
33
|
|
|
$name = 'taskscheduler.on'.ucfirst($event); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->emitter->addListener($event, $handler); |
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Emit process event |
42
|
|
|
*/ |
43
|
3 |
|
protected function emit(Process $process): bool |
44
|
|
|
{ |
45
|
3 |
|
switch ($process->getStatus()) { |
46
|
3 |
|
case JobInterface::STATUS_WAITING: |
47
|
3 |
|
$this->emitter->emit('taskscheduler.onWaiting', $process); |
48
|
3 |
|
return true; |
49
|
2 |
|
case JobInterface::STATUS_PROCESSING: |
50
|
2 |
|
$this->emitter->emit('taskscheduler.onStart', $process); |
51
|
2 |
|
return true; |
52
|
2 |
|
case JobInterface::STATUS_DONE: |
53
|
1 |
|
$this->emitter->emit('taskscheduler.onDone', $process); |
54
|
1 |
|
return true; |
55
|
1 |
|
case JobInterface::STATUS_POSTPONED: |
56
|
|
|
$this->emitter->emit('taskscheduler.onPostponed', $process); |
57
|
|
|
return true; |
58
|
1 |
|
case JobInterface::STATUS_FAILED: |
59
|
1 |
|
$this->emitter->emit('taskscheduler.onFailed', $process); |
60
|
1 |
|
return true; |
61
|
|
|
case JobInterface::STATUS_TIMEOUT: |
62
|
|
|
$this->emitter->emit('taskscheduler.onTimeout', $process); |
63
|
|
|
return true; |
64
|
|
|
case JobInterface::STATUS_CANCELED: |
65
|
|
|
$this->emitter->emit('taskscheduler.onCancel', $process); |
66
|
|
|
return true; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|