diff options
author | Olaf Tomalka <olaf.tomalka@gmail.com> | 2016-09-05 10:18:08 +0200 |
---|---|---|
committer | Olaf Tomalka <olaf.tomalka@gmail.com> | 2016-09-07 19:41:25 +0200 |
commit | e25b48ffcf1f7ef31df8d6c3366674e7f5c29893 (patch) | |
tree | 89e6864c451bfd3ebf68af14d12743304968cbc3 | |
parent | 6690fc70478b4d82c49689e9e13e414bc77bb271 (diff) | |
download | gitlab-ce-e25b48ffcf1f7ef31df8d6c3366674e7f5c29893.tar.gz |
Added cron to prune events older than 12 months.
Since contribution calendar shows only 12 months of activity,
events older than that time are not visible anywhere and can be
safely pruned saving big amount of database storage.
Fixes #21164
-rw-r--r-- | CHANGELOG | 1 | ||||
-rw-r--r-- | app/workers/prune_old_events_worker.rb | 8 | ||||
-rw-r--r-- | config/initializers/1_settings.rb | 3 | ||||
-rw-r--r-- | spec/workers/prune_old_events_worker_spec.rb | 24 |
4 files changed, 36 insertions, 0 deletions
diff --git a/CHANGELOG b/CHANGELOG index 16b813492a8..fea56a6ef21 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,6 +4,7 @@ v 8.12.0 (unreleased) - Add ability to fork to a specific namespace using API. (ritave) - Cleanup misalignments in Issue list view !6206 - Prepend blank line to `Closes` message on merge request linked to issue (lukehowell) + - Prune events older than 12 months. - Filter tags by name !6121 - Make push events have equal vertical spacing. - Add two-factor recovery endpoint to internal API !5510 diff --git a/app/workers/prune_old_events_worker.rb b/app/workers/prune_old_events_worker.rb new file mode 100644 index 00000000000..a0182fc67d0 --- /dev/null +++ b/app/workers/prune_old_events_worker.rb @@ -0,0 +1,8 @@ +class PruneOldEventsWorker + include Sidekiq::Worker + + def perform + # Contribution calendar shows maximum 12 months of events + Event.where('created_at < ?', (12.months + 1.day).ago).destroy_all + end +end diff --git a/config/initializers/1_settings.rb b/config/initializers/1_settings.rb index 4a01b9e40fb..2fac3e34dc5 100644 --- a/config/initializers/1_settings.rb +++ b/config/initializers/1_settings.rb @@ -299,6 +299,9 @@ Settings.cron_jobs['remove_expired_members_worker']['job_class'] = 'RemoveExpire Settings.cron_jobs['remove_expired_group_links_worker'] ||= Settingslogic.new({}) Settings.cron_jobs['remove_expired_group_links_worker']['cron'] ||= '10 0 * * *' Settings.cron_jobs['remove_expired_group_links_worker']['job_class'] = 'RemoveExpiredGroupLinksWorker' +Settings.cron_jobs['prune_old_events_worker'] ||= Settingslogic.new({}) +Settings.cron_jobs['prune_old_events_worker']['cron'] ||= '0 0 * * *' +Settings.cron_jobs['prune_old_events_worker']['job_class'] = 'PruneOldEventsWorker' # # GitLab Shell diff --git a/spec/workers/prune_old_events_worker_spec.rb b/spec/workers/prune_old_events_worker_spec.rb new file mode 100644 index 00000000000..35e1518a35e --- /dev/null +++ b/spec/workers/prune_old_events_worker_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper' + +describe PruneOldEventsWorker do + describe '#perform' do + let!(:expired_event) { create(:event, author_id: 0, created_at: 13.months.ago) } + let!(:not_expired_event) { create(:event, author_id: 0, created_at: 1.day.ago) } + let!(:exactly_12_months_event) { create(:event, author_id: 0, created_at: 12.months.ago) } + + it 'prunes events older than 12 months' do + expect { subject.perform }.to change { Event.count }.by(-1) + expect(Event.find_by(id: expired_event.id)).to be_nil + end + + it 'leaves fresh events' do + subject.perform + expect(not_expired_event.reload).to be_present + end + + it 'leaves events from exactly 12 months ago' do + subject.perform + expect(exactly_12_months_event).to be_present + end + end +end |