Skip to content

Commit c462b6d

Browse files
committed
Merge branch 'release/0.7.0'
2 parents ac1fe8a + e57e20b commit c462b6d

21 files changed

Lines changed: 245 additions & 262 deletions

.env.test

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ SENEC_HOST=10.0.1.123
33
SENEC_INTERVAL=5
44

55
# Example of an InfluxDB host
6-
INFLUX_HOST=influx.example.com
7-
INFLUX_SCHEMA=https
8-
INFLUX_PORT=443
9-
INFLUX_TOKEN=this.is.just.an.example
10-
INFLUX_ORG=solectrus
11-
INFLUX_BUCKET=SENEC
6+
INFLUX_HOST=localhost
7+
INFLUX_SCHEMA=http
8+
INFLUX_PORT=8086
9+
INFLUX_TOKEN=my-token
10+
INFLUX_ORG=my-org
11+
INFLUX_BUCKET=my-bucket

.rubocop.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require:
22
- rubocop-minitest
33
- rubocop-rake
4+
- rubocop-performance
45

56
AllCops:
67
TargetRubyVersion: 3.2
@@ -16,7 +17,7 @@ Style/FrozenStringLiteralComment:
1617
Enabled: false
1718

1819
Metrics/MethodLength:
19-
Max: 15
20+
Max: 20
2021

2122
Minitest/MultipleAssertions:
2223
Max: 10

.ruby-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.2.0
1+
3.2.1

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"ruby.lint": {
3+
"rubocop": {
4+
"forceExclusion": true
5+
}
6+
}
7+
}

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM ruby:3.2.0-alpine AS Builder
1+
FROM ruby:3.2.1-alpine AS Builder
22
RUN apk add --no-cache build-base
33

44
WORKDIR /senec-collector
@@ -8,7 +8,7 @@ RUN bundle config --local frozen 1 && \
88
bundle install -j4 --retry 3 && \
99
bundle clean --force
1010

11-
FROM ruby:3.2.0-alpine
11+
FROM ruby:3.2.1-alpine
1212
LABEL maintainer="georg@ledermann.dev"
1313

1414
# Decrease memory usage

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ group :development, :test do
2828
# A RuboCop plugin for Rake (https://github.com/rubocop/rubocop-rake)
2929
gem 'rubocop-rake'
3030

31+
# Automatic performance checking tool for Ruby code. (https://github.com/rubocop/rubocop-performance)
32+
gem 'rubocop-performance'
33+
3134
# Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests. (https://relishapp.com/vcr/vcr/docs)
3235
gem 'vcr'
3336

Gemfile.lock

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,47 @@ GEM
66
ast (2.4.2)
77
crack (0.4.5)
88
rexml
9-
domain_name (0.5.20190701)
10-
unf (>= 0.0.5, < 1.0.0)
119
dotenv (2.8.1)
12-
ffi (1.15.5)
13-
ffi-compiler (1.0.1)
14-
ffi (>= 1.0.0)
15-
rake
1610
hashdiff (1.0.1)
17-
http (5.1.1)
18-
addressable (~> 2.8)
19-
http-cookie (~> 1.0)
20-
http-form_data (~> 2.2)
21-
llhttp-ffi (~> 0.4.0)
22-
http-cookie (1.0.5)
23-
domain_name (~> 0.5)
24-
http-form_data (2.3.0)
11+
httparty (0.21.0)
12+
mini_mime (>= 1.0.0)
13+
multi_xml (>= 0.5.2)
2514
influxdb-client (2.9.0)
2615
json (2.6.3)
27-
llhttp-ffi (0.4.0)
28-
ffi-compiler (~> 1.0)
29-
rake (~> 13.0)
30-
minitest (5.17.0)
31-
oj (3.14.1)
16+
mini_mime (1.1.2)
17+
minitest (5.18.0)
18+
multi_xml (0.6.0)
19+
oj (3.14.2)
3220
parallel (1.22.1)
33-
parser (3.2.0.0)
21+
parser (3.2.1.1)
3422
ast (~> 2.4.1)
3523
public_suffix (5.0.1)
3624
rainbow (3.1.1)
3725
rake (13.0.6)
38-
regexp_parser (2.6.2)
26+
regexp_parser (2.7.0)
3927
rexml (3.2.5)
40-
rubocop (1.44.1)
28+
rubocop (1.48.1)
4129
json (~> 2.3)
4230
parallel (~> 1.10)
4331
parser (>= 3.2.0.0)
4432
rainbow (>= 2.2.2, < 4.0)
4533
regexp_parser (>= 1.8, < 3.0)
4634
rexml (>= 3.2.5, < 4.0)
47-
rubocop-ast (>= 1.24.1, < 2.0)
35+
rubocop-ast (>= 1.26.0, < 2.0)
4836
ruby-progressbar (~> 1.7)
4937
unicode-display_width (>= 2.4.0, < 3.0)
50-
rubocop-ast (1.24.1)
51-
parser (>= 3.1.1.0)
52-
rubocop-minitest (0.27.0)
53-
rubocop (>= 0.90, < 2.0)
38+
rubocop-ast (1.27.0)
39+
parser (>= 3.2.1.0)
40+
rubocop-minitest (0.29.0)
41+
rubocop (>= 1.39, < 2.0)
42+
rubocop-performance (1.16.0)
43+
rubocop (>= 1.7.0, < 2.0)
44+
rubocop-ast (>= 0.4.0)
5445
rubocop-rake (0.6.0)
5546
rubocop (~> 1.0)
56-
ruby-progressbar (1.11.0)
57-
senec (0.7.0)
58-
http (~> 5)
59-
unf (0.1.4)
60-
unf_ext
61-
unf_ext (0.0.8.2)
47+
ruby-progressbar (1.13.0)
48+
senec (0.7.1)
49+
httparty
6250
unicode-display_width (2.4.2)
6351
vcr (6.1.0)
6452
webmock (3.18.1)
@@ -77,10 +65,11 @@ DEPENDENCIES
7765
rake
7866
rubocop
7967
rubocop-minitest
68+
rubocop-performance
8069
rubocop-rake
8170
senec
8271
vcr
8372
webmock
8473

