Skip to content

Commit 2709e88

Browse files
committed
Merge branch 'release/0.2.0'
2 parents 5c7b765 + 3db9928 commit 2709e88

File tree

8 files changed

+123
-8
lines changed

8 files changed

+123
-8
lines changed

.env.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ INFLUX_BUCKET=my-bucket
1111
INFLUX_MEASUREMENT=my_power_splitter
1212
INFLUX_INTERVAL=5
1313

14+
##### Redis
15+
REDIS_URL=redis://localhost:6379/1
16+
1417
# Sensor mapping: Map to Measurement/Field in InfluxDB
1518
INFLUX_SENSOR_GRID_IMPORT_POWER=SENEC:grid_power_plus
1619
INFLUX_SENSOR_HOUSE_POWER=SENEC:house_power

.github/workflows/push.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ jobs:
1818
test:
1919
runs-on: ubuntu-latest
2020

21+
services:
22+
redis:
23+
image: redis:7-alpine
24+
ports: ['6379:6379']
25+
2126
env:
2227
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
2328
CI: true

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ gem 'base64'
1515
# A toolkit of support libraries and Ruby core extensions extracted from the Rails framework. (https://rubyonrails.org)
1616
gem 'activesupport'
1717

18+
# A Ruby client library for Redis (https://github.com/redis/redis-rb)
19+
gem 'redis'
20+
1821
group :development do
1922
# Guard gem for RSpec (https://github.com/guard/guard-rspec)
2023
gem 'guard-rspec', require: false

Gemfile.lock

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ GEM
2525
rexml
2626
csv (3.3.0)
2727
diff-lcs (1.5.1)
28-
docile (1.4.0)
28+
docile (1.4.1)
2929
dotenv (3.1.2)
3030
drb (2.2.1)
3131
ffi (1.17.0)
@@ -44,7 +44,7 @@ GEM
4444
guard (~> 2.1)
4545
guard-compat (~> 1.1)
4646
rspec (>= 2.99.0, < 4.0)
47-
hashdiff (1.1.0)
47+
hashdiff (1.1.1)
4848
i18n (1.14.5)
4949
concurrent-ruby (~> 1.0)
5050
influxdb-client (3.1.0)
@@ -69,14 +69,18 @@ GEM
6969
coderay (~> 1.1)
7070
method_source (~> 1.0)
7171
public_suffix (6.0.1)
72-
racc (1.8.0)
72+
racc (1.8.1)
7373
rainbow (3.1.1)
7474
rake (13.2.1)
7575
rb-fsevent (0.11.2)
7676
rb-inotify (0.11.1)
7777
ffi (~> 1.0)
78+
redis (5.2.0)
79+
redis-client (>= 0.22.0)
80+
redis-client (0.22.2)
81+
connection_pool
7882
regexp_parser (2.9.2)
79-
rexml (3.3.2)
83+
rexml (3.3.4)
8084
strscan
8185
rspec (3.13.0)
8286
rspec-core (~> 3.13.0)
@@ -91,7 +95,7 @@ GEM
9195
diff-lcs (>= 1.2.0, < 2.0)
9296
rspec-support (~> 3.13.0)
9397
rspec-support (3.13.1)
94-
rubocop (1.65.0)
98+
rubocop (1.65.1)
9599
json (~> 2.3)
96100
language_server-protocol (>= 3.17.0)
97101
parallel (~> 1.10)
@@ -102,14 +106,14 @@ GEM
102106
rubocop-ast (>= 1.31.1, < 2.0)
103107
ruby-progressbar (~> 1.7)
104108
unicode-display_width (>= 2.4.0, < 3.0)
105-
rubocop-ast (1.31.3)
109+
rubocop-ast (1.32.0)
106110
parser (>= 3.3.1.0)
107111
rubocop-performance (1.21.1)
108112
rubocop (>= 1.48.1, < 2.0)
109113
rubocop-ast (>= 1.31.1, < 2.0)
110114
rubocop-rake (0.6.0)
111115
rubocop (~> 1.0)
112-
rubocop-rspec (3.0.3)
116+
rubocop-rspec (3.0.4)
113117
rubocop (~> 1.61)
114118
rubocop-thread_safety (0.5.1)
115119
rubocop (>= 0.90.0)
@@ -144,6 +148,7 @@ DEPENDENCIES
144148
guard-rspec
145149
influxdb-client
146150
rake
151+
redis
147152
rspec
148153
rubocop
149154
rubocop-performance
@@ -155,4 +160,4 @@ DEPENDENCIES
155160
webmock
156161

