-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an in-process executor for console commands; fix a fatal error wi…
…th invalid message body received; echoback command uses the given output for echoing
- Loading branch information
gggeek
committed
Nov 6, 2015
1 parent
ef04a93
commit 313eee0
Showing
8 changed files
with
204 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Kaliop\QueueingBundle\Service; | ||
|
||
use Symfony\Component\Console\Event\ConsoleCommandEvent; | ||
|
||
/** | ||
* Keeps a pointer to the executing Application. NB: even if recursive apps are used, it only keeps a ref to the original one | ||
*/ | ||
class ConsoleEventListener | ||
{ | ||
protected $application; | ||
|
||
public function onConsoleCommand(ConsoleCommandEvent $event) | ||
{ | ||
if ($this->application == null) { | ||
$this->application = $event->getCommand()->getApplication(); | ||
} | ||
} | ||
|
||
public function getCurrentApplication() | ||
{ | ||
return $this->application; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Kaliop\QueueingBundle\Service\MessageConsumer; | ||
|
||
use Symfony\Component\Console\Input\StringInput; | ||
use Symfony\Component\Console\Output\BufferedOutput; | ||
use Kaliop\QueueingBundle\Command\ConsumerCommand; | ||
|
||
/** | ||
* Instead of forking a php process to run a Symfony console command, runs it directly in-process. | ||
* This is expected to be: | ||
* - faster | ||
* - unsafe, as there is no guarantee against memory leaks, stale database connections etc... | ||
* | ||
* NB: the the Application HAS to be injected into this message consumer or a fatal error will be thrown | ||
*/ | ||
class InProcessConsoleCommand extends ConsoleCommand | ||
{ | ||
protected function runCommand($consoleCommand, $arguments = array(), $options = array()) | ||
{ | ||
$input = new StringInput($this->buildCommandString($consoleCommand, $arguments, $options)); | ||
|
||
$label = trim(ConsumerCommand::getLabel()); | ||
if ($label != '') { | ||
$label = " '$label'"; | ||
} | ||
|
||
if ($this->logger) { | ||
$this->logger->debug("console command will be executed in-process from MessageConsumer{$label}: " . (string)$input); | ||
} | ||
|
||
$kernel = $this->application->getKernel(); | ||
// q: is this helpful / needed ? | ||
//$kernel->shutdown(); | ||
//$kernel->boot(); | ||
|
||
$applicationClass = get_class($this->application); | ||
$app = new $applicationClass($kernel); | ||
$app->setAutoExit(false); | ||
|
||
$output = new BufferedOutput(); | ||
$retCode = $app->run($input, $output); | ||
|
||
$results = array($retCode, $output->fetch(), ''); | ||
|
||
if ($retCode != 0 && $this->logger) { | ||
$this->logger->error( | ||
"Console command executed in-process from MessageConsumer{$label} failed. Retcode: $retCode, Output: '" . trim($results[1]) . "'", | ||
array()); | ||
} | ||
|
||
return $results; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters