ruby-cli-daemon 0.2.0 → 0.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 01a6d3c7d2c60a46410e956b907597e743cd1358cf6ea479bb472b3fe150f52f
4
- data.tar.gz: a65afda0b9ba23674c2a575126241be29dea66026a6fcfdaa3c3593666718a8c
3
+ metadata.gz: f8c5509e29395a1de3c0f720971cb4a645cd6c4d5801cb876eaaf224c40a699a
4
+ data.tar.gz: '0659e79f4e92b56d5a27832321d2ac7b9f40fb5c59a84391689a340661f67900'
5
5
  SHA512:
6
- metadata.gz: 116a867d73b9bae3d739cfc89383bf3507ab77c22ca77e6c64f9695075c79ce079c0a45edf4a0668a3469a8efc5cf897f7466cc8b9bc9e06a114b2331427ee9c
7
- data.tar.gz: 1a49ca90bc902b26b9abb39e230e612091e78037c1339cb65ac3c3c035e778def57ada09f7cccedc26aa845f96abac5e86af67491fa7750140fc8a9122b22a08
6
+ metadata.gz: aa4a1dbf9d6ba2eb48bf9778d05dafebe699b2304b4a218d4dd935f814dd4b8a16c66256e818b0f16efa45ddb0a91a0f57865d637f990d2c30fcf17baef4683e
7
+ data.tar.gz: 97c2e4082bc8f967810738ca4790a828dec80c1c1247ebfc2012ded362d5f3332491d027db27a6ef3e917d3c13514ac46dcd76858d3a86478bded5cd688b55d6
@@ -11,7 +11,7 @@ stop)
11
11
  -v|--version)
12
12
  exec ruby -r$lib/ruby_cli_daemon/version.rb -e "puts RubyCliDaemon::VERSION"
13
13
  ;;
14
- -h|--help)
14
+ ""|-*)
15
15
  echo "Usage:"
16
16
  echo " ruby-cli-daemon <ruby-executable> [arg]*"
17
17
  echo " Start or use background worker to execute command"
@@ -23,7 +23,11 @@ stop)
23
23
  echo "Options:"
24
24
  echo " -v / --version Show version"
25
25
  echo " -h / --help Show this help"
26
- exit
26
+ if [[ "$1" = "-h" || "$1" = "--help" ]]; then
27
+ exit 0
28
+ else
29
+ exit 1
30
+ fi
27
31
  ;;
28
32
  esac
29
33
 
@@ -37,10 +41,10 @@ log=${TMPDIR}ruby_cli_daemon.log
37
41
  # spawn new daemon if none exists
38
42
  if [[ ! -e $socket ]]; then
39
43
  # absolute executable so a single gem install is enough for all rubies
40
- nohup ruby -r$lib/ruby_cli_daemon.rb -rbundler/setup -e RubyCliDaemon.start\ \"$socket\",\ \"$executable\" 0<&- &>$log &
44
+ nohup ruby -r$lib/ruby_cli_daemon.rb -e RubyCliDaemon.start\ \"$socket\",\ \"$executable\" 0<&- &>$log &
41
45
  while [ ! -e $socket ]; do
42
46
  sleep 0.1
43
- kill -0 $(jobs -p) || (echo "Failed to start worker, check $log" && false) # fail fast when worker failed
47
+ kill -0 $(jobs -p) &>/dev/null || (cat $log && false) # fail fast when worker failed
44
48
  done
45
49
  fi
46
50
 
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module RubyCliDaemon
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
@@ -13,8 +13,8 @@ module RubyCliDaemon
13
13
  end
14
14
 
15
15
  def start(socket, executable)
16
- server = create_socket(socket)
17
16
  path = preload_gem(executable)
17
+ server = create_socket(socket) # do this last, it signals we are ready
18
18
 
19
19
  loop do
20
20
  return unless (command = wait_for_command(server))
@@ -39,15 +39,46 @@ module RubyCliDaemon
39
39
 
40
40
  private
41
41
 
42
- # preload the libraries we'll need to speed up execution
42
+ # preload the libraries we'll need, to speed up execution
43
+ # first try with bundler and then without
43
44
  def preload_gem(executable)
44
- spec = Gem.loaded_specs.each_value.detect { |s| s.executables.include?(executable) }
45
- path = spec.bin_file executable
46
- require spec.name
45
+ name, path = fork_with_return do
46
+ require "bundler/setup"
47
+ find_gem_spec(executable)
48
+ end
49
+
50
+ if name
51
+ require "bundler/setup"
52
+ else
53
+ name, path = find_gem_spec(executable)
54
+ raise "No gem with executable #{executable} found" unless name
55
+ end
56
+
57
+ require name
47
58
  GC.start # https://bugs.ruby-lang.org/issues/15878
48
59
  path
49
60
  end
50
61
 
62
+ def find_gem_spec(executable)
63
+ spec = Gem::Specification.detect { |s| s.executables.include?(executable) }
64
+ [spec.name, spec.bin_file(executable)] if spec # need something we can send out from fork
65
+ end
66
+
67
+ def fork_with_return
68
+ read, write = IO.pipe
69
+ Process.wait(fork do
70
+ read.close
71
+ begin
72
+ Marshal.dump(yield, write)
73
+ rescue StandardError => e
74
+ Marshal.dump(e, write)
75
+ end
76
+ end)
77
+ write.close
78
+ result = Marshal.load(read)
79
+ result.is_a?(StandardError) ? raise(result) : result
80
+ end
81
+
51
82
  def wait_for_command(server)
52
83
  return unless IO.select([server], nil, nil, TIMEOUT)
53
84
  connection = server.accept
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.2.0
4
+ version: 0.3.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-05-30 00:00:00.000000000 Z
11
+ date: 2019-06-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: michael@grosser.it
@@ -39,8 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  requirements: []
42
- rubyforge_project:
43
- rubygems_version: 2.7.6
42
+ rubygems_version: 3.0.3
44
43
  signing_key:
45
44
  specification_version: 4
46
45
  summary: Preforking daemon that makes all ruby binaries faster