fix(job): Cron::WeeklyOverviewJob simplified and respect dolist API rate limiting

This commit is contained in:
Colin Darie 2023-03-02 19:01:14 +01:00
parent f8f14830a6
commit 43613009d6
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
2 changed files with 20 additions and 15 deletions

View file

@ -1,13 +1,16 @@
class Cron::WeeklyOverviewJob < Cron::CronJob
self.schedule_expression = "every monday at 7 am"
def perform(*args)
def perform
# Feature flipped to avoid mails in staging due to unprocessed dossier
if Rails.application.config.ds_weekly_overview
Instructeur.all
.map { |instructeur| [instructeur, instructeur.last_week_overview] }
.reject { |_, overview| overview.nil? }
.each { |instructeur, _| InstructeurMailer.last_week_overview(instructeur).deliver_later }
return unless Rails.application.config.ds_weekly_overview
Instructeur.find_each do |instructeur|
# NOTE: it's not exactly accurate because rate limit is not shared between jobs processes
Dolist::API.sleep_until_limit_reset if Dolist::API.near_rate_limit?
# mailer won't send anything if overview if empty
InstructeurMailer.last_week_overview(instructeur)&.deliver_later
end
end
end