enviroblyd 0.4.6 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a8467f960365419e9f02f14d3d3186a511787dff28798fd8883ac328a2443e9c
4
- data.tar.gz: 2f39dbca93e49cf69ab2bb9aaed76fe89ed2ebd7867baebba640c8ea665082ae
3
+ metadata.gz: 329d9e7586d58e8b529d2a94d46be76130efb24bf688a4d53efe17dfeaf3e7e0
4
+ data.tar.gz: 47cf08b7ecf7231460aeed688aa76705abff6cdcfdf770148f9f86f378ee513e
5
5
  SHA512:
6
- metadata.gz: dc3ea42ebc9125f88ec1828b4ebdf9032ba2482d1db359860b9b7c117ae0cb12eb57369531bd760cb54b2563f64c63e3afb89a304a574c23fa19bc7ff4334214
7
- data.tar.gz: 3c9baed9045d458595627b186f5be800abe5da2df36beea47e22fd478bc28e25e984814de9731118040b4a65f16fabea10923d06daaa0f419639ace81f0be67f
6
+ metadata.gz: f57916ca0d0615cd05cffcf15527f506a8ca60a47cec03f5d9ea4262b4ce0b298ae764cace907c764c4de9307f870659657610629c98e7ebd8d8c81388000623
7
+ data.tar.gz: a260266cb641e0b7e99d2e35f21cde3fcd82f80570782ce5628c4081c4e6f9773ee35ea4d660514267407ccc000146b3db1b504b60370518653f2b61c13ba719
data/bin/enviroblyd CHANGED
@@ -3,4 +3,12 @@
3
3
 
4
4
  require "enviroblyd"
5
5
 
6
- Enviroblyd::Daemon.start
6
+ daemon = Enviroblyd::Daemon.new
7
+
8
+ trap "INT" do
9
+ puts "Gracefully shutting down..."
10
+ daemon&.shutdown
11
+ exit
12
+ end
13
+
14
+ daemon.listen
@@ -6,29 +6,25 @@ class Enviroblyd::Daemon
6
6
  MAX_MESSAGE_SIZE = 6000 # bytes
7
7
  PORT = ENV.fetch("ENVIROBLYD_PORT", 63106).to_i
8
8
 
9
- def self.start
9
+ def initialize
10
10
  imds = Enviroblyd::IMDS.new
11
- host = imds.private_ipv4
12
- daemon = new(host)
13
- daemon.listen
14
- end
15
-
16
- def initialize(host)
17
- @host = host
11
+ @host = imds.private_ipv4
12
+ @threads = []
13
+ @shutdown = false
18
14
  end
19
15
 
20
16
  def listen
21
- server = TCPServer.new @host, PORT
17
+ @server = TCPServer.new @host, PORT
22
18
  puts "Listening on #{@host}:#{PORT}"
23
19
  Enviroblyd::Web.register
24
20
 
25
- loop do
26
- Thread.start(server.accept) do |client|
21
+ until @shutdown do
22
+ @threads << Thread.start(@server.accept) do |client|
27
23
  message = client.recv(MAX_MESSAGE_SIZE)
28
24
  command = Enviroblyd::Command.new message
29
25
 
30
26
  unless command.valid?
31
- $stderr.puts "Invalid message received: #{message}"
27
+ puts "Invalid message received: #{message}"
32
28
  client.puts "Invalid message"
33
29
  next
34
30
  end
@@ -38,11 +34,23 @@ class Enviroblyd::Daemon
38
34
  command.run
39
35
  ensure
40
36
  client.close
41
- command = nil
42
- message = nil
43
- client = nil
44
- GC.start
45
37
  end
38
+
39
+ delete_dead_threads
40
+ GC.start
41
+ GC.compact
46
42
  end
47
43
  end
44
+
45
+ def shutdown
46
+ @threads.each(&:join)
47
+ end
48
+
49
+ private
50
+ def delete_dead_threads
51
+ @threads.each do |thread|
52
+ next if thread.alive?
53
+ @threads.delete thread
54
+ end
55
+ end
48
56
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enviroblyd
4
- VERSION = "0.4.6"
4
+ VERSION = "0.5.0"
5
5
  end
@@ -33,8 +33,8 @@ class Enviroblyd::Web
33
33
 
34
34
  def http(url, type: Net::HTTP::Get, params: nil, headers: {}, retry_interval: 3, retries: 10, backoff: :exponential, tries: 1)
35
35
  if retries <= tries
36
- $stderr.puts "Retried #{tries} times. Aborting."
37
- exit 1
36
+ puts "Retried #{url} #{tries} times. Aborting."
37
+ return
38
38
  end
39
39
 
40
40
  uri = URI(url)
@@ -56,7 +56,7 @@ class Enviroblyd::Web
56
56
 
57
57
  if response == :retry || (500..599).include?(response.code.to_i)
58
58
  sleep_time = (backoff == :exponential) ? (retry_interval * tries) : retry_interval
59
- $stderr.puts "Retry #{uri} in #{sleep_time}s"
59
+ puts "Retry #{uri} in #{sleep_time}s"
60
60
  sleep sleep_time
61
61
  http(url, type:, params:, retry_interval:, retries:, backoff:, tries: (tries + 1))
62
62
  else
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enviroblyd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Starsi