157162
BUNDLED WITH
158-
2.5.16
163+
2.5.17

lib/config.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Config # rubocop:disable Metrics/ClassLength
1212
:influx_bucket,
1313
:influx_measurement,
1414
:influx_interval,
15+
:redis_url,
1516
:time_zone
1617

1718
def initialize(env, logger: NullLogger.new)
@@ -30,6 +31,7 @@ def initialize(env, logger: NullLogger.new)
3031
logger.info "Accessing InfluxDB at #{influx_url}, bucket #{influx_bucket}"
3132

3233
@time_zone = env.fetch('TZ', 'Europe/Berlin')
34+
@redis_url = env.fetch('REDIS_URL', nil)
3335

3436
init_sensors(env)
3537
validate_sensors!

lib/loop.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'influx_push'
22
require 'influx_pull'
33
require 'processor'
4+
require 'redis_cache'
45

56
class Loop
67
def initialize(config:)
@@ -53,6 +54,8 @@ def process_historical_data
5354
day += 1.day
5455
end
5556

57+
RedisCache.new(config:).flush
58+
5659
config.logger.info '--- Processing historical data successfully finished'
5760
end
5861

lib/redis_cache.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'redis'
2+
3+
class RedisCache
4+
def initialize(config:)
5+
@config = config
6+
end
7+
attr_reader :config
8+
9+
def flush
10+
unless redis
11+
config.logger.warn 'REDIS_URL not set, skipping Redis cache flush'
12+
return
13+
end
14+
15+
result = redis.flushall
16+
if result == 'OK'
17+
config.logger.info 'Redis cache flushed'
18+
else
19+
config.logger.error "Flushing Redis cache failed: #{result}"
20+
end
21+
rescue StandardError => e
22+
config.logger.error "Flushing Redis cache failed: #{e.message}"
23+
end
24+
25+
def redis
26+
return unless config.redis_url
27+
28+
@redis ||= Redis.new(url: config.redis_url)
29+
end
30+
end

spec/lib/redis_cache_spec.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
require 'redis_cache'
2+
3+
describe RedisCache do
4+
subject(:redis_cache) { described_class.new(config:) }
5+
6+
let(:config) { Config.new(ENV.to_h, logger:) }
7+
let(:logger) { MemoryLogger.new }
8+
9+
describe '#flush' do
10+
subject(:flush) { redis_cache.flush }
11+
12+
context 'when Redis is available' do
13+
it 'writes info message into log' do
14+
flush
15+
16+
expect(logger.info_messages).to include('Redis cache flushed')
17+
end
18+
end
19+
20+
context 'when Redis is not available' do
21+
before do
22+
allow(config).to receive(:redis_url).and_return(
23+
'redis://localhost:1234',
24+
)
25+
end
26+
27+
it 'writes error message into log' do
28+
flush
29+
30+
expect(logger.error_messages).to include(
31+
'Flushing Redis cache failed: Connection refused - connect(2) for 127.0.0.1:1234 (redis://localhost:1234)',
32+
)
33+
end
34+
end
35+
36+
context 'when Redis cannot flush' do
37+
before do
38+
allow(Redis).to receive(:new).and_return(
39+
instance_double(Redis, flushall: 'ERROR'),
40+
)
41+
end
42+
43+
it 'writes error message into log' do
44+
flush
45+
46+
expect(logger.error_messages).to include(
47+
'Flushing Redis cache failed: ERROR',
48+
)
49+
end
50+
end
51+
52+
context 'when REDIS_URL missing' do
53+
before { allow(config).to receive(:redis_url).and_return(nil) }
54+
55+
it 'writes warn message into log' do
56+
flush
57+
58+
expect(logger.warn_messages).to include(
59+
'REDIS_URL not set, skipping Redis cache flush',
60+
)
61+
end
62+
end
63+
end
64+
end

0 commit comments

Comments
 (0)