rubypitaya 1.3.1 → 1.4.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: 47e0970be5b8e403375046ab301bf90738610d336c94942e29512c54d9100d36
4
- data.tar.gz: 3d5ca8e24fc63223516723a6db06f85a3f0228bde29a58f88187675bdf82e78c
3
+ metadata.gz: 3fe7572b5e800053db6b8d837220cd94b1fbe34c3d7b31b712bd009adc81cfa0
4
+ data.tar.gz: 5d9b2a19b1f7884982755de17477c74703da673a570c826dcf34dda450b58db1
5
5
  SHA512:
6
- metadata.gz: 39246abfef21adb49e4434f7500d7577998e061a7f735ea3d394b3d7ffded21a6a95394146dbd29abd7dce45300ea1c3c8db7ebe9bf7b9657bef1b75a2b40b3c
7
- data.tar.gz: b1de3be06e60907f611e2dce20161f02a1aaf5cf98a97b4c3330859cbb8d8266d572bde3c16f459612a0ecc8f6b4414dbc9918ddcea3c5e2562d30e4f4471e8e
6
+ metadata.gz: 4b840698bd22dcca93067960b568e836bf586d6cb1e25289ad16d432161380f85bd087728edfc4678f68daf41e73ca63ea7a51db4ee79169db60a68c7862aa1d
7
+ data.tar.gz: 0af3191c1ac731265e3e8f16d085bdcea0c144865cb2b4a2239a5a27a7f5adcd264679cc79045abfcbe40420a75086d592dd6f7b529d55ff7052c087b954bf58
@@ -1,6 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem 'rubypitaya', '1.3.1'
3
+ gem 'rubypitaya', '1.4.0'
4
4
 
5
5
  group :development do
6
6
  gem 'pry', '0.12.2'
@@ -62,7 +62,7 @@ GEM
62
62
  diff-lcs (>= 1.2.0, < 2.0)
63
63
  rspec-support (~> 3.8.0)
64
64
  rspec-support (3.8.3)
65
- rubypitaya (1.3.1)
65
+ rubypitaya (1.4.0)
66
66
  activerecord (= 6.0.2)
67
67
  etcdv3 (= 0.10.2)
68
68
  eventmachine (= 1.2.7)
@@ -85,7 +85,7 @@ DEPENDENCIES
85
85
  pry (= 0.12.2)
86
86
  rake (= 10.0)
87
87
  rspec (= 3.8.0)
88
- rubypitaya (= 1.3.1)
88
+ rubypitaya (= 1.4.0)
89
89
 
90
90
  BUNDLED WITH
91
91
  1.17.2
@@ -63,6 +63,7 @@ services:
63
63
  NATS_URL: 'nats://nats:4222'
64
64
  ETCD_URL: 'http://etcd:2379'
65
65
  ETCD_PREFIX: 'rubypitaya/'
66
+ ETCD_LEASE_SECONDS: '10'
66
67
  REDIS_URL: 'redis://redis:6379'
67
68
  DATABASE_HOST: 'db'
68
69
  DATABASE_USER: 'postgres'
@@ -6,35 +6,71 @@ module RubyPitaya
6
6
  class EtcdConnector
7
7
 
8
8
  def initialize(server_uuid, desktop_name, server_name, etcd_prefix,
9
- etcd_address, allow_reconnect)
9
+ etcd_address, allow_reconnect, lease_seconds)
10
10
  @server_uuid = server_uuid
11
11
  @server_name = server_name
12
12
  @desktop_name = desktop_name
13
13
  @etcd_prefix = etcd_prefix
14
14
  @etcd_address = etcd_address
15
15
  @allow_reconnect = allow_reconnect
16
+ @lease_seconds = lease_seconds
17
+
18
+ @renew_connection_seconds = lease_seconds / 2.0
19
+
20
+ @renew_connection_key = nil
21
+ @renew_connection_value = nil
22
+ @renew_connection_thread = nil
16
23
  end
17
24
 
18
25
  def connect
26
+ connection_key = get_connection_key
27
+ connection_value = get_connection_value
28
+
19
29
  @connection = Etcdv3.new(endpoints: @etcd_address,
20
30
  allow_reconnect: @allow_reconnect)
21
- @connection.put(connection_key, connection_value)
31
+
32
+ @lease = @connection.lease_grant(@lease_seconds)
33
+
34
+ @connection.put(connection_key, connection_value, lease: @lease.ID)
35
+
36
+ @renew_connection_key = connection_key
37
+ @renew_connection_value = connection_value
38
+
39
+ renew_connection
22
40
  end
23
41
 
24
42
  def disconnect
25
- @connection.del(connection_key)
43
+ @connection.del(get_connection_key)
26
44
  end
27
45
 
28
46
  private
29
47
 
30
- def connection_key
48
+ def get_connection_key
31
49
  "#{@etcd_prefix}servers/#{@server_name}/#{@server_uuid}"
32
50
  end
33
51
 
34
- def connection_value
52
+ def get_connection_value
35
53
  JSON.generate(get_server_data)
36
54
  end
37
55
 
56
+ def renew_connection
57
+ stop_renew_connection
58
+
59
+ @renew_connection_thread = Thread.new do
60
+ loop do
61
+ sleep(@renew_connection_seconds)
62
+
63
+ @lease = @connection.lease_grant(@lease_seconds)
64
+ @connection.put(@renew_connection_key, @renew_connection_value,
65
+ lease: @lease.ID)
66
+ end
67
+ end
68
+ end
69
+
70
+ def stop_renew_connection
71
+ @renew_connection_thread.exit unless @renew_connection_thread.nil?
72
+ end
73
+
38
74
  def get_server_data
39
75
  {
40
76
  id: @server_uuid,
@@ -38,10 +38,12 @@ module RubyPitaya
38
38
 
39
39
  @etcd_prefix = ENV['ETCD_PREFIX']
40
40
  @etcd_address = ENV['ETCD_URL']
41
+ @etcd_lease_seconds = ENV['ETCD_LEASE_SECONDS'].to_i
41
42
  @allow_reconnect = false
42
43
  @etcd_connector = EtcdConnector.new(@service_uuid, @desktop_name,
43
44
  @server_name, @etcd_prefix,
44
- @etcd_address, @allow_reconnect)
45
+ @etcd_address, @allow_reconnect,
46
+ @etcd_lease_seconds)
45
47
  @etcd_connector.connect
46
48
 
47
49
  @nats_address = ENV['NATS_URL']
@@ -1,4 +1,4 @@
1
1
  module RubyPitaya
2
- VERSION = "1.3.1"
2
+ VERSION = "1.4.0"
3
3
  end
4
4
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubypitaya
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luciano Prestes Cavalcanti
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-01 00:00:00.000000000 Z
11
+ date: 2020-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg