Skip to content

Commit 0817c37

Browse files
committed
fix(worker): correct variable naming conflict in Manager::emitCommand
1 parent 1feb2bf commit 0817c37

File tree

2 files changed

+44
-42
lines changed

2 files changed

+44
-42
lines changed

src/Worker/Manager.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ public function add(Worker $worker): void
7777
*
7878
* @return void
7979
*/
80-
public function emitCommand(Command $command, string $name, int $index): void
80+
public function emitCommand(Command $command, string $workerName, int $index): void
8181
{
8282
switch ($command->name) {
8383
case BaseWorker::COMMAND_RELOAD:
84-
$name = $command->arguments['name'] ?? null;
85-
$this->reload($name);
84+
$workerName = $command->arguments['name'] ?? null;
85+
$this->reload($workerName);
8686
break;
8787

8888
case Manager::COMMAND_COMMAND_TO_WORKER:
@@ -99,27 +99,27 @@ public function emitCommand(Command $command, string $name, int $index): void
9999

100100
case Manager::COMMAND_REFRESH_METADATA:
101101
$metadata = $command->arguments['metadata'];
102-
if (isset($this->process[$name][$index])) {
103-
$this->process[$name][$index]->metadata = $metadata;
102+
if (isset($this->process[$workerName][$index])) {
103+
$this->process[$workerName][$index]->metadata = $metadata;
104104
}
105105
break;
106106

107107
case Manager::COMMAND_SUPERVISOR_METADATA:
108108
$result = [];
109109
foreach ($this->workers as $worker) {
110-
$name = $worker->name;
111-
$result[$name] = [];
110+
$_workerName = $worker->name;
111+
$result[$_workerName] = [];
112112

113-
foreach ($this->process as $workerName => $processes) {
113+
foreach ($this->process as $processes) {
114114
foreach ($processes as $index => $process) {
115-
$result[$name][$workerName][$index] = $process->metadata;
115+
$result[$_workerName][$index] = $process->metadata;
116116
}
117117
}
118118
}
119119

120120
$id = $command->arguments['id'];
121121
$command = Command::make(Manager::COMMAND_SUPERVISOR_METADATA, ['metadata' => $result, 'id' => $id]);
122-
$this->sendToWorker($command, $name, $index);
122+
$this->sendToWorker($command, $workerName, $index);
123123
break;
124124
}
125125
}

src/Worker/Process.php

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,14 @@ private function spawnParent(): void
100100
});
101101

102102
$this->parentStream->watchRead(fn () => go(function () {
103-
$content = $this->parentStream->read(1024);
104-
foreach ($this->zx7e->fill($content) as $string) {
105-
$this->manager->emitCommand(Command::fromString($string), $this->worker->name, $this->index);
106-
}
103+
while ($content = $this->parentStream->read(1024)) {
104+
foreach ($this->zx7e->fill($content) as $string) {
105+
$this->manager->emitCommand(Command::fromString($string), $this->worker->name, $this->index);
106+
}
107107

108-
if ($this->parentStream->eof()) {
109-
$this->parentStream->close();
108+
if ($this->parentStream->eof()) {
109+
$this->parentStream->close();
110+
}
110111
}
111112
}));
112113

@@ -140,7 +141,7 @@ public function reload(): void
140141
*/
141142
public function terminate(): void
142143
{
143-
Scheduler::terminate($this->guard)->resolve();
144+
Scheduler::terminate($this->guard)->unwrap();
144145

145146
try {
146147
$this->send(Command::make(BaseWorker::COMMAND_TERMINATE));
@@ -206,33 +207,34 @@ private static function spawnChild(BaseWorker $worker, Stream $childStream): voi
206207

207208
$childZx7e = new Zx7e();
208209
$childStream->watchRead(static fn () => go(static function () use ($worker, $childStream, &$childZx7e) {
209-
$content = $childStream->read(1024);
210-
foreach ($childZx7e->fill($content) as $string) {
211-
$command = Command::fromString($string);
212-
switch ($command->name) {
213-
case BaseWorker::COMMAND_RELOAD:
214-
$worker->onReload();
215-
break;
216-
217-
case BaseWorker::COMMAND_TERMINATE:
218-
$worker->onTerminate();
219-
break;
220-
221-
case Manager::COMMAND_SUPERVISOR_METADATA:
222-
$id = $command->arguments['id'];
223-
if ($owner = $worker->subs[$id] ?? null) {
224-
unset($worker->subs[$id]);
225-
Scheduler::resume($owner, $command->arguments['metadata']);
226-
}
227-
break;
228-
229-
default:
230-
$worker->onCommand($command);
210+
while ($content = $childStream->read(1024)) {
211+
foreach ($childZx7e->fill($content) as $string) {
212+
$command = Command::fromString($string);
213+
switch ($command->name) {
214+
case BaseWorker::COMMAND_RELOAD:
215+
$worker->onReload();
216+
break;
217+
218+
case BaseWorker::COMMAND_TERMINATE:
219+
$worker->onTerminate();
220+
break;
221+
222+
case Manager::COMMAND_SUPERVISOR_METADATA:
223+
$id = $command->arguments['id'];
224+
if ($owner = $worker->subs[$id] ?? null) {
225+
unset($worker->subs[$id]);
226+
Scheduler::resume($owner, $command->arguments['metadata']);
227+
}
228+
break;
229+
230+
default:
231+
$worker->onCommand($command);
232+
}
231233
}
232-
}
233234

234-
if ($childStream->eof()) {
235-
exit(0);
235+
if ($childStream->eof()) {
236+
exit(0);
237+
}
236238
}
237239
}));
238240

0 commit comments

Comments
 (0)