Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.7] Fix an issue where the worker process would not be killed by the listener when the timeout is exceeded #25981

Merged
merged 4 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 34 additions & 49 deletions src/Illuminate/Queue/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Illuminate\Queue;

use Closure;
use Illuminate\Support\ProcessUtils;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\PhpExecutableFinder;

Expand Down Expand Up @@ -37,13 +36,6 @@ class Listener
*/
protected $maxTries = 0;

/**
* The queue worker command line.
*
* @var string
*/
protected $workerCommand;

/**
* The output handler callback.
*
Expand All @@ -60,19 +52,6 @@ class Listener
public function __construct($commandPath)
{
$this->commandPath = $commandPath;
$this->workerCommand = $this->buildCommandTemplate();
}

/**
* Build the environment specific worker command.
*
* @return string
*/
protected function buildCommandTemplate()
{
$command = 'queue:work %s --once --queue=%s --delay=%s --memory=%s --sleep=%s --tries=%s';

return "{$this->phpBinary()} {$this->artisanBinary()} {$command}";
}

/**
Expand All @@ -82,9 +61,7 @@ protected function buildCommandTemplate()
*/
protected function phpBinary()
{
return ProcessUtils::escapeArgument(
(new PhpExecutableFinder)->find(false)
);
return (new PhpExecutableFinder)->find(false);
}

/**
Expand All @@ -94,9 +71,7 @@ protected function phpBinary()
*/
protected function artisanBinary()
{
return defined('ARTISAN_BINARY')
? ProcessUtils::escapeArgument(ARTISAN_BINARY)
: ProcessUtils::escapeArgument('artisan');
return defined('ARTISAN_BINARY') ? ARTISAN_BINARY : 'artisan';
}

/**
Expand Down Expand Up @@ -126,24 +101,28 @@ public function listen($connection, $queue, ListenerOptions $options)
*/
public function makeProcess($connection, $queue, ListenerOptions $options)
{
$command = $this->workerCommand;
// First, we will just create the worker commands with all of the various
// options available for the command. This will produce the final command
// line array that we will pass into a Symfony process object for processing.
$command = $this->createCommand(
$connection,
$queue,
$options
);

// If the environment is set, we will append it to the command string so the
// If the environment is set, we will append it to the command array so the
// workers will run under the specified environment. Otherwise, they will
// just run under the production environment which is not always right.
if (isset($options->environment)) {
$command = $this->addEnvironment($command, $options);
}

// Next, we will just format out the worker commands with all of the various
// options available for the command. This will produce the final command
// line that we will pass into a Symfony process object for processing.
$command = $this->formatCommand(
$command, $connection, $queue, $options
);

return new Process(
$command, $this->commandPath, null, null, $options->timeout
$command,
$this->commandPath,
null,
null,
$options->timeout
);
}

Expand All @@ -152,31 +131,37 @@ public function makeProcess($connection, $queue, ListenerOptions $options)
*
* @param string $command
* @param \Illuminate\Queue\ListenerOptions $options
* @return string
* @return array
*/
protected function addEnvironment($command, ListenerOptions $options)
{
return $command.' --env='.ProcessUtils::escapeArgument($options->environment);
return array_merge($command, ["--env={$options->environment}"]);
}

/**
* Format the given command with the listener options.
* Create the command with the listener options.
*
* @param string $command
* @param string $connection
* @param string $queue
* @param \Illuminate\Queue\ListenerOptions $options
* @return string
*/
protected function formatCommand($command, $connection, $queue, ListenerOptions $options)
protected function createCommand($connection, $queue, ListenerOptions $options)
{
return sprintf(
$command,
ProcessUtils::escapeArgument($connection),
ProcessUtils::escapeArgument($queue),
$options->delay, $options->memory,
$options->sleep, $options->maxTries
);
return array_filter([
$this->phpBinary(),
$this->artisanBinary(),
'queue:work',
$connection,
'--once',
"--queue={$queue}",
"--delay={$options->delay}",
"--memory={$options->memory}",
"--sleep={$options->sleep}",
"--tries={$options->maxTries}",
], function ($value) {
return ! is_null($value);
});
}

/**
Expand Down
34 changes: 33 additions & 1 deletion tests/Queue/QueueListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,38 @@ public function testMakeProcessCorrectlyFormatsCommandLine()
$this->assertInstanceOf(Process::class, $process);
$this->assertEquals(__DIR__, $process->getWorkingDirectory());
$this->assertEquals(3, $process->getTimeout());
$this->assertEquals($escape.PHP_BINARY.$escape." {$escape}artisan{$escape} queue:work {$escape}connection{$escape} --once --queue={$escape}queue{$escape} --delay=1 --memory=2 --sleep=3 --tries=0", $process->getCommandLine());
$this->assertEquals($escape.PHP_BINARY.$escape." {$escape}artisan{$escape} {$escape}queue:work{$escape} {$escape}connection{$escape} {$escape}--once{$escape} {$escape}--queue=queue{$escape} {$escape}--delay=1{$escape} {$escape}--memory=2{$escape} {$escape}--sleep=3{$escape} {$escape}--tries=0{$escape}", $process->getCommandLine());
}

public function testMakeProcessCorrectlyFormatsCommandLineWithAnEnvironmentSpecified()
{
$listener = new Listener(__DIR__);
$options = new ListenerOptions('test');
$options->delay = 1;
$options->memory = 2;
$options->timeout = 3;
$process = $listener->makeProcess('connection', 'queue', $options);
$escape = '\\' === DIRECTORY_SEPARATOR ? '"' : '\'';

$this->assertInstanceOf(Process::class, $process);
$this->assertEquals(__DIR__, $process->getWorkingDirectory());
$this->assertEquals(3, $process->getTimeout());
$this->assertEquals($escape.PHP_BINARY.$escape." {$escape}artisan{$escape} {$escape}queue:work{$escape} {$escape}connection{$escape} {$escape}--once{$escape} {$escape}--queue=queue{$escape} {$escape}--delay=1{$escape} {$escape}--memory=2{$escape} {$escape}--sleep=3{$escape} {$escape}--tries=0{$escape} {$escape}--env=test{$escape}", $process->getCommandLine());
}

public function testMakeProcessCorrectlyFormatsCommandLineWhenTheConnectionIsNotSpecified()
{
$listener = new Listener(__DIR__);
$options = new ListenerOptions('test');
$options->delay = 1;
$options->memory = 2;
$options->timeout = 3;
$process = $listener->makeProcess(null, 'queue', $options);
$escape = '\\' === DIRECTORY_SEPARATOR ? '"' : '\'';

$this->assertInstanceOf(Process::class, $process);
$this->assertEquals(__DIR__, $process->getWorkingDirectory());
$this->assertEquals(3, $process->getTimeout());
$this->assertEquals($escape.PHP_BINARY.$escape." {$escape}artisan{$escape} {$escape}queue:work{$escape} {$escape}--once{$escape} {$escape}--queue=queue{$escape} {$escape}--delay=1{$escape} {$escape}--memory=2{$escape} {$escape}--sleep=3{$escape} {$escape}--tries=0{$escape} {$escape}--env=test{$escape}", $process->getCommandLine());
}
}