|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of YaEtl |
|
5
|
|
|
* (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl |
|
6
|
|
|
* This source file is licensed under the MIT license which you will |
|
7
|
|
|
* find in the LICENSE file or at https://opensource.org/licenses/MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace fab2s\YaEtl\Laravel\Callbacks; |
|
11
|
|
|
|
|
12
|
|
|
use fab2s\NodalFlow\Callbacks\CallbackAbstract; |
|
13
|
|
|
use fab2s\NodalFlow\Flows\FlowInterface; |
|
14
|
|
|
use fab2s\NodalFlow\Nodes\NodeInterface; |
|
15
|
|
|
use Illuminate\Console\Command; |
|
16
|
|
|
use Illuminate\Console\OutputStyle; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class ProgressCallback |
|
20
|
|
|
*/ |
|
21
|
|
|
class ProgressCallback extends CallbackAbstract |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* The Laravel Command object, as it does not make sense |
|
25
|
|
|
* to display CLI progress otherwise |
|
26
|
|
|
* |
|
27
|
|
|
* @var Command |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $command; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The Laravel output object, extracted from the command object |
|
33
|
|
|
* |
|
34
|
|
|
* @var OutputStyle |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $output; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The number of records |
|
40
|
|
|
* |
|
41
|
|
|
* @var int |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $numRecords = 0; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Progress modulo, should align with YaEtl's one |
|
47
|
|
|
* |
|
48
|
|
|
* @var int |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $progressMod = 1; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Set progress modulo |
|
54
|
|
|
* |
|
55
|
|
|
* @param int $progressMod |
|
56
|
|
|
* |
|
57
|
|
|
* @return static |
|
58
|
|
|
*/ |
|
59
|
|
|
public function setProgressMod(int $progressMod): self |
|
60
|
|
|
{ |
|
61
|
|
|
$this->progressMod = max(1, (int) $progressMod); |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Set the total number of records prior to FLow execution |
|
68
|
|
|
* |
|
69
|
|
|
* @param int $numRecords |
|
70
|
|
|
* |
|
71
|
|
|
* @return static |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setNumRecords(int $numRecords): self |
|
74
|
|
|
{ |
|
75
|
|
|
$this->numRecords = max(0, $numRecords); |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Set Laravel's Command |
|
82
|
|
|
* |
|
83
|
|
|
* @param Command $command |
|
84
|
|
|
* |
|
85
|
|
|
* @return static |
|
86
|
|
|
*/ |
|
87
|
|
|
public function setCommand(Command $command): self |
|
88
|
|
|
{ |
|
89
|
|
|
$this->command = $command; |
|
90
|
|
|
|
|
91
|
|
|
$this->output = $this->command->getOutput(); |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Triggered when a Flow starts |
|
98
|
|
|
* |
|
99
|
|
|
* @param FlowInterface $flow |
|
100
|
|
|
*/ |
|
101
|
|
|
public function start(FlowInterface $flow) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->command->info('[YaEtl] Start'); |
|
104
|
|
|
$this->output->progressStart($this->numRecords); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Triggered when a Flow progresses, |
|
109
|
|
|
* eg exec once or generates once |
|
110
|
|
|
* |
|
111
|
|
|
* @param FlowInterface $flow |
|
112
|
|
|
* @param NodeInterface $node |
|
113
|
|
|
*/ |
|
114
|
|
|
public function progress(FlowInterface $flow, NodeInterface $node) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->output->progressAdvance($this->progressMod); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Triggered when a Flow succeeds |
|
121
|
|
|
* |
|
122
|
|
|
* @param FlowInterface $flow |
|
123
|
|
|
*/ |
|
124
|
|
|
public function success(FlowInterface $flow) |
|
125
|
|
|
{ |
|
126
|
|
|
$this->output->progressFinish(); |
|
127
|
|
|
|
|
128
|
|
|
$flowStatus = $flow->getFlowStatus(); |
|
129
|
|
|
if ($flowStatus->isDirty()) { |
|
130
|
|
|
$this->command->warn('[YaEtl] Dirty Success'); |
|
131
|
|
|
} else { |
|
132
|
|
|
$this->command->info('[YaEtl] Clean Success'); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Triggered when a Flow fails |
|
138
|
|
|
* |
|
139
|
|
|
* @param FlowInterface $flow |
|
140
|
|
|
*/ |
|
141
|
|
|
public function fail(FlowInterface $flow) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->command->error('[YaEtl] Failed'); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|