bg 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/bg.rb +13 -0
- data/lib/bg/drb_runner.rb +36 -0
- data/lib/bg/runner.rb +62 -0
- data/lib/bg/version.rb +3 -0
- data/test/runner_test.rb +21 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c84b2acf5bc22423b5a47b7705fd9f20022fab38
|
4
|
+
data.tar.gz: 087b132f0f6ad791d32608192b7d8676325649e5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ee2a7e4363d06db876253b5383514c82e66b82582a73db39c95d8edd9fd7d2042c7ce080a2d47bd5ab47618f001954aa9d2d2afe21ee1cfa79eb49a3df5ccf7e
|
7
|
+
data.tar.gz: 993822472d6d798aa60acbcfb79bbaf0c814663ed72ab4fae54f48bbfd01c8ccd53c7341b54263e8d7c605cc71344b6c7b79ebf6db1498b3413b63e5ddbd1505
|
data/lib/bg.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require "thread"
|
2
|
+
require "drb"
|
3
|
+
require "logger"
|
4
|
+
|
5
|
+
module Bg
|
6
|
+
class DrbRunner
|
7
|
+
attr_reader :logger
|
8
|
+
|
9
|
+
def initialize(logfile: logfile)
|
10
|
+
@logger = Logger.new(logfile || "/dev/null")
|
11
|
+
end
|
12
|
+
|
13
|
+
def ready?
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
def run(proc: nil, args: [])
|
18
|
+
Thread.new do
|
19
|
+
begin
|
20
|
+
sleep 0
|
21
|
+
method = "define_method :run #{proc}"
|
22
|
+
runner = Class.new { eval method }
|
23
|
+
logger.info "Start exec: #{method}"
|
24
|
+
runner.new.run(*Marshal.load(args))
|
25
|
+
logger.info "Finish exec: #{method}"
|
26
|
+
rescue Exception => e
|
27
|
+
logger.error "Failed exec: #{method}\n#{e}"
|
28
|
+
ensure
|
29
|
+
DRb.stop_service rescue nil
|
30
|
+
Process.exit
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
data/lib/bg/runner.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require "method_source"
|
2
|
+
require "thread"
|
3
|
+
require "drb"
|
4
|
+
require "bg/drb_runner"
|
5
|
+
|
6
|
+
module Bg
|
7
|
+
class Runner
|
8
|
+
attr_reader :logfile, :drb_uri, :drb_pid, :drb_runner
|
9
|
+
|
10
|
+
def initialize(logfile: nil)
|
11
|
+
@logfile = logfile
|
12
|
+
@drb_uri = "druby://127.0.0.1:#{random_port}"
|
13
|
+
end
|
14
|
+
|
15
|
+
def run(*args, &block)
|
16
|
+
DRb.start_service
|
17
|
+
start_drb_runner
|
18
|
+
drb_runner.run(
|
19
|
+
proc: proc_string(block),
|
20
|
+
args: Marshal.dump(args)
|
21
|
+
)
|
22
|
+
DRb.stop_service
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def proc_string(proc)
|
28
|
+
code = proc.source
|
29
|
+
index = code.index(/\{|do/)
|
30
|
+
code[index..-1].strip
|
31
|
+
end
|
32
|
+
|
33
|
+
def start_drb_runner
|
34
|
+
return if drb_runner
|
35
|
+
@drb_pid = fork do
|
36
|
+
DRb.start_service drb_uri, DrbRunner.new(logfile: logfile)
|
37
|
+
DRb.thread.join
|
38
|
+
end
|
39
|
+
Process.detach drb_pid
|
40
|
+
@drb_runner = DRbObject.new_with_uri(drb_uri)
|
41
|
+
sleep 0.001 while !drb_runner_ready?
|
42
|
+
drb_runner
|
43
|
+
end
|
44
|
+
|
45
|
+
def drb_runner_ready?
|
46
|
+
begin
|
47
|
+
drb_runner.ready?
|
48
|
+
rescue DRb::DRbConnError
|
49
|
+
false
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def random_port
|
54
|
+
socket = Socket.new(:INET, :STREAM, 0)
|
55
|
+
socket.bind(Addrinfo.tcp("127.0.0.1", 0))
|
56
|
+
port = socket.local_address.ip_port
|
57
|
+
socket.close
|
58
|
+
port
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
data/lib/bg/version.rb
ADDED
data/test/runner_test.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "micro_test"
|
2
|
+
require "pry"
|
3
|
+
require_relative "../lib/bg"
|
4
|
+
|
5
|
+
module Bg
|
6
|
+
class RunnerTest < MicroTest::Test
|
7
|
+
|
8
|
+
before do
|
9
|
+
Bg.logfile = File.expand_path("../../log/test.log", __FILE__)
|
10
|
+
end
|
11
|
+
|
12
|
+
test "3 sleeps" do
|
13
|
+
Bg.run 1, 2, 3 do |a, b, c|
|
14
|
+
sleep a
|
15
|
+
sleep b
|
16
|
+
sleep c
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bg
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nathan Hopkins
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: os
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.9.6
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.9.6
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: method_source
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.8.2
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.8.2
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: micro_test
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.4.4
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.4.4
|
97
|
+
description: Easily run code in a separate background process.
|
98
|
+
email:
|
99
|
+
- natehop@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- lib/bg.rb
|
105
|
+
- lib/bg/drb_runner.rb
|
106
|
+
- lib/bg/runner.rb
|
107
|
+
- lib/bg/version.rb
|
108
|
+
- test/runner_test.rb
|
109
|
+
homepage: https://github.com/hopsoft/bg
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 2.2.0
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: Easily run code in a separate background process.
|
133
|
+
test_files:
|
134
|
+
- test/runner_test.rb
|