Skip to the content
Documentation
Help Start
Reading in:
  • German: 9,220 of 9,220 pages
About the help in other languages
All help pages

Queue delivery: sending through the job queue

Without this feature, two cronjob parts send the newsletter: cjw_newsletter_mailqueue_create creates the queue of a send (one item per recipient), cjw_newsletter_mailqueue_process sends the items. Both wait for the cron…

Download as PDF

Address: https://doc.exponential.earth/learninminutes/exphelp/viewer/ext/cjw_newsletter/queue-delivery.md

On this page

Queue delivery: sending through the job queue

Without this feature, two cronjob parts send the newsletter: cjw_newsletter_mailqueue_create creates the queue of a send (one item per recipient), cjw_newsletter_mailqueue_process sends the items. Both wait for the cron interval, run as one process per host and mark an item sent after its mail went out, so a process that dies in between leaves the item to be sent again.

With queue delivery, the newsletter hands this work to the job queue expqueue of Exponential:

  • the queue of a send is created by a job as soon as there is something to create;
  • each send gets one delivery job that sends one batch, gives the worker back and comes again when the next batch may go;
  • every item is claimed in the database before its mail is sent, so it is sent at most once, also when two workers, a retried job and the cron meet on the same send.

It is off by default. Without expqueue, or switched off, everything works as before.

1. Switching it on

