Skip to content

Commit 9b2cf03

Browse files
committed
feat: add scheduled archive of units
1 parent 33944dd commit 9b2cf03

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
# Move old units to archive folder
4+
class ArchiveOldUnitsJob
5+
include Sidekiq::Job
6+
7+
def perform
8+
archive_period = Doubtfire::Application.config.unit_archive_after_period
9+
10+
archive_period = 1.year if archive_period < 1.year
11+
12+
units = Unit.where(archived: false).where('end_date < :archive_before', archive_before: DateTime.now - archive_period)
13+
14+
units.find_each(&:move_files_to_archive)
15+
rescue StandardError => e
16+
begin
17+
# Notify system admin
18+
mail = ErrorLogMailer.error_message('Archive Units', "Failed to move old units to archive", e)
19+
mail.deliver if mail.present?
20+
21+
logger.error e
22+
rescue StandardError => e
23+
logger.error "Failed to send error log to admin"
24+
end
25+
end
26+
end

config/schedule.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ progress_turn_it_in_jobs:
1111
clean_up_auth_tokens:
1212
cron: "every 30 minutes"
1313
class: "ClearAccessTokensJob"
14+
15+
archive_old_units:
16+
cron: "every 6 months"
17+
class: "ArchiveOldUnitsJob"

test/models/unit_model_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,4 +874,22 @@ def test_archive_unit
874874
assert_not File.exist?(p.portfolio_path), "New portfolio exists after delete - #{p.portfolio_path}"
875875
end
876876

877+
def test_archive_unit_job
878+
unit = FactoryBot.create :unit, with_students: false, task_count: 0
879+
880+
unit.end_date = Time.zone.now - Doubtfire::Application.config.unit_archive_after_period - 1.day
881+
unit.start_date = unit.end_date - 14.weeks
882+
unit.save!
883+
884+
unit2 = FactoryBot.create :unit, with_students: false, task_count: 0
885+
886+
assert_not unit.archived
887+
assert_not unit2.archived
888+
ArchiveOldUnitsJob.new.perform
889+
unit.reload
890+
891+
assert unit.archived
892+
assert_not unit2.archived
893+
end
894+
877895
end

0 commit comments

Comments
 (0)