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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e7ecbd1e3b5a79a58076357cd42643f4c791dfde3d7c18863f907ceb82956e58
4
- data.tar.gz: 8ae5d956213df23d0022ac8f8f57a6778fa3988fd9512565008e501f45bc4efd
3
+ metadata.gz: 28032d96f5467e69e05901a8397137c0706c58d78a0db0da86af3472ae44f80f
4
+ data.tar.gz: 4b48ce0e2ffe83e286551a211853d6cb81d5a317b86758d0a5f7814444d88b81
5
5
  SHA512:
6
- metadata.gz: 8305d8f10bc7dd72bab13cbfd371e877d7d80fa4e1451c8dcf68d1e0da47fb69698554981a34be57e2cb17326974748b69fbbf8a44c9ada7eaa45e57f0e87df4
7
- data.tar.gz: 8719ebd454a9917ade205b17e2d1422e6fccac6c4264117e8a75235da1d8a674b6b6397ec8063d71fd5d38bba060e591baa9ac2729cb11e27fbce06de3c002d9
6
+ metadata.gz: cbad1d7fff67fe668a4e3a38b413a7267887abf3cad210f541d063f222809512a7584454546c5ed6301505862708d3437aa1f5e701d01afa83fb33ae4e157ee2
7
+ data.tar.gz: c10303fb398d4b46429ff1d4353b00be9ad661e2b6aae7e9a189ffed887f2c13834525ae977af3fb3fc9942e5dad383e26c0fedaa9a04f41927b90a8ceb03e67
@@ -50,23 +50,20 @@ fi
50
50
 
51
51
  # prepare output so we can start tailing
52
52
  status="${socket}.status"
53
- stdout="${socket}.out"
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 the command and parsable env vars to the daemon
59
- { echo $@; awk 'BEGIN{for(v in ENVIRON) printf "--RCD-- %s %s", v, ENVIRON[v] }';} | nc -U $socket
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
- # stream output
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)"
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module RubyCliDaemon
3
- VERSION = "0.4.1"
3
+ VERSION = "0.5.0"
4
4
  end
@@ -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 (command = wait_for_command(server))
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
- # send back response
32
- File.write("#{socket}.status", status.exitstatus)
33
- end
34
- end
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 wait_for_command(server)
84
- return unless IO.select([server], nil, nil, TIMEOUT)
85
-
78
+ def replace_env(server)
86
79
  connection = server.accept
87
- command = connection.gets.shellsplit
88
-
89
- env = connection.read.split("--RCD-- ")
90
- env.shift
91
- env = Hash[env.map { |s| s.split(/ /, 2) }]
92
-
93
- connection.close
94
- [command, env]
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.1
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-06 00:00:00.000000000 Z
11
+ date: 2019-06-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: michael@grosser.it