🚧 Added deliverytime parameter to the `BatchSendingService`
ref https://linear.app/tryghost/issue/ONC-217/implement-the-deliverytime-option-in-mailgun-api-calls
Ghost arguably experiences its highest peak load immediately after sending out a newsletter, as it recieves an influx of traffic from users clicking on the links in the email, a burst of email analytics events to process from mailgun, and an increase in organic traffic to the site's frontend as well as the admin analytics pages. The BatchSendingService currently sends all the batches to Mailgun as quickly as possible, which may contribute to higher peak loads.
This commit adds a deliverytime parameter to our calls to Mailgun, which allows us to specify a time in the future when we want the email to be delivered. This will allow us to spread out the load on our servers by sending the batches to Mailgun at a slower rate, and letting Mailgun handle the delivery at the specified time.
Todo:
- [x] Fix failing email service unit tests
- [ ] Test on a large email send with many batches — so far I've only tested this on an email with 2 recipients and a batch size of 1 and it works as expected, but there may be some surprises at scale.
- [x] Move the inter-batch delay to config — it's currently hardcoded to 5 seconds, but ideally we'd be able to enable/disable this via config, and may want to adjust how long the delay is between each subsequent batch.
- [x] Refactor to specify the target delivery window, rather than the constant delay
- [ ] Test with scheduled posts — it should work the same way, but just to be sure...
- [ ] Test with timezones — again, it should workfine, but just to be sure...
- [ ] Test with a failed then retried email
- [ ] Add the
deliverytimeto logs when sending to mailgun for easier debugging