ruby-cli-daemon 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/ruby-cli-daemon.sh +11 -14
- data/lib/ruby_cli_daemon/version.rb +1 -1
- data/lib/ruby_cli_daemon.rb +19 -37
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 28032d96f5467e69e05901a8397137c0706c58d78a0db0da86af3472ae44f80f
|
4
|
+
data.tar.gz: 4b48ce0e2ffe83e286551a211853d6cb81d5a317b86758d0a5f7814444d88b81
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cbad1d7fff67fe668a4e3a38b413a7267887abf3cad210f541d063f222809512a7584454546c5ed6301505862708d3437aa1f5e701d01afa83fb33ae4e157ee2
|
7
|
+
data.tar.gz: c10303fb398d4b46429ff1d4353b00be9ad661e2b6aae7e9a189ffed887f2c13834525ae977af3fb3fc9942e5dad383e26c0fedaa9a04f41927b90a8ceb03e67
|
data/bin/ruby-cli-daemon.sh
CHANGED
@@ -50,23 +50,20 @@ fi
|
|
50
50
|
|
51
51
|
# prepare output so we can start tailing
|
52
52
|
status="${socket}.status"
|
53
|
-
|
54
|
-
stderr="${socket}.err"
|
55
|
-
rm -f $status $stdout $stderr # clear previous
|
56
|
-
touch $stdout $stderr
|
53
|
+
rm -f $status # clear previous
|
57
54
|
|
58
|
-
# send
|
59
|
-
|
55
|
+
# send IOs / command / env ... TODO: use perl or awk or bash to be faster ... see experiments/send_io.sh
|
56
|
+
ruby --disable-gems -rsocket -rshellwords -e "
|
57
|
+
s = UNIXSocket.new('$socket')
|
58
|
+
s.send_io STDOUT
|
59
|
+
s.send_io STDERR
|
60
|
+
s.send_io STDIN
|
61
|
+
s.puts ARGV.shelljoin # as a single line <-> gets
|
62
|
+
s.print ENV.map { |k, v| %(#{k} #{v}) }.join('--RCD--')
|
63
|
+
" -- "$@"
|
60
64
|
|
61
|
-
#
|
62
|
-
tail -f $stdout &
|
63
|
-
tail -f $stderr >&2 &
|
64
|
-
|
65
|
-
# wait for command to finish, tight loop so we don't lose time
|
65
|
+
# wait for command to finish, tight loop so we don't lose time TODO: open another socket to be faster/efficient?
|
66
66
|
while [ ! -f $status ]; do sleep 0.02; done
|
67
67
|
|
68
|
-
# kill log streamers, they should be done (but not the spawned worker)
|
69
|
-
for job in `jobs -p | tail -n2`; do kill $job; done
|
70
|
-
|
71
68
|
# replay exit status
|
72
69
|
exit "$(cat $status)"
|
data/lib/ruby_cli_daemon.rb
CHANGED
@@ -17,21 +17,16 @@ module RubyCliDaemon
|
|
17
17
|
server = create_socket(socket) # do this last, it signals we are ready
|
18
18
|
|
19
19
|
loop do
|
20
|
-
return unless (
|
21
|
-
command, env = command
|
22
|
-
# execute the command in a fork
|
23
|
-
capture :STDOUT, "#{socket}.out" do
|
24
|
-
capture :STDERR, "#{socket}.err" do
|
25
|
-
_, status = Process.wait2(fork do
|
26
|
-
ENV.replace env # uncovered
|
27
|
-
ARGV.replace(command) # uncovered
|
28
|
-
load path # uncovered
|
29
|
-
end)
|
20
|
+
return unless IO.select([server], nil, nil, TIMEOUT)
|
30
21
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
22
|
+
# execute the gems binary in a fork
|
23
|
+
_, status = Process.wait2(fork do
|
24
|
+
replace_env server # uncovered
|
25
|
+
load path # uncovered
|
26
|
+
end)
|
27
|
+
|
28
|
+
# send back exit status
|
29
|
+
File.write("#{socket}.status", status.exitstatus)
|
35
30
|
end
|
36
31
|
ensure
|
37
32
|
# signal that this program is done so ruby-sli-daemon.sh restarts it
|
@@ -80,35 +75,22 @@ module RubyCliDaemon
|
|
80
75
|
result.is_a?(StandardError) ? raise(result) : result
|
81
76
|
end
|
82
77
|
|
83
|
-
def
|
84
|
-
return unless IO.select([server], nil, nil, TIMEOUT)
|
85
|
-
|
78
|
+
def replace_env(server)
|
86
79
|
connection = server.accept
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
80
|
+
begin
|
81
|
+
STDOUT.reopen connection.recv_io
|
82
|
+
STDERR.reopen connection.recv_io
|
83
|
+
STDIN.reopen connection.recv_io
|
84
|
+
ARGV.replace connection.gets.shellsplit
|
85
|
+
ENV.replace Hash[connection.read.split("--RCD--").map { |s| s.split(/ /, 2) }]
|
86
|
+
ensure
|
87
|
+
connection.close # not sure if this is necessary
|
88
|
+
end
|
95
89
|
end
|
96
90
|
|
97
91
|
def create_socket(socket)
|
98
92
|
FileUtils.mkdir_p(File.dirname(socket))
|
99
93
|
UNIXServer.new(socket)
|
100
94
|
end
|
101
|
-
|
102
|
-
# StringIO does not work with rubies `system` call that `sh` uses under the hood, so using Tempfile + reopen
|
103
|
-
# https://grosser.it/2018/11/23/ruby-capture-stdout-without-stdout/
|
104
|
-
def capture(stream, path)
|
105
|
-
const = Object.const_get(stream)
|
106
|
-
const.sync = true
|
107
|
-
old_stream = const.dup
|
108
|
-
const.reopen(path)
|
109
|
-
yield
|
110
|
-
ensure
|
111
|
-
const.reopen(old_stream)
|
112
|
-
end
|
113
95
|
end
|
114
96
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-cli-daemon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Grosser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06-
|
11
|
+
date: 2019-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email: michael@grosser.it
|