Requirements: Exponential with the extension expqueue (package se7enxweb/expqueue, listed under suggest in the newsletter's composer.json), its tables installed, and a worker running.

  1. Activate expqueue in the siteaccesses that send newsletters (the admin, and the siteaccess of the cron).

  2. Install its tables: ./console ext:expqueue:install-schema (or the SQL of the extension).

  3. Run a worker: the cron part expqueue every minute (php runcronjobs.php -q -s <siteaccess> expqueue), the command ./console ext:expqueue:work, or the systemd service of expqueue. Workers never run inside a web request.

  4. Switch it on in settings/override/cjw_newsletter.ini.append.php (or a siteaccess):

    [QueueDeliverySettings]
    QueueDelivery=enabled
  5. Clear the INI cache (php bin/php/ezcache.php --clear-tag=ini) and reload PHP-FPM.

It is in effect only when all three hold: QueueDelivery=enabled, expqueue is active in the current siteaccess, and its tables exist. The newsletter dashboard has a block "Queue delivery" that says whether it is in effect and, when it is switched on but not in effect, why (expqueue not active, tables missing).

Keep the newsletter cron parts in the crontab: they are the safety net (section 6).

2. What is queued

Job typeWhenPayloadDoes
cjw_newsletter.queue_createa send from the send form (at once), a scheduled send (not before its time), a subscriber entering a welcome series whose first step has no delay (at the start of the next minute)reasonthe same run as the cron part that creates the queue
cjw_newsletter.deliver_sendthe queue of a send was created; the cron finds an open send without a jobsend_idone batch of one send
cjw_newsletter.system_mailthe confirmation or information mail of a subscription failed for a temporary reasonnewsletter_user_id, mail, requestthe subscription mail again

Payloads carry ids only, never an address (the job queue's admin pages show payloads). The job types are registered in settings/expqueue.ini.append.php; timeouts 600 s (queue_create, deliver_send) and 60 s (system_mail), 5 attempts each.

Approvals within the same minute share one creation job, and an import queues nothing. Recurring sends are still made by the cron part that creates the queue.

3. Classes and concurrency

[QueueDeliverySettings]
EditionClass=bulk          # edition sends
SeriesClass=interactive    # the step sends of a welcome series
CreateClass=editorial      # the creation of the queue

The subscription mails go into the class interactive.

Each delivery job runs under the resource cjw_newsletter.<transport> (smtp, sendmail, file, or the name of the send's throttle transport). The newsletter's expqueue.ini.append.php sets MaxConcurrent=1 for smtp, sendmail and file: sends of different lists take turns per transport, sends with different transports run side by side.

The rate limits stay those of [ThrottleSettings] (MaxPerMinute[<transport>], MaxPerHour[<transport>], PauseBetweenBatches, see deliverability.md). The throttle checks and counts in two steps, so a site that raises MaxConcurrent of a transport accepts an overshoot of up to one batch per extra worker in a minute.

4. One batch per run

A run of cjw_newsletter.deliver_send:

  1. ends at once when the send is gone, aborted, or finished with nothing due;
  2. turns claims older than ClaimTimeout into "unknown outcome" (section 5);
  3. waits a minute when a feature holds the send back (a paused transport, an A/B test waiting for its winner);
  4. takes one batch of at most BatchSize items ([ThrottleSettings], also with Throttle=disabled), claims its items, sends them, records each result;
  5. comes again when the next batch may go: at once while items are due, else at the end of a pause, at the start of the next minute (or hour) when the rate limit was reached, after PauseBetweenBatches, or at the earliest retry of a refused item. A job that waits holds no worker.

When nothing is new or being sent any more, the run marks the send finished and the job is done. A send reopened by a retry later gets a new job.

Failures stay per recipient, exactly as with the cron: a temporary refusal is retried with the growing delays of [DeliverabilitySettings], a permanent one is a hard bounce, a mail the e-mail preferences refuse is closed. None of them fails the job. A delivery job fails only when the send cannot be worked on (for example the database is gone); expqueue retries it and, after its last attempt, it lands in the dead letter. The send's statistic then says so, and the cron does not queue a new job for that send until the dead one is retried in the job queue (expqueue/job/<uuid>, or ./console ext:expqueue:retry).

5. Claims and items of unknown outcome

Before a mail is sent, its item is claimed for the batch that sends it (status 2, "being sent", with the batch's id):

UPDATE cjwnl_edition_send_item SET status = 2, batch_id = <batch>, processed = <now>
 WHERE edition_send_id = <send> AND status = 0 AND next_retry <= <now> AND id IN ( <due ids> )
SELECT ... WHERE edition_send_id = <send> AND batch_id = <batch> AND status = 2

The batch id is unique, so what is read back belongs to that batch alone; every result is written with AND status = 2 AND batch_id = <batch>, so a batch never overwrites what another decided. The SQL is plain for every engine (no LIMIT in the UPDATE, no RETURNING).

An item that stays claimed longer than ClaimTimeout (900 s, longer than the job timeout plus expqueue's LeaseMargin) becomes unknown outcome (status 3): its worker stopped after the claim, and its mail may or may not have gone out. Such an item is never sent again by itself; a possibly lost mail is preferred over a possible duplicate. It counts as done for finishing the send.

An administrator decides:

  • in the send statistic of the edition (Delivery part): Send again (the items are new and due at once; a finished send is opened again) or Close (aborted). The buttons post to newsletter/send_unknown/<send id>, which needs the policy newsletter/send;

  • on the console:

    ./console ext:cjw_newsletter:queue --unknown                                  # the sends with unknown items
    ./console ext:cjw_newsletter:queue --resolve-unknown=<send id> --action=resend
    ./console ext:cjw_newsletter:queue --resolve-unknown=<send id> --action=close

Each decision is an audit event system.cjw_newsletter.unknown_items with the send id, the count and the action (no addresses), when the audit trail is on.

6. The cron parts with queue delivery

  • cjw_newsletter_mailqueue_create runs as before; the sends it creates get their delivery job.
  • cjw_newsletter_mailqueue_process still runs the hooks of the run (the SMS queue, the choice of an A/B winner), but sends no newsletter mail: it turns stale claims into unknown outcome and makes sure every open send has its delivery job (a job lost with a worker is queued again; a dead one is left to an administrator).
  • Queue delivery switched on but not in effect in the cron's process (expqueue not active there, tables missing): the cron sends as before, but with claims, so it never sends an item a worker on another host still holds.
  • Switched off: the cron is the 4.3 code path, without claims.

A refusal of the queue (a quota, a busy lock) never breaks the send form or a subscription: it is logged in the newsletter log, and the cron part picks the work up at its next run.

7. Subscription mails

The confirmation and information mails are still sent within the request, so the person reads "check your mailbox" a second later. Only when that attempt fails for a temporary reason (a 4xx reply, a mail server that cannot be reached) and queue delivery is in effect, a job sends the mail again with expqueue's retries. A permanent failure bounces the newsletter user as before. A confirmation is only sent again while the user still waits for it. Without queue delivery, a failed mail is logged and not retried, as before.

8. Switching it off, downgrading

Set QueueDelivery=disabled and clear the INI cache. Open sends continue with the cron at its next run; items claimed by a running job finish with it, stale ones become unknown outcome and are shown.

The item states 2 ("being sent") and 3 ("unknown outcome") are new in this release; no column or table was added. Before downgrading the package to a release that does not know them, resolve the unknown items (--resolve-unknown) and wait until no batch is running (no item in status 2).

9. For developers

  • CjwNewsletterQueueDelivery: inEffect(), reason(), ensureDelivery( $send ), dispatch( $send, $now ), queueCreate(), retrySubscriptionMail(), sendState( $send ).
  • CjwNewsletterQueue is the interface to the job queue (CjwNewsletterExpQueue for expqueue); the tests use a queue in memory.
  • CjwNewsletterRunner::processSend( $send, $out, &$totals, $claim ) is the work on one send, shared by the cron part and the delivery job.
  • CjwNewsletterDeliverability::claimItems(), finishItem(), markStaleClaims(), resolveUnknown(), unknownSends().
  • The extension point handler is CjwNewsletterQueueDeliveryHooks (sendFormStored, subscriptionStatusChanged, sendQueueCreated, dashboardSummary).

Something wrong on this page? Report an error

Documentation license

This page is part of the documentation of the extension cjw_newsletter and is distributed under the license of that extension.