45 lines
1.3 KiB
PHP
45 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Temporal\WebhookDelivery;
|
|
|
|
use App\Models\WebhookDelivery;
|
|
use App\Temporal\Shared\FaultSimulator;
|
|
use Temporal\Activity;
|
|
|
|
class WebhookDeliveryActivity implements WebhookDeliveryActivityInterface
|
|
{
|
|
public function deliverToEndpoint(string $endpoint, array $payload, array $simulationConfig = []): array
|
|
{
|
|
$startTime = hrtime(true);
|
|
|
|
FaultSimulator::maybeApply($simulationConfig, "deliverToEndpoint:{$endpoint}");
|
|
|
|
// Simulate HTTP POST latency
|
|
usleep(mt_rand(100000, 400000));
|
|
|
|
$responseTime = (int) ((hrtime(true) - $startTime) / 1_000_000);
|
|
|
|
return [
|
|
'status' => 'delivered',
|
|
'statusCode' => 200,
|
|
'attempt' => Activity::getInfo()->attempt,
|
|
'responseTime' => $responseTime,
|
|
];
|
|
}
|
|
|
|
public function deadLetter(string $endpoint, array $payload, string $reason): bool
|
|
{
|
|
WebhookDelivery::create([
|
|
'workflow_id' => Activity::getInfo()->workflowExecution->getID(),
|
|
'endpoint' => $endpoint,
|
|
'payload' => $payload,
|
|
'status' => 'dead_lettered',
|
|
'attempts' => 0,
|
|
'last_error' => $reason,
|
|
'dead_lettered_at' => now(),
|
|
]);
|
|
|
|
return true;
|
|
}
|
|
}
|