SendGrid Contact Sync
Overview
This project syncs restaurant-related contacts to SendGrid Marketing Contacts daily using SendgridContactSyncWorker.
- Worker:
SendgridContactSyncWorker - Service:
SendgridContactSyncer - Scheduler config:
config/schedule.yml - Schedule:
0 2 * * * Asia/Bangkok(2:00 AM Bangkok time)
Create 10 Marketing Lists and Get list_id Values
Use the SendGrid Marketing Lists API to create the lists once and then copy their list IDs into Sendgrid::ContactLists::LISTS.
Names used in this project:
- Alpha
- Bravo
- Charlie
- Delta
- Echo
- Foxtrot
- Golf
- Hotel
- India
- Juliet
Example command to create one list:
curl -X POST "https://api.sendgrid.com/v3/marketing/lists" \
-H "Authorization: Bearer $SENDGRID_API_KEY" \
-H "Content-Type: application/json" \
-d '{"name":"Alpha"}'
Then open the response and copy the id value into:
app/my_lib/sendgrid/contact_lists.rb
Manual Test in Rails Console
Run sync manually for a quick test:
SendgridContactSyncWorker.new.perform
For safer testing, create only a few contacts first and run the worker.
Monitor in Sidekiq
Open Sidekiq Web UI and check:
- Enqueued/Processed jobs for
SendgridContactSyncWorker - Retry set behavior (worker retries up to 3 times)
- Logs for batch
job_idvalues returned by SendGrid
Notes
- Contacts are batched to 10,000 per request.
- SendGrid upsert endpoint:
PUT /v3/marketing/contacts. - 429 and connection/timeouts use exponential retry via
Retriable. - Non-2xx (except 429) raises
SendgridContactSyncer::SendgridSyncError.