elf 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/elf.rb +1 -1
- data/lib/elf/child.rb +5 -4
- data/lib/elf/process.rb +20 -7
- data/lib/elf/version.rb +1 -1
- data/spec/elf/process_spec.rb +24 -0
- metadata +2 -2
data/lib/elf.rb
CHANGED
data/lib/elf/child.rb
CHANGED
@@ -2,8 +2,9 @@ module Elf
|
|
2
2
|
class Child
|
3
3
|
attr_reader :success
|
4
4
|
|
5
|
-
def initialize(cmd)
|
5
|
+
def initialize(cmd, comment)
|
6
6
|
@cmd = cmd
|
7
|
+
@comment = comment
|
7
8
|
end
|
8
9
|
|
9
10
|
def on_success(&blk)
|
@@ -14,18 +15,18 @@ module Elf
|
|
14
15
|
end
|
15
16
|
|
16
17
|
def fire_success
|
17
|
-
puts "Success: #{@cmd}"
|
18
|
+
puts "Success: #{@cmd} # #{@comment}"
|
18
19
|
if @success
|
19
20
|
@success.call
|
20
21
|
end
|
21
22
|
end
|
22
23
|
|
23
24
|
def fire_error
|
24
|
-
puts "Failed: #{@cmd}"
|
25
|
+
puts "Failed: #{@cmd} # #{@comment}"
|
25
26
|
end
|
26
27
|
|
27
28
|
def fire
|
28
|
-
puts "Fired: #{@cmd}"
|
29
|
+
puts "Fired: #{@cmd} # #{@comment}"
|
29
30
|
fork_pid = ::Process.fork do
|
30
31
|
exec @cmd
|
31
32
|
end
|
data/lib/elf/process.rb
CHANGED
@@ -1,20 +1,33 @@
|
|
1
1
|
module Elf
|
2
2
|
class Process
|
3
3
|
def initialize(&blk)
|
4
|
-
|
4
|
+
@pids = []
|
5
|
+
@pids << blk.call(self)
|
5
6
|
::Process.waitall
|
7
|
+
ensure
|
8
|
+
# killall!
|
6
9
|
end
|
7
10
|
|
8
|
-
def fork(cmd)
|
9
|
-
elf = Elf::Fork.new(cmd)
|
11
|
+
def fork(cmd, comment=nil)
|
12
|
+
elf = Elf::Fork.new(cmd, comment)
|
10
13
|
yield elf if block_given?
|
11
|
-
|
14
|
+
@pids << elf.fire
|
12
15
|
end
|
13
16
|
|
14
|
-
def sync(cmd)
|
15
|
-
elf = Elf::Sync.new(cmd)
|
17
|
+
def sync(cmd, comment=nil)
|
18
|
+
elf = Elf::Sync.new(cmd, comment)
|
16
19
|
yield elf if block_given?
|
17
|
-
elf.fire
|
20
|
+
@pids << elf.fire
|
21
|
+
end
|
22
|
+
|
23
|
+
def killall!
|
24
|
+
@pids.each do |pid|
|
25
|
+
begin
|
26
|
+
::Process.kill(9, pid)
|
27
|
+
rescue ::Errno::ESRCH
|
28
|
+
# ok
|
29
|
+
end
|
30
|
+
end
|
18
31
|
end
|
19
32
|
end
|
20
33
|
end
|
data/lib/elf/version.rb
CHANGED
data/spec/elf/process_spec.rb
CHANGED
@@ -95,4 +95,28 @@ describe Elf::Process do
|
|
95
95
|
end
|
96
96
|
(Time.now-start).to_i.must_equal(6)
|
97
97
|
end
|
98
|
+
|
99
|
+
it "should work with sync jobs in success callback" do
|
100
|
+
start = Time.now
|
101
|
+
Elf::Process.new do |elf|
|
102
|
+
elf.fork("sleep 1", "first async") do |f|
|
103
|
+
f.on_success do
|
104
|
+
elf.sync("sleep 1", "first sync callback")
|
105
|
+
elf.sync("sleep 1", "second sync callback")
|
106
|
+
end
|
107
|
+
end
|
108
|
+
elf.fork("sleep 1", "second async") do |f|
|
109
|
+
f.on_success do
|
110
|
+
elf.fork("sleep 1", "first async callback") do |f1|
|
111
|
+
f1.on_success do
|
112
|
+
elf.fork("sleep 1", "one more async call")
|
113
|
+
elf.sync("sleep 1", "one more sync call")
|
114
|
+
end
|
115
|
+
end
|
116
|
+
elf.fork("sleep 1", "second async callback")
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
(Time.now-start).to_i.must_equal(4)
|
121
|
+
end
|
98
122
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-19 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: Elves are creatures to manage background processes
|
15
15
|
email:
|