sockd 0.1.0 → 0.1.1
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/lib/sockd/runner.rb +38 -4
- data/lib/sockd/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f68f3a2116561ffd7fc5101c67fda661e884e78a
|
4
|
+
data.tar.gz: 46ebcbfd7636d55685c63b7de5a3afe983cbd6e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0e6b7cc336524b6b8a0a83598be660fd361d53c704c496cec8637f57526c1b48c5fbebd1a3201722874ed45673bfdcd5ff6a4e662c20df7d860a6080721c6be5
|
7
|
+
data.tar.gz: e04246c93263fd5f5571d33ab9ed87237f50c73c2c18cb2c88fe2bd3576926fa907ba183e34390673e1343a64d7b83286f21c20c28796c0fb942246642217d82
|
data/lib/sockd/runner.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "logger"
|
2
2
|
require "socket"
|
3
|
+
require "timeout"
|
3
4
|
require "fileutils"
|
4
5
|
require "sockd/errors"
|
5
6
|
|
@@ -62,10 +63,10 @@ module Sockd
|
|
62
63
|
# define our socket handler by providing a block, or trigger the callback
|
63
64
|
# with the provided message
|
64
65
|
# @runner.handle { |msg| if msg == 'foo' then return 'bar' ... }
|
65
|
-
def handle(message = nil, &block)
|
66
|
+
def handle(message = nil, socket = nil, &block)
|
66
67
|
return self if block_given? && @handle = block
|
67
68
|
@handle || (raise SockdError, "No message handler provided.")
|
68
|
-
@handle.call(message,
|
69
|
+
@handle.call(message, socket)
|
69
70
|
end
|
70
71
|
|
71
72
|
# call one of start, stop, restart, or send
|
@@ -98,6 +99,7 @@ module Sockd
|
|
98
99
|
on_interrupt do |signal|
|
99
100
|
log "#{signal} received, shutting down..."
|
100
101
|
teardown
|
102
|
+
cleanup
|
101
103
|
exit 130
|
102
104
|
end
|
103
105
|
|
@@ -157,7 +159,12 @@ module Sockd
|
|
157
159
|
begin
|
158
160
|
# wait for input
|
159
161
|
if IO.select([sock], nil, nil, 2.0)
|
160
|
-
|
162
|
+
msg = sock.recv(256, Socket::MSG_PEEK)
|
163
|
+
if msg.chomp == "ping"
|
164
|
+
sock.print "pong\r\n"
|
165
|
+
else
|
166
|
+
handle msg, sock
|
167
|
+
end
|
161
168
|
else
|
162
169
|
log "connection timed out"
|
163
170
|
end
|
@@ -172,7 +179,25 @@ module Sockd
|
|
172
179
|
# return a UNIXServer or TCPServer instance depending on config
|
173
180
|
def server(&block)
|
174
181
|
if options[:socket]
|
175
|
-
|
182
|
+
begin
|
183
|
+
UNIXServer.open(options[:socket], &block)
|
184
|
+
rescue Errno::EADDRINUSE
|
185
|
+
begin
|
186
|
+
Timeout.timeout(5) do
|
187
|
+
UNIXSocket.open(options[:socket]) do |sock|
|
188
|
+
sock.write "ping\r\n"
|
189
|
+
if sock.gets.chomp == "pong"
|
190
|
+
raise ProcError, "socket #{options[:socket]} already in use by another instance of #{name}"
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
raise ProcError, "socket #{options[:socket]} already in use by another process"
|
195
|
+
rescue Errno::ECONNREFUSED, Timeout::Error
|
196
|
+
log "socket is stale, reopening"
|
197
|
+
cleanup
|
198
|
+
UNIXServer.open(options[:socket], &block)
|
199
|
+
end
|
200
|
+
end
|
176
201
|
else
|
177
202
|
TCPServer.open(options[:host], options[:port], &block)
|
178
203
|
end
|
@@ -193,6 +218,15 @@ module Sockd
|
|
193
218
|
raise ProcError, "unable to open socket: #{sock} (check permissions)"
|
194
219
|
end
|
195
220
|
|
221
|
+
# clean up UNIXSocket upon termination
|
222
|
+
def cleanup
|
223
|
+
if options[:socket] && File.exists?(options[:socket])
|
224
|
+
File.delete(options[:socket])
|
225
|
+
end
|
226
|
+
rescue StandardError
|
227
|
+
raise ProcError, "unable to unlink socket: #{options[:socket]} (check permissions)"
|
228
|
+
end
|
229
|
+
|
196
230
|
# handle process termination signals
|
197
231
|
def on_interrupt(&block)
|
198
232
|
trap("INT") { yield "SIGINT" }
|
data/lib/sockd/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sockd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Greiling
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|