Oban worker for polling AWS SQS queue for email events.
This worker replaces the GenServer-based SQSWorker with an Oban-based approach that allows dynamic enabling/disabling without application restart.
Architecture
AWS SES → SNS Topic → SQS Queue → SQSPollingJob (Oban) → SQSProcessor → DatabaseFeatures
- Dynamic Configuration: Automatically responds to settings changes without restart
- Oban Integration: Uses Oban's job system for reliable background processing
- Self-Scheduling: Each job schedules the next polling cycle
- Batch Processing: Process up to 10 messages at a time
- Error Handling: Retry logic with Dead Letter Queue
- Settings-Based Control: Polling can be enabled/disabled via Settings
Configuration
All settings are retrieved from PhoenixKit Settings:
sqs_polling_enabled- enable/disable polling (checked before each cycle)sqs_polling_interval_ms- interval between polling cyclessqs_max_messages_per_poll- maximum messages per batchsqs_visibility_timeout- time for message processingaws_sqs_queue_url- SQS queue URLaws_region- AWS region
Usage
# Enable polling (starts first job)
PhoenixKit.Modules.Emails.SQSPollingManager.enable_polling()
# Disable polling (stops scheduling new jobs)
PhoenixKit.Modules.Emails.SQSPollingManager.disable_polling()
# Trigger immediate polling
PhoenixKit.Modules.Emails.SQSPollingManager.poll_now()
# Check status
PhoenixKit.Modules.Emails.SQSPollingManager.status()Oban Queue Configuration
Add to your config/config.exs:
config :your_app, Oban,
repo: YourApp.Repo,
queues: [
sqs_polling: 1 # Only one concurrent polling job
]Implementation Notes
- Uses
unique: [period: 60]to prevent duplicate jobs - Schedules next job only if polling is enabled
- Uses existing SQSProcessor for event processing
- Compatible with existing SQSWorker API
Summary
Functions
Cancels all scheduled SQS polling jobs.
Functions
@spec cancel_scheduled() :: {:ok, non_neg_integer()}
Cancels all scheduled SQS polling jobs.
Called when polling is disabled to immediately clean up pending jobs.
Returns
{:ok, count}- Number of cancelled jobs
Examples
iex> PhoenixKit.Modules.Emails.SQSPollingJob.cancel_scheduled()
{:ok, 2}