|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the reliforp/reli-prof package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) sji <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Reli\Inspector\Daemon\Reader\Controller; |
|
15
|
|
|
|
|
16
|
|
|
use Reli\Inspector\Daemon\AutoContextRecoveringInterface; |
|
17
|
|
|
use Reli\Inspector\Daemon\Dispatcher\TargetProcessDescriptor; |
|
18
|
|
|
use Reli\Inspector\Daemon\Reader\Protocol\Message\DetachWorkerMessage; |
|
19
|
|
|
use Reli\Inspector\Daemon\Reader\Protocol\Message\TraceMessage; |
|
20
|
|
|
use Reli\Inspector\Daemon\Reader\Protocol\Message\AttachMessage; |
|
21
|
|
|
use Reli\Inspector\Daemon\Reader\Protocol\Message\SetSettingsMessage; |
|
22
|
|
|
use Reli\Inspector\Daemon\Reader\Protocol\PhpReaderControllerProtocolInterface; |
|
23
|
|
|
use Reli\Inspector\Settings\GetTraceSettings\GetTraceSettings; |
|
24
|
|
|
use Reli\Inspector\Settings\TraceLoopSettings\TraceLoopSettings; |
|
25
|
|
|
|
|
26
|
|
|
final class PhpReaderController implements PhpReaderControllerInterface |
|
27
|
|
|
{ |
|
28
|
|
|
private ?SetSettingsMessage $settings_already_sent = null; |
|
29
|
|
|
private ?AttachMessage $attach_already_sent = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param AutoContextRecoveringInterface<PhpReaderControllerProtocol> $auto_context_recovering |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct( |
|
35
|
|
|
private AutoContextRecoveringInterface $auto_context_recovering, |
|
36
|
|
|
) { |
|
37
|
|
|
$this->auto_context_recovering->onRecover( |
|
38
|
|
|
function () { |
|
39
|
|
|
if ($this->settings_already_sent !== null) { |
|
40
|
|
|
$this->auto_context_recovering |
|
41
|
|
|
->getContext() |
|
42
|
|
|
->getProtocol() |
|
43
|
|
|
->sendSettings($this->settings_already_sent) |
|
44
|
|
|
; |
|
45
|
|
|
} |
|
46
|
|
|
if ($this->attach_already_sent !== null) { |
|
47
|
|
|
$this->auto_context_recovering |
|
48
|
|
|
->getContext() |
|
49
|
|
|
->getProtocol() |
|
50
|
|
|
->sendAttach($this->attach_already_sent) |
|
51
|
|
|
; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function start(): void |
|
58
|
|
|
{ |
|
59
|
|
|
$this->auto_context_recovering->getContext()->start(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function isRunning(): bool |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->auto_context_recovering->getContext()->isRunning(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function sendSettings( |
|
68
|
|
|
TraceLoopSettings $loop_settings, |
|
69
|
|
|
GetTraceSettings $get_trace_settings |
|
70
|
|
|
): void { |
|
71
|
|
|
$settings_message = new SetSettingsMessage( |
|
72
|
|
|
$loop_settings, |
|
73
|
|
|
$get_trace_settings |
|
74
|
|
|
); |
|
75
|
|
|
$this->auto_context_recovering->withAutoRecover( |
|
76
|
|
|
function (PhpReaderControllerProtocolInterface $protocol) use ($settings_message) { |
|
77
|
|
|
$protocol->sendSettings($settings_message); |
|
78
|
|
|
$this->settings_already_sent = $settings_message; |
|
79
|
|
|
}, |
|
80
|
|
|
'failed on sending settings to worker' |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function sendAttach(TargetProcessDescriptor $process_descriptor): void |
|
85
|
|
|
{ |
|
86
|
|
|
$attach_message = new AttachMessage($process_descriptor); |
|
87
|
|
|
$this->auto_context_recovering->withAutoRecover( |
|
88
|
|
|
function (PhpReaderControllerProtocolInterface $protocol) use ($attach_message) { |
|
89
|
|
|
$protocol->sendAttach($attach_message); |
|
90
|
|
|
$this->attach_already_sent = $attach_message; |
|
91
|
|
|
}, |
|
92
|
|
|
'failed on attaching worker' |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function receiveTraceOrDetachWorker(): TraceMessage|DetachWorkerMessage |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->auto_context_recovering->withAutoRecover( |
|
99
|
|
|
function (PhpReaderControllerProtocolInterface $protocol): TraceMessage|DetachWorkerMessage { |
|
100
|
|
|
$message = $protocol->receiveTraceOrDetachWorker(); |
|
101
|
|
|
if ($message instanceof DetachWorkerMessage) { |
|
102
|
|
|
$this->attach_already_sent = null; |
|
103
|
|
|
} |
|
104
|
|
|
return $message; |
|
105
|
|
|
}, |
|
106
|
|
|
'failed to receive trace or detach worker' |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|