forked from demarches-simplifiees/demarches-simplifiees.fr
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge tag '2023-06-26-01' into feature/import-2023-06-26
# Conflicts: # db/schema.rb
- Loading branch information
Showing
12 changed files
with
145 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Cron::ExpiredDossiersBrouillonDeletionJob < Cron::CronJob | ||
self.schedule_expression = "every day at 10 pm" | ||
|
||
def perform(*args) | ||
ExpiredDossiersDeletionService.new.process_expired_dossiers_brouillon | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
app/jobs/cron/expired_dossiers_en_construction_deletion_job.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Cron::ExpiredDossiersEnConstructionDeletionJob < Cron::CronJob | ||
self.schedule_expression = "every day at 3 pm" | ||
|
||
def perform(*args) | ||
ExpiredDossiersDeletionService.new.process_expired_dossiers_en_construction | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Cron::ExpiredDossiersTermineDeletionJob < Cron::CronJob | ||
self.schedule_expression = "every day at 7 am" | ||
|
||
def perform(*args) | ||
ExpiredDossiersDeletionService.new.process_expired_dossiers_termine | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,13 @@ | ||
class Cron::WeeklyOverviewJob < Cron::CronJob | ||
self.schedule_expression = "every monday at 7 am" | ||
self.schedule_expression = "every monday at 4 am" | ||
|
||
def perform | ||
# Feature flipped to avoid mails in staging due to unprocessed dossier | ||
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 | ||
InstructeurMailer.last_week_overview(instructeur)&.deliver_later(wait: rand(0..3.hours)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class MailRateLimiter | ||
attr_reader :delay, :current_window | ||
|
||
def send_with_delay(mail) | ||
if current_window_full? | ||
@delay += @window | ||
end | ||
if current_window_full? || current_window_expired? | ||
@current_window = { started_at: Time.current, sent: 0 } | ||
end | ||
@current_window[:sent] += 1 | ||
|
||
mail.deliver_later(wait: delay) | ||
end | ||
|
||
private | ||
|
||
def initialize(limit:, window:) | ||
@limit = limit | ||
@window = window | ||
@current_window = { started_at: Time.current, sent: 0 } | ||
@delay = 0 | ||
end | ||
|
||
def current_window_expired? | ||
(@current_window[:started_at] + @window).past? | ||
end | ||
|
||
def current_window_full? | ||
@current_window[:sent] >= @limit | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class AddIndexToParentDossierId < ActiveRecord::Migration[7.0] | ||
disable_ddl_transaction! | ||
|
||
def change | ||
add_index :dossiers, :parent_dossier_id, algorithm: :concurrently | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
describe MailRateLimiter do | ||
describe 'hits limits' do | ||
let(:limit) { 10 } | ||
let(:window) { 2.seconds } | ||
let(:rate_limiter) { MailRateLimiter.new(limit:, window:) } | ||
let(:mail) { DossierMailer.notify_automatic_deletion_to_user([], '[email protected]') } | ||
|
||
it 'decreases current_window[:limit]' do | ||
expect { rate_limiter.send_with_delay(mail) }.to change { rate_limiter.current_window[:sent] }.by(1) | ||
end | ||
|
||
it 'increases the delay by window when it reaches the max number of call' do | ||
expect do | ||
(limit + 1).times { rate_limiter.send_with_delay(mail) } | ||
end.to change { rate_limiter.delay }.by(window) | ||
end | ||
|
||
it 'renews current_window when it expires' do | ||
rate_limiter.send_with_delay(mail) | ||
travel_to(Time.current + window + 1.second) do | ||
rate_limiter.send_with_delay(mail) | ||
expect(rate_limiter.current_window[:sent]).to eq(1) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.