Skip to content

Commit cfd2460

Browse files
committed
fix: send output from running console commands to logger
1 parent 88bd21a commit cfd2460

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

src/Lambda/Handler/ConsoleCommandLambdaEventHandler.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,28 @@
1818
use Ymir\Runtime\Lambda\InvocationEvent\InvocationEventInterface;
1919
use Ymir\Runtime\Lambda\Response\ProcessResponse;
2020
use Ymir\Runtime\Lambda\Response\ResponseInterface;
21+
use Ymir\Runtime\Logger;
2122

2223
/**
2324
* Lambda invocation event handler for console commands.
2425
*/
2526
class ConsoleCommandLambdaEventHandler implements LambdaEventHandlerInterface
2627
{
28+
/**
29+
* The logger that sends logs to CloudWatch.
30+
*
31+
* @var Logger
32+
*/
33+
private $logger;
34+
35+
/**
36+
* Constructor.
37+
*/
38+
public function __construct(Logger $logger)
39+
{
40+
$this->logger = $logger;
41+
}
42+
2743
/**
2844
* {@inheritdoc}
2945
*/
@@ -43,7 +59,9 @@ public function handle(InvocationEventInterface $event): ResponseInterface
4359

4460
$process = Process::fromShellCommandline("{$event->getCommand()} 2>&1");
4561
$process->setTimeout(null);
46-
$process->run();
62+
$process->run(function ($type, $output) {
63+
$this->logger->info($output);
64+
});
4765

4866
return new ProcessResponse($process);
4967
}

src/Runtime.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public static function createFromEnvironmentVariable(): self
120120
new LambdaEventHandlerCollection($logger, [
121121
new PingLambdaEventHandler(),
122122
new WarmUpEventHandler(new LambdaClient(['region' => $region], null, null, $logger)),
123-
new ConsoleCommandLambdaEventHandler(),
123+
new ConsoleCommandLambdaEventHandler($logger),
124124
new WordPressLambdaEventHandler($logger, $phpFpmProcess, $rootDirectory),
125125
new BedrockLambdaEventHandler($logger, $phpFpmProcess, $rootDirectory),
126126
new PhpScriptLambdaEventHandler($logger, $phpFpmProcess, $rootDirectory, getenv('_HANDLER') ?: 'index.php'),

tests/Unit/Lambda/Handler/ConsoleCommandLambdaEventHandlerTest.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Ymir\Runtime\Lambda\Response\ProcessResponse;
1919
use Ymir\Runtime\Tests\Mock\ConsoleCommandEventMockTrait;
2020
use Ymir\Runtime\Tests\Mock\InvocationEventInterfaceMockTrait;
21+
use Ymir\Runtime\Tests\Mock\LoggerMockTrait;
2122

2223
/**
2324
* @covers \Ymir\Runtime\Lambda\Handler\ConsoleCommandLambdaEventHandler
@@ -26,31 +27,31 @@ class ConsoleCommandLambdaEventHandlerTest extends TestCase
2627
{
2728
use ConsoleCommandEventMockTrait;
2829
use InvocationEventInterfaceMockTrait;
30+
use LoggerMockTrait;
2931

3032
public function testCanHandlePingEventType()
3133
{
32-
$handler = new ConsoleCommandLambdaEventHandler();
33-
34-
$this->assertTrue($handler->canHandle($this->getConsoleCommandEventMock()));
34+
$this->assertTrue((new ConsoleCommandLambdaEventHandler($this->getLoggerMock()))->canHandle($this->getConsoleCommandEventMock()));
3535
}
3636

3737
public function testCanHandleWrongEventType()
3838
{
39-
$handler = new ConsoleCommandLambdaEventHandler();
40-
41-
$this->assertFalse($handler->canHandle($this->getInvocationEventInterfaceMock()));
39+
$this->assertFalse((new ConsoleCommandLambdaEventHandler($this->getLoggerMock()))->canHandle($this->getInvocationEventInterfaceMock()));
4240
}
4341

4442
public function testHandleWithSuccessfulCommand()
4543
{
4644
$event = $this->getConsoleCommandEventMock();
47-
$handler = new ConsoleCommandLambdaEventHandler();
45+
$logger = $this->getLoggerMock();
4846

4947
$event->expects($this->once())
5048
->method('getCommand')
5149
->willReturn('ls -la');
5250

53-
$response = $handler->handle($event);
51+
$logger->expects($this->once())
52+
->method('info');
53+
54+
$response = (new ConsoleCommandLambdaEventHandler($logger))->handle($event);
5455
$responseData = $response->getResponseData();
5556

5657
$this->assertInstanceOf(ProcessResponse::class, $response);
@@ -61,13 +62,16 @@ public function testHandleWithSuccessfulCommand()
6162
public function testHandleWithUnsuccessfulCommand()
6263
{
6364
$event = $this->getConsoleCommandEventMock();
64-
$handler = new ConsoleCommandLambdaEventHandler();
65+
$logger = $this->getLoggerMock();
6566

6667
$event->expects($this->once())
6768
->method('getCommand')
6869
->willReturn('foo');
6970

70-
$response = $handler->handle($event);
71+
$logger->expects($this->once())
72+
->method('info');
73+
74+
$response = (new ConsoleCommandLambdaEventHandler($logger))->handle($event);
7175
$responseData = $response->getResponseData();
7276

7377
$this->assertInstanceOf(ProcessResponse::class, $response);
@@ -80,8 +84,6 @@ public function testHandleWithWrongEventType()
8084
$this->expectException(\InvalidArgumentException::class);
8185
$this->expectExceptionMessage('ConsoleCommandLambdaEventHandler can only handle ConsoleCommandEvent objects');
8286

83-
$handler = new ConsoleCommandLambdaEventHandler();
84-
85-
$handler->handle($this->getInvocationEventInterfaceMock());
87+
(new ConsoleCommandLambdaEventHandler($this->getLoggerMock()))->handle($this->getInvocationEventInterfaceMock());
8688
}
8789
}

0 commit comments

Comments
 (0)