Skip to content

Commit 6c76764

Browse files
committed
Do not raise errors on purge.
1 parent 75a2f5f commit 6c76764

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

.rubocop_todo.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2020-04-26 17:46:16 -0400 using RuboCop version 0.81.0.
3+
# on 2020-06-16 16:54:39 -0400 using RuboCop version 0.81.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -44,11 +44,13 @@ Naming/HeredocDelimiterNaming:
4444
- 'sample_apps/sample_app_activerecord/commands/help.rb'
4545
- 'sample_apps/sample_app_mongoid/commands/help.rb'
4646

47-
# Offense count: 1
47+
# Offense count: 3
4848
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
4949
# AllowedNames: io, id, to, by, on, in, at, ip, db, os, pp
5050
Naming/MethodParameterName:
5151
Exclude:
52+
- 'lib/slack-ruby-bot-server/models/team/activerecord.rb'
53+
- 'lib/slack-ruby-bot-server/models/team/mongoid.rb'
5254
- 'lib/slack-ruby-bot-server/service.rb'
5355

5456
# Offense count: 1

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### 0.12.1 (Next)
44

5+
* [#118](https://github.com/slack-ruby/slack-ruby-bot-server/pull/118): Do not fail to start on errors in `Team#purge!` - [@dblock](https://github.com/dblock).
56
* Your contribution here.
67

78
#### 0.12.0 (2020/4/26)

lib/slack-ruby-bot-server/models/team/activerecord.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
class Team < ActiveRecord::Base
44
include Methods
55

6-
def self.purge!
7-
# destroy teams inactive for two weeks
8-
Team.where(active: false).where('updated_at <= ?', 2.weeks.ago).each do |team|
9-
puts "Destroying #{team}, inactive since #{team.updated_at}, over two weeks ago."
10-
team.destroy
6+
def self.purge!(dt = 2.weeks.ago)
7+
Team.where(active: false).where('updated_at <= ?', dt).each do |team|
8+
begin
9+
logger.info "Destroying #{team}, inactive since #{team.updated_at}."
10+
team.destroy
11+
rescue StandardError => e
12+
logger.warn "Error destroying #{team}, #{e.message}."
13+
end
1114
end
1215
end
1316
end

lib/slack-ruby-bot-server/models/team/mongoid.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ class Team
1515

1616
include Methods
1717

18-
def self.purge!
18+
def self.purge!(dt = 2.weeks.ago)
1919
# destroy teams inactive for two weeks
20-
Team.where(active: false, :updated_at.lte => 2.weeks.ago).each do |team|
21-
Mongoid.logger.info "Destroying #{team}, inactive since #{team.updated_at}, over two weeks ago."
22-
team.destroy
20+
Team.where(active: false, :updated_at.lte => dt).each do |team|
21+
begin
22+
logger.info "Destroying #{team}, inactive since #{team.updated_at}."
23+
team.destroy
24+
rescue StandardError => e
25+
logger.warn "Error destroying #{team}, #{e.message}."
26+
end
2327
end
2428
end
2529
end

spec/models/team_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,13 @@
1717
expect(Team.where(id: inactive_team_two_weeks_ago.id).first).to be nil
1818
expect(Team.where(id: inactive_team_a_month_ago.id).first).to be nil
1919
end
20+
it 'does not raise errors when a team cannot be destroyed' do
21+
allow_any_instance_of(Team).to receive(:destroy).and_raise(StandardError, 'cannot be destroyed')
22+
expect do
23+
expect do
24+
Team.purge!
25+
end.to_not change(Team, :count)
26+
end.to_not raise_error
27+
end
2028
end
2129
end

0 commit comments

Comments
 (0)