hustle 0.0.3 → 0.0.4

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
  SHA1:
3
- metadata.gz: 0751e2a30bd9eaa27e1a2d91386ac2e708e3dec2
4
- data.tar.gz: b398cd8b489a16bfeb07ad6ecae646dac4633d6d
3
+ metadata.gz: c8fb122e01f5a7a7434a81d4407794b313cd36a4
4
+ data.tar.gz: f90f07d87d1b73ccdd480a0de9be5fbab9c4db2b
5
5
  SHA512:
6
- metadata.gz: 49e168a14e448afe2a261672f0493588219bd86fbb0055374a730fa52bc7d22f33f6479598f0ef7d8516ff75506ebd69a7435fdbad26a86527a7a979dc88fd40
7
- data.tar.gz: 046f513b6dd4045aa34091c854f9fd9e1b88ee099689b3787f6ecdc32d64e10c372a749a8d533610eb69a0ceae6d03d08d103fc7167fca8a06c066d08a1d8201
6
+ metadata.gz: f393b3dfc7c25a44434bc342a33d56a6434d70a7e806083de142eaeaa1c5bb79ad4b56f4e65136e49d2e151a11db05d127dc1e275657c6a547c3744d3fda14a3
7
+ data.tar.gz: f48ebf55c0f69ad9f64917ebfe8ddea5e3446647ac2e7e89e9dd442760e51bab96b179d3ac8522195428bcc00b77a43b41bc2d5dce028412b9c88d9e205ad4f9
data/lib/hustle.rb CHANGED
@@ -1,97 +1,14 @@
1
- require_relative "hustle/version"
2
- require_relative "hustle/runner"
3
- require "os"
4
- require "socket"
5
1
  require "drb"
6
- require "thread"
7
- require "singleton"
8
2
  require "forwardable"
9
- require "monitor"
3
+ require_relative "hustle/version"
4
+ require_relative "hustle/runner"
5
+ require_relative "hustle/hustler"
10
6
 
11
7
  module Hustle
12
8
  class << self
13
9
  extend Forwardable
14
10
  def_delegators :"Hustle::Hustler.instance", :go, :wait
15
11
  end
16
-
17
- class Hustler
18
- include Singleton
19
- include MonitorMixin
20
-
21
- def cores
22
- @cores ||= OS.cpu_count
23
- end
24
-
25
- def active_runners
26
- @active_runners ||= {}
27
- end
28
-
29
- def start_drb
30
- @drb ||= begin
31
- DRb.start_service
32
- sleep 0 while server.is_a?(DRb::DRbServerNotFound)
33
- end
34
- end
35
-
36
- def stop_drb
37
- synchronize do
38
- if active_runners.empty?
39
- DRb.stop_service
40
- @drb = nil
41
- end
42
- end
43
- end
44
-
45
- def go(callback: -> (val) {}, &block)
46
- start_drb
47
- sleep 0 while active_runners.size >= cores.size
48
- uri = "druby://127.0.0.1:#{random_port}"
49
- runner = Runner.new(uri)
50
- runner.start_remote_instance
51
- synchronize do
52
- active_runners[runner.pid] = runner
53
- end
54
- finish runner, callback, &block
55
- end
56
-
57
- def wait
58
- active_runners.each do |_, runner|
59
- runner.callback_thread.join
60
- end
61
- end
62
-
63
- private
64
-
65
- def random_port
66
- socket = Socket.new(:INET, :STREAM, 0)
67
- socket.bind(Addrinfo.tcp("127.0.0.1", 0))
68
- port = socket.local_address.ip_port
69
- socket.close
70
- port
71
- end
72
-
73
- def finish(runner, callback, &block)
74
- runner.callback_thread = Thread.new do
75
- value = runner.run_remote(&block)
76
- runner.stop_remote_instance
77
- synchronize do
78
- active_runners.delete(runner.pid)
79
- end
80
- stop_drb
81
- callback.call value
82
- end
83
- end
84
-
85
- def server
86
- begin
87
- DRb.current_server
88
- rescue DRb::DRbServerNotFound => e
89
- e
90
- end
91
- end
92
-
93
- end
94
-
95
12
  end
96
13
 
97
14
  Signal.trap(0) do
@@ -0,0 +1,85 @@
1
+ require "os"
2
+ require "socket"
3
+ require "thread"
4
+ require "singleton"
5
+ require "monitor"
6
+
7
+ module Hustle
8
+ class Hustler
9
+ include Singleton
10
+ include MonitorMixin
11
+
12
+ def cores
13
+ @cores ||= OS.cpu_count
14
+ end
15
+
16
+ def active_runners
17
+ @active_runners ||= {}
18
+ end
19
+
20
+ def start_drb
21
+ @drb ||= begin
22
+ DRb.start_service
23
+ sleep 0 while server.is_a?(DRb::DRbServerNotFound)
24
+ end
25
+ end
26
+
27
+ def stop_drb
28
+ synchronize do
29
+ if active_runners.empty?
30
+ DRb.stop_service
31
+ @drb = nil
32
+ end
33
+ end
34
+ end
35
+
36
+ def go(callback: -> (val) {}, &block)
37
+ start_drb
38
+ sleep 0 while active_runners.size >= cores.size
39
+ uri = "druby://127.0.0.1:#{random_port}"
40
+ runner = Runner.new(uri)
41
+ runner.start_remote_instance
42
+ synchronize do
43
+ active_runners[runner.pid] = runner
44
+ end
45
+ finish runner, callback, &block
46
+ end
47
+
48
+ def wait
49
+ active_runners.each do |_, runner|
50
+ runner.callback_thread.join
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def random_port
57
+ socket = Socket.new(:INET, :STREAM, 0)
58
+ socket.bind(Addrinfo.tcp("127.0.0.1", 0))
59
+ port = socket.local_address.ip_port
60
+ socket.close
61
+ port
62
+ end
63
+
64
+ def finish(runner, callback, &block)
65
+ runner.callback_thread = Thread.new do
66
+ value = runner.run_remote(&block)
67
+ runner.stop_remote_instance
68
+ synchronize do
69
+ active_runners.delete(runner.pid)
70
+ end
71
+ stop_drb
72
+ callback.call value
73
+ end
74
+ end
75
+
76
+ def server
77
+ begin
78
+ DRb.current_server
79
+ rescue DRb::DRbServerNotFound => e
80
+ e
81
+ end
82
+ end
83
+
84
+ end
85
+ end
data/lib/hustle/runner.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  module Hustle
2
-
3
2
  class Runner
4
3
  attr_reader :uri, :pid, :value
5
4
  attr_accessor :callback_thread
@@ -60,5 +59,4 @@ module Hustle
60
59
  end
61
60
 
62
61
  end
63
-
64
62
  end
@@ -1,3 +1,3 @@
1
1
  module Hustle
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hustle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Hopkins
@@ -88,6 +88,7 @@ extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
90
  - lib/hustle.rb
91
+ - lib/hustle/hustler.rb
91
92
  - lib/hustle/runner.rb
92
93
  - lib/hustle/version.rb
93
94
  - test/hustle_test.rb