Skip to content

Commit c0114bf

Browse files
authored
Merge pull request #105 from craftcms/job-sqs-handler
Job SQS Handler
2 parents 23d4667 + 687e39e commit c0114bf

File tree

5 files changed

+58
-7
lines changed

5 files changed

+58
-7
lines changed

src/bref/craft/CraftCliEntrypoint.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace craft\cloud\bref\craft;
44

5+
use Bref\Context\Context;
56
use craft\cloud\AppConfig;
67
use craft\cloud\queue\SqsQueue;
78
use craft\cloud\runtime\event\EventHandler;
@@ -37,8 +38,22 @@ private function command(string $command, array $environment, int $timeout): arr
3738
];
3839
}
3940

40-
public function lambdaCommand(string $command, array $environment)
41+
public function lambdaCommand(string $command, Context $context): array
4142
{
43+
$environment = $this->invocationContext($context);
44+
4245
return $this->command($command, $environment, self::LAMBDA_EXECUTION_LIMIT);
4346
}
47+
48+
public function craftJob(string $jobId, Context $context): array
49+
{
50+
$environment = $this->invocationContext($context);
51+
52+
return $this->command("cloud/queue/exec $jobId", $environment, self::LAMBDA_EXECUTION_LIMIT);
53+
}
54+
55+
private function invocationContext(Context $context): array
56+
{
57+
return ['LAMBDA_INVOCATION_CONTEXT' => json_encode($context, JSON_THROW_ON_ERROR)];
58+
}
4459
}

src/bref/handlers/CommandHandler.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ public function handle(mixed $event, Context $context): array
2323

2424
$command = $event['command'];
2525

26-
$environment = ['LAMBDA_INVOCATION_CONTEXT' => json_encode($context, JSON_THROW_ON_ERROR)];
27-
2826
$entrypoint = new CraftCliEntrypoint();
2927

30-
return $entrypoint->lambdaCommand($command, $environment);
28+
return $entrypoint->lambdaCommand($command, $context);
3129
}
3230
}

src/bref/handlers/CommandSqsHandler.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ public function handleSqs(SqsEvent $event, Context $context): void
4242
public function runCommand(string $command, Context $context): array
4343
{
4444
try {
45-
$environment = ['LAMBDA_INVOCATION_CONTEXT' => json_encode($context, JSON_THROW_ON_ERROR)];
46-
47-
return $this->entrypoint->lambdaCommand($command, $environment);
45+
return $this->entrypoint->lambdaCommand($command, $context);
4846
} catch (Throwable $t) {
4947
return [
5048
'exit_code' => 1,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace craft\cloud\bref\handlers;
4+
5+
use Bref\Context\Context;
6+
use Bref\Event\Sqs\SqsEvent;
7+
use Bref\Event\Sqs\SqsHandler;
8+
use craft\cloud\bref\craft\CraftCliEntrypoint;
9+
use RuntimeException;
10+
11+
/**
12+
* @internal
13+
*/
14+
final class JobSqsHandler extends SqsHandler
15+
{
16+
private CraftCliEntrypoint $entrypoint;
17+
18+
public function __construct()
19+
{
20+
$this->entrypoint = new CraftCliEntrypoint();
21+
}
22+
23+
public function handleSqs(SqsEvent $event, Context $context): void
24+
{
25+
foreach ($event->getRecords() as $record) {
26+
$message = $record->getBody();
27+
28+
$payload = json_decode($message, associative: true, flags: JSON_THROW_ON_ERROR);
29+
30+
$jobId = $payload['jobId'] ?? throw new RuntimeException("Job ID not found. Message: [$message]");
31+
32+
$this->entrypoint->craftJob($jobId, $context);
33+
}
34+
}
35+
}

src/bref/handlers/job-sqs-cmd.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
require __DIR__ . '/../../../../../autoload.php';
4+
5+
return new \craft\cloud\bref\handlers\JobSqsHandler();

0 commit comments

Comments
 (0)