Skip to content

Commit 3c36c97

Browse files
authored
Update ConsumeCommand.php
1 parent ab21031 commit 3c36c97

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/Console/ConsumeCommand.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ final class ConsumeCommand extends WorkCommand
2929
{--consumer-tag}
3030
{--prefetch-size=0}
3131
{--prefetch-count=1000}
32+
{--num-processes=2 : Number of processes to run in parallel}
3233
';
3334

3435
protected $description = 'Consume messages';
@@ -37,15 +38,35 @@ final class ConsumeCommand extends WorkCommand
3738
public function handle(): int|null
3839
{
3940
$consumer = $this->worker;
41+
$numProcesses = $this->option('num-processes');
4042

4143
$consumer->setContainer($this->laravel);
4244
$consumer->setName($this->option('name'));
4345
$consumer->setConsumerTag($this->consumerTag());
4446
$consumer->setMaxPriority((int) $this->option('max-priority'));
4547
$consumer->setPrefetchSize((int) $this->option('prefetch-size'));
4648
$consumer->setPrefetchCount((int) $this->option('prefetch-count'));
49+
50+
for ($i = 0; $i < $numProcesses; $i++) {
51+
$pid = pcntl_fork();
4752

48-
parent::handle();
53+
if ($pid === -1) {
54+
// Error handling
55+
echo "Could not fork process \n";
56+
exit(1);
57+
} elseif ($pid === 0) {
58+
// This is the child process
59+
$this->consume();
60+
exit(0);
61+
}
62+
}
63+
64+
// Wait for all child processes to finish
65+
while (pcntl_waitpid(0, $status) !== -1) {
66+
// Handle exit status if needed
67+
}
68+
69+
return 0;;
4970
}
5071

5172
private function consumerTag(): string

0 commit comments

Comments
 (0)