Skip to main content

Resolve the Outage Alert When Access Is Restored

Free Intermediate Since v1.2.0

The other half of Route license hard cutoff to PagerDuty. benecaster_license_payment_grace_recovered fires only when a day-30 cutoff is cleared and tokens are actually restored — never on the ordinary pay-before-the- deadline path — so it maps cleanly onto resolving an incident that the cutoff opened.

Use benecaster_license_grace_recovered instead if you want to know about every recovery. This one answers a narrower question: did an outage that people noticed just end?

$restored_count is the number of tokens un-revoked, and $started_at is when the grace window opened, so the difference gives total outage duration. Both belong in the resolve message — they are what tells you how bad it was after the fact.

Code

<?php
add_action(
    'benecaster_license_payment_grace_recovered',
    function ( int $restored_count, int $started_at ): void {
        $hours = (int) round( ( time() - $started_at ) / HOUR_IN_SECONDS );

        // Resolve the PagerDuty incident opened at cutoff — same dedup_key.
        wp_remote_post( 'https://events.pagerduty.com/v2/enqueue', [
            'headers' => [ 'Content-Type' => 'application/json' ],
            'timeout' => 5,
            'body'    => wp_json_encode( [
                'routing_key'  => MY_PAGERDUTY_ROUTING_KEY,
                'event_action' => 'resolve',
                'dedup_key'    => 'benecaster-grace-cutoff-' . home_url(),
            ] ),
        ] );

        // Post a human-readable summary to Slack.
        wp_remote_post( MY_SLACK_WEBHOOK_URL, [
            'headers' => [ 'Content-Type' => 'application/json' ],
            'timeout' => 5,
            'body'    => wp_json_encode( [
                'text' => sprintf(
                    ':white_check_mark: Benecaster access restored on %s — %d subscriber%s back after roughly %d hours dark.',
                    home_url(),
                    $restored_count,
                    1 === $restored_count ? '' : 's',
                    $hours
                ),
            ] ),
        ] );
    },
    10,
    2
);

View on GitHub →

Hooks Used