pigato 0.1.1 → 0.1.2
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 +4 -4
- data/examples/echo_worker.rb +12 -0
- data/lib/pigato/version.rb +1 -1
- data/lib/pigato/worker.rb +111 -0
- data/lib/pigato.rb +1 -0
- data/pigato.gemspec +1 -0
- metadata +18 -2
- /data/examples/{client.rb → echo_client.rb} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 084cf3ce0c185586f92f4801285553e68e23085d
|
4
|
+
data.tar.gz: bdffbb4efc8e86d3ffb2ed669fade66987c4e8a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6a2f6ccb48abb66956e8695079b07e53ca372b7ecb12ae6558935c5e3d10c4dda831a6035b0d055d401199bfec1ea894b84c0d1d8cd11db37896914437e1375
|
7
|
+
data.tar.gz: 427d7dec7fe448f198a72f3c2c0eff322bf9729dd30c5d8724e3bd83d12fb1bb8a81b38bdaf5cf55305c78f2fa4cb8fc5451b68c96ab17bb65761e48ef4e882b
|
data/lib/pigato/version.rb
CHANGED
@@ -0,0 +1,111 @@
|
|
1
|
+
require "json"
|
2
|
+
require "ffi-rzmq"
|
3
|
+
|
4
|
+
class PigatoWorker
|
5
|
+
HEARTBEAT_LIVENESS = 3 # 3-5 is reasonable
|
6
|
+
|
7
|
+
def initialize broker, service
|
8
|
+
@broker = broker
|
9
|
+
@service = service
|
10
|
+
@context = ZMQ::Context.new(1)
|
11
|
+
@poller = ZMQ::Poller.new
|
12
|
+
@worker = nil # Socket to broker
|
13
|
+
@heartbeat_at = 0 # When to send HEARTBEAT (relative to time.time(), so in seconds)
|
14
|
+
@liveness = 0 # How many attempts left
|
15
|
+
@timeout = 2500
|
16
|
+
@heartbeat = 2500 # Heartbeat delay, msecs
|
17
|
+
@reconnect = 2500 # Reconnect delay, msecs
|
18
|
+
|
19
|
+
@reply_to = nil
|
20
|
+
@reply_rid = nil
|
21
|
+
@reply_service = nil
|
22
|
+
|
23
|
+
reconnect_to_broker
|
24
|
+
end
|
25
|
+
|
26
|
+
def reply reply
|
27
|
+
reply = [@reply_to, '', @reply_rid, '0'].concat([reply.to_json])
|
28
|
+
send_to_broker Pigato::W_REPLY, reply, nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def recv reply
|
32
|
+
loop do
|
33
|
+
@reply_rid = nil
|
34
|
+
@reply_to = nil
|
35
|
+
@reply_service = nil
|
36
|
+
|
37
|
+
items = @poller.poll(@timeout)
|
38
|
+
if items
|
39
|
+
messages = []
|
40
|
+
@worker.recv_strings messages
|
41
|
+
|
42
|
+
@liveness = HEARTBEAT_LIVENESS
|
43
|
+
|
44
|
+
header = messages.shift
|
45
|
+
if header != Pigato::W_WORKER
|
46
|
+
puts "E: Header is not Pigato::WORKER"
|
47
|
+
end
|
48
|
+
|
49
|
+
command = messages.shift
|
50
|
+
|
51
|
+
case command
|
52
|
+
when Pigato::W_REQUEST
|
53
|
+
# We should pop and save as many addresses as there are
|
54
|
+
# up to a null part, but for now, just save one...
|
55
|
+
puts "REQUEST"
|
56
|
+
@reply_to = messages.shift
|
57
|
+
@reply_service = messages.shift
|
58
|
+
messages.shift # empty
|
59
|
+
@reply_rid = messages.shift
|
60
|
+
return messages[0] # We have a request to process
|
61
|
+
when Pigato::W_HEARTBEAT
|
62
|
+
# do nothing
|
63
|
+
when Pigato::W_DISCONNECT
|
64
|
+
reconnect_to_broker
|
65
|
+
else
|
66
|
+
end
|
67
|
+
else
|
68
|
+
@liveness -= 1
|
69
|
+
if @liveness == 0
|
70
|
+
sleep 0.001*@reconnect
|
71
|
+
reconnect_to_broker
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
if Time.now > @heartbeat_at
|
76
|
+
send_to_broker Pigato::W_HEARTBEAT
|
77
|
+
@heartbeat_at = Time.now + 0.001 * @heartbeat
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def reconnect_to_broker
|
84
|
+
if @worker
|
85
|
+
@poller.deregister @worker, ZMQ::DEALER
|
86
|
+
@worker.close
|
87
|
+
end
|
88
|
+
|
89
|
+
@worker = @context.socket ZMQ::DEALER
|
90
|
+
@worker.setsockopt ZMQ::IDENTITY, 'WRK01'
|
91
|
+
@worker.setsockopt ZMQ::LINGER, 0
|
92
|
+
@worker.connect @broker
|
93
|
+
@poller.register @worker, ZMQ::POLLIN
|
94
|
+
send_to_broker(Pigato::W_READY, @service, [])
|
95
|
+
@liveness = HEARTBEAT_LIVENESS
|
96
|
+
@heartbeat_at = Time.now + 0.001 * @heartbeat
|
97
|
+
end
|
98
|
+
|
99
|
+
def send_to_broker command, message=nil, options=nil
|
100
|
+
if message.nil?
|
101
|
+
message = []
|
102
|
+
elsif not message.is_a?(Array)
|
103
|
+
message = [message]
|
104
|
+
end
|
105
|
+
|
106
|
+
message = [Pigato::W_WORKER, command].concat message
|
107
|
+
message = message.concat(options) if options
|
108
|
+
|
109
|
+
@worker.send_strings message
|
110
|
+
end
|
111
|
+
end
|
data/lib/pigato.rb
CHANGED
data/pigato.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pigato
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paolo Ardoino
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: ffi-rzmq
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
41
55
|
description: PIGATO-RUBY
|
42
56
|
email:
|
43
57
|
- paolo.ardoino@gmail.com
|
@@ -53,11 +67,13 @@ files:
|
|
53
67
|
- Rakefile
|
54
68
|
- bin/console
|
55
69
|
- bin/setup
|
56
|
-
- examples/
|
70
|
+
- examples/echo_client.rb
|
71
|
+
- examples/echo_worker.rb
|
57
72
|
- lib/pigato.rb
|
58
73
|
- lib/pigato/client.rb
|
59
74
|
- lib/pigato/proto.rb
|
60
75
|
- lib/pigato/version.rb
|
76
|
+
- lib/pigato/worker.rb
|
61
77
|
- pigato.gemspec
|
62
78
|
homepage: https://github.com/prdn/pigato
|
63
79
|
licenses:
|
File without changes
|