enviroblyd 0.4.6 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a8467f960365419e9f02f14d3d3186a511787dff28798fd8883ac328a2443e9c
4
- data.tar.gz: 2f39dbca93e49cf69ab2bb9aaed76fe89ed2ebd7867baebba640c8ea665082ae
3
+ metadata.gz: cf14c9c81dfe5bf5842d0cfca53466605c2c053149096a74c955a506132ae30a
4
+ data.tar.gz: 45c8f5812463f757a9a56ab906d1a15e61fb3ed5ae6c84fc123b86b12e887f74
5
5
  SHA512:
6
- metadata.gz: dc3ea42ebc9125f88ec1828b4ebdf9032ba2482d1db359860b9b7c117ae0cb12eb57369531bd760cb54b2563f64c63e3afb89a304a574c23fa19bc7ff4334214
7
- data.tar.gz: 3c9baed9045d458595627b186f5be800abe5da2df36beea47e22fd478bc28e25e984814de9731118040b4a65f16fabea10923d06daaa0f419639ace81f0be67f
6
+ metadata.gz: a2c0a0d8077062a6149ad420e2e1a3769bc5ce16c47815bf65f4b96ab3ef15f5c7e3d1f93c5eb7ba862e822dbbb8cfb39c2c42122a823298e54e9a9c82e314cc
7
+ data.tar.gz: c82f027d69d99d30a3aa17b51adc6ed26e0474910aa89041747091193ddcd2ec8c2bb548c91574a4bbfd8eeb429b679c7b351f5cfdc5be71ae84e47ade290034
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
@@ -2,6 +2,8 @@
2
2
 
3
3
  require "open3"
4
4
  require "json"
5
+ require "zlib"
6
+ require "base64"
5
7
 
6
8
  class Enviroblyd::Command
7
9
  DEFAULT_TIMEOUT_SECONDS = 5 * 60
@@ -10,7 +12,7 @@ class Enviroblyd::Command
10
12
  def initialize(message)
11
13
  params = parse_message message
12
14
  @url = params.fetch "url"
13
- @script = params.fetch "script"
15
+ @script = decode_and_decompress params.fetch("script")
14
16
  @runtime = params.fetch "runtime", DEFAULT_RUNTIME
15
17
  @timeout = params.fetch "timeout", DEFAULT_TIMEOUT_SECONDS
16
18
  @stdout = @stderr = @exit_code = nil
@@ -54,4 +56,8 @@ class Enviroblyd::Command
54
56
  rescue
55
57
  nil
56
58
  end
59
+
60
+ def decode_and_decompress(encoded_data)
61
+ Zlib::Inflate.inflate Base64.decode64(encoded_data)
62
+ end
57
63
  end
@@ -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.6.0"
5
5
  end
@@ -2,7 +2,6 @@
2
2
 
3
3
  require "net/http"
4
4
  require "uri"
5
- require "open3"
6
5
  require "fileutils"
7
6
  require "pathname"
8
7
  require "json"
@@ -10,7 +9,6 @@ require "json"
10
9
  class Enviroblyd::Web
11
10
  USER_AGENT = "enviroblyd #{Enviroblyd::VERSION}"
12
11
  CONTENT_TYPE = "application/json"
13
- API_HOST = ENV.fetch("ENVIROBLYD_API_HOST", "envirobly.com")
14
12
  WORKING_DIR = Pathname.new ENV.fetch("ENVIROBLYD_WORKING_DIR", "/var/envirobly/daemon")
15
13
  INITIALIZED_FILE = WORKING_DIR.join "initialized"
16
14
 
@@ -33,8 +31,8 @@ class Enviroblyd::Web
33
31
 
34
32
  def http(url, type: Net::HTTP::Get, params: nil, headers: {}, retry_interval: 3, retries: 10, backoff: :exponential, tries: 1)
35
33
  if retries <= tries
36
- $stderr.puts "Retried #{tries} times. Aborting."
37
- exit 1
34
+ puts "Retried #{url} #{tries} times. Aborting."
35
+ return
38
36
  end
39
37
 
40
38
  uri = URI(url)
@@ -56,7 +54,7 @@ class Enviroblyd::Web
56
54
 
57
55
  if response == :retry || (500..599).include?(response.code.to_i)
58
56
  sleep_time = (backoff == :exponential) ? (retry_interval * tries) : retry_interval
59
- $stderr.puts "Retry #{uri} in #{sleep_time}s"
57
+ puts "Retry #{uri} in #{sleep_time}s"
60
58
  sleep sleep_time
61
59
  http(url, type:, params:, retry_interval:, retries:, backoff:, tries: (tries + 1))
62
60
  else
metadata CHANGED
@@ -1,14 +1,14 @@
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.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Starsi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-13 00:00:00.000000000 Z
11
+ date: 2024-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor