Inbound mail ingestion

Ingestion copies each site’s IMAP mailboxes into LH Messaging (LH_Email_Ingest, includes/class-lh-email-ingest.php). It runs only in the background, on the lh_email_ingest Action Scheduler action, or through the run-ingest ability, never inside a page request. A run works for about 20 seconds (lh_email_return_ingest_time_budget): new mail since the folder’s cursor first, then any failed messages due for a retry, then the newest-first backfill. While work remains it queues the next run a minute later; otherwise the hourly wakeup starts the next one.

A database lock (GET_LOCK, released when the run ends or its connection closes) keeps two runs from overlapping. A run that finds the lock held logs “Another ingest run is in progress” and tries again a minute later.

Runs the host ends

The host can end a request without PHP running any shutdown code. Action Scheduler then leaves the action in progress for an hour before marking it failed. Since 3.20.3 the hourly wakeup counts only a waiting run as a duplicate, not one still shown as in progress, so a killed run costs at most an hour of ingest instead of two. Each run also records the step it is on (connecting, the new-mail, retry or backfill UIDs, or the bodies being fetched and their size) in the lh_email_ingest_progress option, deleted when the run ends. A record still there when the next run takes the lock belongs to a run that never finished, and that run logs a warning: “Ingest: the previous run on site N never finished: started …, last step at …: …”.

Messages that fail

A batch that fails as a whole is retried one message at a time, so a single unreadable message never stalls the cursor. A message that still cannot be read, or is fetched but not stored, is logged (“UID … skipped” or “UID … not stored”, with LH Messaging’s database reason since its 0.4.2), counted as failed, and, since 3.21.0, recorded in the lh_email_ingest_failures table (schema 2.11.0) with its connection, folder, UIDVALIDITY, error, attempt count and times. The cursor moves on, but the message is not lost: later runs retry it.

run-ingest shows all of this. A run reports failed, retried and recovered counts per folder, and every folder report carries failures (how many are retrying and how many were given up). status also lists the failures themselves, given-up ones first. action: retry with no UIDs gives every given-up message a fresh set of attempts (optionally for one slug or folder). With slug, folder and uids it adds those UIDs to the list, for mail lost before 3.21.0. Either way the next run is queued a minute later.