Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
33 / 33 |
TrackerPull | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
33 / 33 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
6 / 6 |
|||
handle | |
100.00% |
1 / 1 |
3 | |
100.00% |
24 / 24 |
|||
getConnections | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\Collect\Console\Commands; |
4 | |
5 | use Exception; |
6 | use Illuminate\Support\Collection; |
7 | use Qmp\Laravel\CommandsLaravel\Middleware\Core\Middleware; |
8 | use Qmp\Laravel\CommandsLaravel\Middleware\Library\Monitoring\{CreateParentProcess, UpdateParentProcess}; |
9 | use Qmp\Laravel\MicroService\Client\Client; |
10 | use Qmp\Laravel\MicroService\Client\Tools\Request as ClientRequest; |
11 | use Qmp\Laravel\AsyncProcessFactory\Traits\ProcessFactory; |
12 | use Qmp\Laravel\ToolsLaravel\Traits\Timer; |
13 | |
14 | class TrackerPull extends \Commando |
15 | { |
16 | use ProcessFactory, Timer; |
17 | |
18 | /** |
19 | * The name and signature of the console command. |
20 | * |
21 | * @var string |
22 | */ |
23 | protected $signature = 'collect:tracker-pull {--launch=run : The processes launch type. "run" to launch one by one, "start" to launch in parralel }'; |
24 | |
25 | /** |
26 | * The console command description. |
27 | * |
28 | * @var string |
29 | */ |
30 | protected $description = 'Pull all data from trackers'; |
31 | |
32 | /** |
33 | |
34 | /** |
35 | * The insert message |
36 | * |
37 | * @var string |
38 | */ |
39 | protected $messageInsert = "<info>Inserting %s entries from chunk #%s ... </info> "; |
40 | |
41 | /** |
42 | * The delete message |
43 | * |
44 | * @var string |
45 | */ |
46 | protected $messageDelete = "<info>Deleting chunk #%s on tracker ... </info>"; |
47 | |
48 | /** |
49 | * Launch type |
50 | * |
51 | * @var [type] |
52 | */ |
53 | protected $launch; |
54 | |
55 | /** |
56 | * Class Constructor |
57 | */ |
58 | public function __construct() |
59 | { |
60 | parent::__construct(); |
61 | |
62 | $this->createProcessFactory('collect:tracker-pull-process') |
63 | ->addEnvs(Middleware::IS_SYSTEM); |
64 | |
65 | $this->middleware([ |
66 | CreateParentProcess::class, |
67 | UpdateParentProcess::class |
68 | ]); |
69 | } |
70 | |
71 | /** |
72 | * Execute the console command. |
73 | * |
74 | * @return void |
75 | * @throws Exception |
76 | */ |
77 | public function handle(): void |
78 | { |
79 | $this->startTimer('total'); |
80 | |
81 | $data = $this->middlewareData(UpdateParentProcess::class) |
82 | ->transfert(CreateParentProcess::class, ['id']); |
83 | |
84 | try { |
85 | if (!in_array($this->option('launch'), ['run', 'start'])) { |
86 | $this->line(''); |
87 | $this->error('Invalid --launch option !'); |
88 | $this->error('Abort ...'); |
89 | $this->line(''); |
90 | $this->table(['Authorized values'], [['--launch=start'], ['--launch=run']]); |
91 | $this->line(''); |
92 | return; |
93 | } |
94 | |
95 | $this->launch = $this->option('launch') . 'Processes'; |
96 | |
97 | $this->getConnections()->each(function ($connection) use ($data) { |
98 | $this->addProcess([$connection->get('id'), $data->id]); |
99 | }); |
100 | |
101 | $this->{$this->launch}(function ($t) { |
102 | $this->output->writeln('<info>' . $t . '</info>'); |
103 | }); |
104 | |
105 | $data->log = [ |
106 | 'duration' => $this->getTimer('total'), |
107 | ]; |
108 | |
109 | $this->error('Total Time : ' . $data->log['duration']); |
110 | } catch (Exception $e) { |
111 | $data->error = $this->reportException($e); |
112 | } |
113 | } |
114 | |
115 | |
116 | /** |
117 | * Get all trackers credentials |
118 | * |
119 | * @return Collection |
120 | */ |
121 | protected function getConnections(): Collection |
122 | { |
123 | $request = ClientRequest::createObject('service_tracker', "config/"); |
124 | $clientResponse = Client::systemSend('get', $request); |
125 | |
126 | return collect($clientResponse->content)->where('active', 1)->recursive(); |
127 | } |
128 | } |