Skip to content
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
17 changes: 16 additions & 1 deletion src/bref/craft/CraftCliEntrypoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace craft\cloud\bref\craft;

use Bref\Context\Context;
use craft\cloud\AppConfig;
use craft\cloud\queue\SqsQueue;
use craft\cloud\runtime\event\EventHandler;
Expand Down Expand Up @@ -37,8 +38,22 @@ private function command(string $command, array $environment, int $timeout): arr
];
}

public function lambdaCommand(string $command, array $environment)
public function lambdaCommand(string $command, Context $context): array
{
$environment = $this->invocationContext($context);

return $this->command($command, $environment, self::LAMBDA_EXECUTION_LIMIT);
}

public function craftJob(string $jobId, Context $context): array
{
$environment = $this->invocationContext($context);

return $this->command("cloud/queue/exec $jobId", $environment, self::LAMBDA_EXECUTION_LIMIT);
}

private function invocationContext(Context $context): array
{
return ['LAMBDA_INVOCATION_CONTEXT' => json_encode($context, JSON_THROW_ON_ERROR)];
}
}
4 changes: 1 addition & 3 deletions src/bref/handlers/CommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ public function handle(mixed $event, Context $context): array

$command = $event['command'];

$environment = ['LAMBDA_INVOCATION_CONTEXT' => json_encode($context, JSON_THROW_ON_ERROR)];

$entrypoint = new CraftCliEntrypoint();

return $entrypoint->lambdaCommand($command, $environment);
return $entrypoint->lambdaCommand($command, $context);
}
}
4 changes: 1 addition & 3 deletions src/bref/handlers/CommandSqsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ public function handleSqs(SqsEvent $event, Context $context): void
public function runCommand(string $command, Context $context): array
{
try {
$environment = ['LAMBDA_INVOCATION_CONTEXT' => json_encode($context, JSON_THROW_ON_ERROR)];

return $this->entrypoint->lambdaCommand($command, $environment);
return $this->entrypoint->lambdaCommand($command, $context);
} catch (Throwable $t) {
return [
'exit_code' => 1,
Expand Down
35 changes: 35 additions & 0 deletions src/bref/handlers/JobSqsHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace craft\cloud\bref\handlers;

use Bref\Context\Context;
use Bref\Event\Sqs\SqsEvent;
use Bref\Event\Sqs\SqsHandler;
use craft\cloud\bref\craft\CraftCliEntrypoint;
use RuntimeException;

/**
* @internal
*/
final class JobSqsHandler extends SqsHandler
{
private CraftCliEntrypoint $entrypoint;

public function __construct()
{
$this->entrypoint = new CraftCliEntrypoint();
}

public function handleSqs(SqsEvent $event, Context $context): void
{
foreach ($event->getRecords() as $record) {
$message = $record->getBody();

$payload = json_decode($message, associative: true, flags: JSON_THROW_ON_ERROR);

$jobId = $payload['jobId'] ?? throw new RuntimeException("Job Id not found. Message: [$message]");

$this->entrypoint->craftJob($jobId, $context);
}
}
}
5 changes: 5 additions & 0 deletions src/bref/handlers/job-sqs-cmd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

require __DIR__ . '/../../../../../autoload.php';

return new \craft\cloud\bref\handlers\JobSqsHandler();
Loading