-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBotDev.php
161 lines (128 loc) · 4.24 KB
/
BotDev.php
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/** @noinspection FileClassnameCaseInspection */
use Core\HMR\HotDirectory;
use React\EventLoop\Loop;
use React\EventLoop\TimerInterface;
use React\Promise\Promise;
use React\Promise\PromiseInterface;
use function React\Async\await;
require_once __DIR__ . '/vendor/autoload.php';
class Command
{
protected const TEMP = __DIR__ . '/temp';
private array $process = [];
private int $stdoutPos = 0;
private int $stderrPos = 0;
public function __construct(
public readonly string $command,
public readonly array $args = [],
) {
}
public function create(string $command, array $args = []): self
{
return new self($command, $args);
}
private function procExecute(string $command): array
{
$stdout = tempnam(sys_get_temp_dir(), 'dphp');
$stderr = tempnam(sys_get_temp_dir(), 'dphp');
$process = proc_open(
$command,
[
1 => ['file', $stdout, 'w'],
2 => ['file', $stderr, 'w'],
],
$pipes
);
return [
'files' => [$stdout, $stderr],
'command' => $command,
'process' => &$process,
];
}
public function execute(): PromiseInterface
{
if ($this->isRunning()) {
throw new LogicException('Command is already running');
}
return new Promise(function ($resolve, $reject) {
$this->process = $this->procExecute($this->command . ' ' . implode(' ', $this->args));
Loop::addPeriodicTimer(
1,
function (TimerInterface $timer) use (&$stdout, &$stderr, $reject, $resolve) {
$status = proc_get_status($this->process['process']);
$stdout = file_get_contents($this->process['files'][0]);
$stderr = file_get_contents($this->process['files'][1]);
if ($status['running']) {
if ($this->stdoutPos < strlen($stdout)) {
echo substr($stdout, $this->stdoutPos);
$this->stdoutPos = strlen($stdout);
}
if ($this->stderrPos < strlen($stderr)) {
echo substr($stderr, $this->stderrPos);
$this->stderrPos = strlen($stderr);
}
return;
}
if ($status['exitcode'] !== 0) {
$reject([$stderr, $this->command, $this->args]);
} else {
$resolve($stdout, $stderr);
}
$this->process = [];
Loop::cancelTimer($timer);
}
);
});
}
public function isRunning(): bool
{
return $this->process !== [];
}
public function getProcess(): array
{
return $this->process;
}
public function kill(): void
{
if (!$this->isRunning()) {
return;
}
$this->stdoutPos = $this->stderrPos = 0;
proc_terminate($this->process['process']);
}
}
$directory = new HotDirectory(__DIR__);
$restart = static function () use (&$command) {
$command ??= new Command('php', ['Bot.php']);
$time = date('H:i:s');
echo "\nRestarting bot ({$time})...\n";
$command->kill();
await(new Promise(static function ($resolve, $reject) use (&$command) {
Loop::addPeriodicTimer(2, static function (TimerInterface $timer) use (&$command, $resolve) {
if ($command->isRunning()) {
echo "Command is still running\n";
return;
}
try {
Loop::cancelTimer($timer);
} catch (Throwable $e) {
}
$resolve();
});
}));
try {
$command->execute();
} catch (Throwable $e) {
echo "Error: {$e->getMessage()}\n";
}
};
$restart();
$directory->on(HotDirectory::EVENT_FILE_ADDED, $restart(...));
$directory->on(HotDirectory::EVENT_FILE_CHANGED, $restart(...));
$directory->on(HotDirectory::EVENT_FILE_REMOVED, $restart(...));
try {
Loop::run();
} catch (Throwable $e) {
Loop::run();
}