Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Oct 28, 2020. It is now read-only.

Commit f7450e2

Browse files
author
v.promzelev
committed
support custom signalizers
1 parent af80d0b commit f7450e2

File tree

3 files changed

+100
-6
lines changed

3 files changed

+100
-6
lines changed

‎README.md‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,24 @@ ActiveRecord::OverflowSignalizer.new(models: [ModelName])
4747
ActiveRecord::OverflowSignalizer.new(days_count: 360)
4848
```
4949

50-
## Development
50+
+ You can use own signalizer for sending notification to e-mail, slack, hipchat, etc.
51+
```ruby
52+
class MyAwesomeSignalizer
53+
def initialize(some_params)
54+
@notifier = SomeChatNotifier.new(some_params)
55+
end
5156

52-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
57+
def signalize(msg)
58+
@notifier.send_msg(msg)
59+
end
60+
end
5361

54-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
62+
ActiveRecord::OverflowSignalizer.new(signalizer: MyAwesomeSignalizer.new(some_params))
63+
```
64+
65+
## Development
5566

56-
For tests you need postgresql connection specified in `spec/database.yml`
67+
For tests you need postgresql connection specified in `spec/database.yml`.
5768

5869
## Contributing
5970

‎lib/activerecord/overflow_signalizer.rb‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ def initialize(type = nil)
1313

1414
DAY = 24 * 60 * 60
1515

16-
def initialize(logger: nil, models: nil, days_count: 60)
16+
def initialize(logger: nil, models: nil, days_count: 60,signalizer: nil)
1717
@logger = logger || ActiveRecord::Base.logger
1818
@models = models || ActiveRecord::Base.descendants
1919
@days_count = days_count
20+
@signalizer = signalizer
2021
end
2122

2223
def analyse!
@@ -59,7 +60,12 @@ def signalize(table, current_value, max_value)
5960
else
6061
msg = "Primary key in table #{table} will overflow soon! #{current_value} from #{max_value}"
6162
end
62-
@logger.warn(msg)
63+
if @logger && @logger.respond_to?(:warn)
64+
@logger.warn(msg)
65+
end
66+
if @signalizer && @signalizer.respond_to?(:signalize)
67+
@signalizer.signalize(msg)
68+
end
6369
end
6470
end
6571
end

‎spec/activerecord/overflow_signalizer_spec.rb‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,82 @@
8282
end
8383
end
8484
end
85+
86+
context 'custom signalizer' do
87+
let!(:signalizer) { double(:signalizer, signalize: true) }
88+
89+
subject { described_class.new(signalizer: signalizer, models: [TestIntModel], days_count: 10) }
90+
91+
context 'empty table' do
92+
it 'doesnt log anything' do
93+
expect(signalizer).not_to receive(:signalize)
94+
subject.analyse!
95+
end
96+
end
97+
98+
context 'not empty table' do
99+
let(:max_int) { 2_147_483_647 }
100+
let(:day) { 24 * 60 * 60 }
101+
let(:today) { Time.now }
102+
103+
context 'overflow far' do
104+
before do
105+
(1..7).each do |t|
106+
TestIntModel.create!(created_at: today - day * t, updated_at: today - day * t)
107+
end
108+
end
109+
110+
after do
111+
TestIntModel.connection.execute(%Q{ALTER SEQUENCE "int_test_id_seq" RESTART WITH 1;})
112+
TestIntModel.destroy_all
113+
end
114+
115+
it 'doesnt log anything' do
116+
expect(signalizer).not_to receive(:signalize)
117+
subject.analyse!
118+
end
119+
end
120+
121+
context 'overflow soon' do
122+
before do
123+
TestIntModel.connection.execute(%Q{ALTER SEQUENCE "int_test_id_seq" RESTART WITH #{max_int - 16};})
124+
(1..7).each do |t|
125+
TestIntModel.create!(created_at: today - day * t, updated_at: today - day * t)
126+
end
127+
end
128+
129+
after do
130+
TestIntModel.connection.execute(%Q{ALTER SEQUENCE "int_test_id_seq" RESTART WITH 1;})
131+
TestIntModel.destroy_all
132+
end
133+
134+
it 'log about owerflow' do
135+
expect(signalizer).to receive(:signalize)
136+
.with("Primary key in table #{TestIntModel.table_name} will overflow soon! #{TestIntModel.last.id} from #{max_int}")
137+
subject.analyse!
138+
end
139+
end
140+
141+
context 'overflowed' do
142+
before do
143+
TestIntModel.connection.execute(%Q{ALTER SEQUENCE "int_test_id_seq" RESTART WITH #{max_int - 6};})
144+
(1..7).each do |t|
145+
TestIntModel.create!(created_at: today - day * t, updated_at: today - day * t)
146+
end
147+
end
148+
149+
after do
150+
TestIntModel.connection.execute(%Q{ALTER SEQUENCE "int_test_id_seq" RESTART WITH 1;})
151+
TestIntModel.destroy_all
152+
end
153+
154+
it 'log about owerflow' do
155+
expect(signalizer).to receive(:signalize)
156+
.with("Primary key in table #{TestIntModel.table_name} overflowed! #{TestIntModel.last.id} from #{max_int}")
157+
subject.analyse!
158+
end
159+
end
160+
end
161+
end
85162
end
86163
end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /