Skip to main content

๐Ÿงฉ Task Scheduling System

RustMailer includes a built-in task scheduling system. Initially, I created a standalone crate โ€” persistent-scheduler โ€” with the intention of using it directly. However, I later realized that tightly integrating the scheduler into RustMailer would make it easier to support custom features like monitoring and event callbacks. So I embedded the scheduler into the project, removed unnecessary functionality, and tailored it specifically for RustMailer's needs.


๐Ÿงต Task Queuesโ€‹

The system defines two main task queues:

  • OUTBOX_QUEUE โ€“ Handles email sending tasks (SmtpTask)
  • EVENTHOOK_QUEUE โ€“ Handles event hook dispatch tasks (EventHookTask)

Each queue has its own configurable concurrency via environment variables:

  • RUSTMAILER_SEND_MAIL_WORKERS โ€“ Number of mail sending workers (default: 10)
  • RUSTMAILER_EVENT_HOOK_WORKERS โ€“ Number of event hook workers (default: 10)

๐Ÿฉบ Task Lifecycle & Statusโ€‹

Each task has a lifecycle tracked via the following statuses:

StatusDescription
ScheduledTask is scheduled but not yet running
RunningTask is currently being processed
SuccessTask completed successfully
FailedTask failed during execution
RemovedTask marked for deletion
StoppedTask was manually stopped
tip

Note: Tasks will only be marked as Failed if all retry attempts have been exhausted. Otherwise, failed tasks will be reset to Scheduled and re-queued for another execution attempt. The next execution time is determined according to the configured retry strategy.


๐Ÿ—ƒ Task Persistence & Storageโ€‹

  • All task metadata is persisted in an embedded database stored as a separate file tasks.db located under RUSTMAILER_ROOT_DIR.
  • Tasks are fully queryable and support manual inspection as well as status updates.

Hook Tasksโ€‹

Email Tasksโ€‹

These endpoints allow you to retrieve task statuses and manage tasks programmatically.


๐Ÿงน Cleanup Mechanismโ€‹

RustMailer includes a built-in cleanup job that runs periodically to remove expired tasks that are no longer needed.
The following task statuses are eligible for cleanup:

  • Removed
  • Success
  • Failed
  • Stopped

This ensures the task store remains clean and efficient over time.

The cleanup interval is controlled by the following configuration:

  • RUSTMAILER_CLEANUP_INTERVAL_HOURS โ€“ Task cleanup interval (default: 72 hours)

๐Ÿ”„ Retry Policies Overviewโ€‹

RustMailer uses different retry strategies for its two main task types to handle failed executions.


1. Event Hook Tasks (Hook Task)โ€‹

  • Currently uses a fixed Exponential Backoff strategy with retry intervals doubling each time (e.g., 2s, 4s, 8sโ€ฆ).
  • Maximum retry attempts: 10.
  • This strategy is fixed at the moment and cannot be configured by users. However, based on user feedback, this may be made configurable in future versions.

2. Email Sending Tasks (SMTP Task)โ€‹

  • Users can customize the retry policy when submitting email sending requests.
  • Supported strategies:
    • Linear: Retries happen at a fixed interval (e.g., every 2 seconds).
    • Exponential: Retries happen with exponentially increasing intervals, similar to Hook Tasks.
  • Configurable parameters include:
    • Retry strategy (Linear or Exponential)
    • Base retry interval in seconds
    • Maximum number of retry attempts
  • If no retry policy is specified, the default is linear retries every 2 seconds, up to 10 attempts.

Summaryโ€‹

Task TypeDefault Retry StrategyMax RetriesUser Configurable
Event Hook TasksExponential backoff (base 2)10No
Email Sending TasksLinear (2 seconds interval)10Yes

Properly configuring retry policies helps improve reliability for your email sending and event processing while avoiding excessive load from rapid repeated failures.


โณ Task Delay and Schedulingโ€‹

  • Hook Tasks: By default, event hook tasks execute immediately with no delay (delay = 0).
  • Email Sending Tasks: The send_at field in send_control allows scheduling an email to be sent at a specific future time.
    The system calculates the delay based on this timestamp and schedules the task accordingly.