Init
This commit is contained in:
44
app/Temporal/WebhookDelivery/WebhookDeliveryActivity.php
Normal file
44
app/Temporal/WebhookDelivery/WebhookDeliveryActivity.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user