Skip to content

Commit e37f708

Browse files
committed
Add status cache.
1 parent d98d566 commit e37f708

File tree

6 files changed

+43
-10462
lines changed

6 files changed

+43
-10462
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
pkg
22
tmp
33
coverage
4-
spec/dummy/log
4+
spec/dummy/log/*.log

lib/status-page/configuration.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ module StatusPage
22
class Configuration
33
PROVIDERS = [:cache, :database, :redis, :resque, :sidekiq].freeze
44

5-
attr_accessor :error_callback, :basic_auth_credentials
5+
attr_accessor :error_callback, :basic_auth_credentials, :interval
66
attr_reader :providers
77

88
def initialize
99
@providers = Set.new
10+
@interval = 10
1011
end
1112

1213
def use(provider_name)

lib/status-page/monitor.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,21 @@ def configure
1717
end
1818

1919
def check(request: nil)
20+
if configuration.interval > 0
21+
if @cached_status && @cached_status[:timestamp] >= (configuration.interval || 5).seconds.ago
22+
return @cached_status
23+
end
24+
end
25+
2026
providers = configuration.providers || []
2127
results = providers.map { |provider| provider_result(provider, request) }
2228

23-
{
29+
@cached_status = {
2430
results: results,
25-
status: results.all? { |result| result[:status] == STATUSES[:ok] } ? :ok : :service_unavailable
31+
status: results.all? { |result| result[:status] == STATUSES[:ok] } ? :ok : :service_unavailable,
32+
timestamp: Time.now
2633
}
34+
@cached_status
2735
end
2836

2937
private
@@ -35,17 +43,15 @@ def provider_result(provider, request)
3543
{
3644
name: provider.provider_name,
3745
message: '',
38-
status: STATUSES[:ok],
39-
timestamp: Time.now.to_s(:db)
46+
status: STATUSES[:ok]
4047
}
4148
rescue => e
4249
configuration.error_callback.call(e) if configuration.error_callback
4350

4451
{
4552
name: provider.provider_name,
4653
message: e.message,
47-
status: STATUSES[:error],
48-
timestamp: Time.now.to_s(:db)
54+
status: STATUSES[:error]
4955
}
5056
end
5157
end

spec/controllers/status_page/status_controller_spec.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
before do
1111
providers = Set.new
1212
providers << StatusPage::Services::Database
13+
1314
allow(StatusPage.configuration).to receive(:providers).and_return(providers)
15+
allow(StatusPage.configuration).to receive(:interval).and_return(0)
1416

1517
Timecop.freeze(time)
1618
end
@@ -45,8 +47,7 @@
4547
expect(json['results']).to eq([{
4648
'name' => 'database',
4749
'message' => '',
48-
'status' => 'OK',
49-
'timestamp' => time.to_s(:db)
50+
'status' => 'OK'
5051
}])
5152
end
5253
end
@@ -87,8 +88,7 @@
8788
expect(json['results'][0]).to eq({
8889
'name' => 'database',
8990
'message' => '',
90-
'status' => 'OK',
91-
'timestamp' => time.to_s(:db)
91+
'status' => 'OK'
9292
})
9393
end
9494

@@ -109,8 +109,7 @@
109109
expect(json['results'][0]).to eq({
110110
'name' => 'database',
111111
'message' => 'Exception',
112-
'status' => 'ERROR',
113-
'timestamp' => time.to_s(:db)
112+
'status' => 'ERROR'
114113
})
115114
end
116115
end

0 commit comments

Comments
 (0)