forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_scrubber.rb
124 lines (108 loc) · 3.46 KB
/
message_scrubber.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#
# Copyright (C) 2013 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Public: Delete old (> 360 days) records from messages table.
class MessageScrubber
# Public: The minimum wait time in seconds between processing batches.
MIN_DELAY = 1
# Public: The default batch size.
BATCH_SIZE = 1000
attr_reader :batch_size, :delay, :limit, :logger
# Public: Create a new MessageScrubber.
#
# options - A settings hash. Accepted options are:
# - batch_size: The number of records to fetch at once (default: 1000).
# - delay: The delay, in seconds, between batches (default: 1).
# - logger: A logger object to log messages to (default: Rails.logger).
def initialize(options = {})
@batch_size = options.fetch(:batch_size, BATCH_SIZE)
@limit = Integer(Setting.get(limit_setting, limit_size)).days.ago
@delay = options.fetch(:delay, MIN_DELAY)
@logger = options.fetch(:logger, Rails.logger)
end
def self.scrub
new.scrub
end
# Public: Delete old delayed messages on the current shard.
#
# options - A settings hash that accepts:
# - dry_run: If true, log the # of records affected but do not delete them (default: false).
#
# Returns nothing.
def scrub(options = {})
dry_run = options.fetch(:dry_run, false)
scope = klass.where("#{filter_attribute} < ?", limit)
dry_run ? log(scope) : delete_messages(scope)
end
# Public: Delete old delayed messages on all shards.
#
# options - A settings hash that accepts:
# - dry_run: If true, log the # of records affected but do not delete them (default: false).
#
# Returns nothing
def scrub_all(options = {})
Shard.with_each_shard { scrub(options) }
end
protected
# Internal: Delete the current batch of messages.
#
# scope - The ActiveRecord scope to work on.
#
# Returns the number of records deleted.
def delete_messages(scope)
count = scope.limit(batch_size).delete_all
total = count
while count > 0
sleep(delay)
count = scope.limit(batch_size).delete_all
total += count
end
total
end
# Internal: The column name to filter messages on (e.g. 'sent_at').
#
# Returns a column name string.
def filter_attribute
'sent_at'
end
# Internal: The class object to delete records from (e.g. 'Message').
#
# Returns class object.
def klass
Message
end
# Internal: The name of the Canvas setting this class' limit is stored in.
#
# Returns a setting name string.
def limit_setting
'message_scrubber_limit'
end
# Internal: The default limit (in days) to delete messages after.
#
# Returns a setting name string.
def limit_size
360
end
# Internal: Log expected action.
#
# scope - The ActiveRecord scope to log.
#
# Returns nothing.
def log(scope)
logger.info("#{self.class.to_s}: #{scope.count} records would be deleted (older than #{limit})")
end
end