PHP code example of gian-tiaga / spiral-outbox

1. Go to this page and download the library: Download gian-tiaga/spiral-outbox library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gian-tiaga / spiral-outbox example snippets


final class PublishStockJob extends JobHandler
{
    public function invoke(
        string $outboxDeliveryId,
        OutboxMessageLoaderContract $outboxMessageLoader,
    ): void {
        $event = $outboxMessageLoader->load(
            outboxDeliveryId: $outboxDeliveryId,
            expectedMessageClass: AvailableStockChangedEvent::class,
        );

        // Вызов прикладного сценария с данными $event.
    }
}

final class PublishStockJob extends JobHandler
{
    public function invoke(string $outboxDeliveryId, bool $outboxLastAttempt): void {}
}

final class SyncCardsJob extends JobHandler
{
    public function invoke(
        string $outboxDeliveryId,
        string $outboxDispatchId,
        OutboxAttemptContract $outboxAttempt,
    ): void {
        foreach ($portions as $portion) {
            if (!$outboxAttempt->isCurrent(
                outboxDeliveryId: $outboxDeliveryId,
                outboxDispatchId: $outboxDispatchId,
            )) {
                return;
            }

            // Работа над одной порцией.
        }
    }
}

return [
    'eventsTableName' => 'outbox_events',
    'deliveriesTableName' => 'outbox_deliveries',
    'batchSize' => 100,
    'routes' => [
        AvailableStockChangedEvent::class => [
            new OutboxRoute(
                job: PublishStockJob::class,
                queue: StockQueue::Publish->value,
                retryDelaysSeconds: [60, 120],
                deliveryTimeoutSeconds: 3600,
            ),
        ],
    ],
];
bash
php app.php outbox:relay
php app.php outbox:relay --loop
bash
php app.php outbox:status