8574
BUNDLED WITH
86-
2.4.6
75+
2.4.9

src/flux_writer.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ def initialize(config)
77

88
attr_reader :config
99

10-
def self.push(config:, record:)
11-
new(config).push(record)
12-
end
13-
1410
def push(record)
1511
return unless record
1612

src/influx_push.rb

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,44 @@
22

33
class InfluxPush
44
def initialize(config:, queue:)
5-
@queue = queue
65
@config = config
7-
@count = 0
6+
@queue = queue
7+
@flux_writer = FluxWriter.new(config)
88
end
99

10-
attr_reader :config, :queue
10+
attr_reader :config, :queue, :flux_writer
1111

1212
def run
13-
until queue.empty?
13+
until queue.closed?
14+
# Wait for a record to be added to the queue
1415
record = queue.pop
1516

17+
# Stop if the queue has been closed
18+
next unless record
19+
1620
begin
17-
FluxWriter.push(config:, record:)
18-
@count += 1
19-
rescue StandardError
20-
# Put the record back into the queue
21-
queue << record
21+
flux_writer.push(record)
22+
puts "Successfully pushed record ##{record.id} to InfluxDB"
23+
rescue StandardError => e
24+
error_handling(record, e)
2225

23-
raise
26+
# Wait a bit before trying again
27+
sleep(5)
2428
end
2529
end
2630
end
2731

28-
def success_message
29-
return unless @count.positive?
32+
private
3033

31-
"Successfully pushed #{pluralize(@count, 'record', 'records')} to InfluxDB"
32-
end
34+
def error_handling(record, error)
35+
# Log the error
36+
puts "Error while pushing record ##{record.id} to InfluxDB: #{error.message}"
3337

34-
def failure_message(error)
35-
"Error while pushing #{pluralize(queue.length, 'record', 'records')} to InfluxDB. #{error.class}: #{error.message}"
36-
end
38+
return if queue.closed?
3739

38-
private
40+
# Put the record back into the queue
41+
queue << record
3942

40-
def pluralize(count, singular, plural)
41-
count == 1 ? singular : "#{count} #{plural}"
43+
puts "The record has been queued. Will retry to push #{queue.size} records later."
4244
end
4345
end

src/loop.rb

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,52 +12,66 @@ def initialize(config:, max_count:)
1212
end
1313

1414
attr_reader :config, :max_count
15-
attr_accessor :queue, :count, :push_thread
15+
attr_accessor :queue
1616

1717
def start
1818
self.queue = Queue.new
19-
self.count = 0
2019

21-
loop do
22-
self.count += 1
20+
pull_thread = Thread.new { pull_loop }
21+
push_thread = Thread.new { push_loop }
2322

24-
pull_from_senec
25-
push_to_influx
23+
# Wait for the pull thread to finish (will happen if max_count is set)
24+
pull_thread.join
2625

27-
break if max_count && count >= max_count
26+
# Push any remaining records to InfluxDB
27+
close_queue
2828

29-
sleep config.senec_interval
30-
end
29+
# Wait for the push thread to finish (will happen because queue is closed)
30+
push_thread.join
31+
rescue SystemExit, Interrupt
32+
puts 'Exiting...'
33+
34+
# Stop pulling data from SENEC
35+
pull_thread.exit
36+
37+
# Push any remaining records to InfluxDB (can take a while)
38+
close_queue
3139

32-
# Wait for the push thread (if there is one) to finish
33-
push_thread&.join
40+
# Stop pushing data to InfluxDB
41+
push_thread.exit
3442
end
3543

3644
private
3745

3846
# Pull data from SENEC and add to queue
39-
def pull_from_senec
47+
def pull_loop
4048
pull = SenecPull.new(config:, queue:)
41-
pull.run
42-
puts pull.success_message(count)
43-
rescue StandardError => e
44-
puts pull.failure_message(e)
45-
end
4649

47-
# Push data from queue to InfluxDB
48-
# Do this in a separate thread so that the main thread is not blocked
49-
def push_to_influx
50-
# Do nothing if there is already a push thread running
51-
return if push_thread&.status
52-
53-
# Create new thread and push to InfluxDB
54-
self.push_thread =
55-
Thread.new do
56-
push = InfluxPush.new(config:, queue:)
57-
push.run
58-
puts push.success_message
50+
loop do
51+
begin
52+
pull.next
53+
puts pull.success_message
5954
rescue StandardError => e
60-
puts push.failure_message(e)
55+
puts pull.failure_message(e)
6156
end
57+
58+
break if max_count && pull.count >= max_count
59+
60+
sleep config.senec_interval
61+
end
62+
end
63+
64+
# Push data from queue to InfluxDB
65+
def push_loop
66+
InfluxPush.new(config:, queue:).run
67+
end
68+
69+
def close_queue
70+
until queue.empty?
71+
puts "Waiting for #{queue.size} records to be pushed to InfluxDB"
72+
sleep 1
73+
end
74+
75+
queue.close
6276
end
6377
end

0 commit comments

Comments
 (0)