\Drupal\strata\Event\Webhook WebhookDispatcher

Queues one delivery per interested endpoint, and performs one attempt at a time.

Nothing is posted inline. A commit is sealed inside a web request, and a subscriber that made an outbound HTTP call there would put a slow or unreachable endpoint directly into the site's own response time - and a failed one into the flush's error path, where it does not belong. So a dispatch writes queue items and returns; the queue worker does the posting on cron.

The body is rendered ONCE per event and reused for every subscription. The signature covers those exact bytes, so re-encoding per endpoint would risk two endpoints being sent bodies that differ in key order and one of them failing verification.

A secret is never written to the queue. The item carries the URL, and the worker resolves the subscription again at delivery time; a subscription removed while its deliveries were waiting is dropped rather than sent.

Summary

Methods
Properties
Constants
__construct
subscriptions
subscriptionFor
dispatch
attempt
post
No public properties found
CONFIG
QUEUE
BACKOFF
MAX_BODY
No protected methods found
No protected properties found
No protected constants found
render
enqueue
deliveryId
http
configFactory
queues
time
logger
No private constants found

Constant

CONFIG

CONFIG = 'strata.webhooks'

The configuration object subscriptions are read from.

QUEUE

QUEUE = 'strata_webhook'

The queue deliveries wait in.

BACKOFF

BACKOFF = 60

Seconds to wait before the second attempt; each further attempt doubles it.

MAX_BODY

MAX_BODY = 65536

Longest body an endpoint is sent.

A payload is a summary of what happened, never the stored content itself, so anything past this is a bug rather than a large site.

Properties

$http

$http : \GuzzleHttp\ClientInterface

Type

ClientInterface

$configFactory

$configFactory : \Drupal\Core\Config\ConfigFactoryInterface

Type

ConfigFactoryInterface

$queues

$queues : \Drupal\Core\Queue\QueueFactory

Type

QueueFactory

$time

$time : \Drupal\Component\Datetime\TimeInterface

Type

TimeInterface

$logger

$logger : \Psr\Log\LoggerInterface

Type

LoggerInterface

Methods

__construct()

__construct(\GuzzleHttp\ClientInterface  $http, \Drupal\Core\Config\ConfigFactoryInterface  $configFactory, \Drupal\Core\Queue\QueueFactory  $queues, \Drupal\Component\Datetime\TimeInterface  $time, \Psr\Log\LoggerInterface  $logger) : mixed

Constructs a dispatcher.

Parameters

\GuzzleHttp\ClientInterface $http

The HTTP client deliveries are posted with.

\Drupal\Core\Config\ConfigFactoryInterface $configFactory

Where subscriptions are read from.

\Drupal\Core\Queue\QueueFactory $queues

Where deliveries wait.

\Drupal\Component\Datetime\TimeInterface $time

The clock a signature is stamped from.

\Psr\Log\LoggerInterface $logger

Records a delivery that failed every attempt.

Returns

mixed —

subscriptions()

subscriptions() : list<\Drupal\strata\Event\Webhook\WebhookSubscription>

Every configured subscription.

Returns

list<\Drupal\strata\Event\Webhook\WebhookSubscription> —

The subscriptions.

subscriptionFor()

subscriptionFor(string  $url) : \Drupal\strata\Event\Webhook\WebhookSubscription|null

The subscription for one URL.

Parameters

string $url

The endpoint.

Returns

\Drupal\strata\Event\Webhook\WebhookSubscription|null —

The subscription, or NULL when it is no longer configured.

dispatch()

dispatch(\Drupal\strata\Event\StrataEvent  $event) : int

Queues one delivery per subscription that wants this event.

Parameters

\Drupal\strata\Event\StrataEvent $event

The event.

Returns

int —

How many deliveries were queued.

attempt()

attempt(array  $item) : bool

Performs one attempt, and re-queues with backoff when there are attempts left.

Parameters

array $item

The queue item.

Returns

bool —

TRUE when the endpoint accepted the delivery.

post()

post(\Drupal\strata\Event\Webhook\WebhookSubscription  $subscription, string  $event, string  $body, string  $delivery) : bool

Posts one delivery.

Parameters

\Drupal\strata\Event\Webhook\WebhookSubscription $subscription

Where it goes.

string $event

The event name.

string $body

The exact bytes to send.

string $delivery

The delivery id.

Returns

bool —

TRUE when the endpoint answered with a 2xx.

render()

render(\Drupal\strata\Event\StrataEvent  $event) : string|null

The body an event is sent as.

Parameters

\Drupal\strata\Event\StrataEvent $event

The event.

Returns

string|null —

The JSON body, or NULL when it could not be encoded or is implausibly large.

enqueue()

enqueue(array  $item, int  $delay = 0) : void

Puts an item in the queue, delayed when the queue backend can delay.

Parameters

array $item

The item.

int $delay

Seconds to hold it for, or 0 for immediately.

Returns

void —

deliveryId()

deliveryId() : string

An id a receiver can deduplicate on.

Returns

string —

32 hex characters.