| Class | Shell::SystemCommand |
| In: |
lib/shell/system-command.rb
|
| Parent: | Filter |
| command | -> | name |
| each | -> | super_each |
| command | [R] |
# File lib/shell/system-command.rb, line 17
17: def initialize(sh, command, *opts)
18: if t = opts.find{|opt| !opt.kind_of?(String) && opt.class}
19: Shell.Fail Error::TypeError, t.class, "String"
20: end
21: super(sh)
22: @command = command
23: @opts = opts
24:
25: @input_queue = Queue.new
26: @pid = nil
27:
28: sh.process_controller.add_schedule(self)
29: end
# File lib/shell/system-command.rb, line 38
38: def active?
39: @shell.process_controller.active_job?(self)
40: end
# File lib/shell/system-command.rb, line 144
144: def each(rs = nil)
145: while (l = @input_queue.pop) != :EOF
146: yield l
147: end
148: end
# File lib/shell/system-command.rb, line 60
60: def flush
61: @pipe_out.flush if @pipe_out and !@pipe_out.closed?
62: end
# File lib/shell/system-command.rb, line 42
42: def input=(inp)
43: super
44: if active?
45: start_export
46: end
47: end
# File lib/shell/system-command.rb, line 75
75: def kill(sig)
76: if @pid
77: Process.kill(sig, @pid)
78: end
79: end
ex)
if you wish to output:
"shell: job(#{@command}:#{@pid}) close pipe-out."
then
mes: "job(%id) close pipe-out."
yorn: Boolean(@shell.debug? or @shell.verbose?)
# File lib/shell/system-command.rb, line 156
156: def notify(*opts, &block)
157: Thread.exclusive do
158: @shell.notify(*opts) {|mes|
159: yield mes if iterator?
160:
161: mes.gsub!("%id", "#{@command}:##{@pid}")
162: mes.gsub!("%name", "#{@command}")
163: mes.gsub!("%pid", "#{@pid}")
164: }
165: end
166: end
# File lib/shell/system-command.rb, line 49
49: def start
50: @pid, @pipe_in, @pipe_out = @shell.process_controller.sfork(self) {
51: Dir.chdir @shell.pwd
52: exec(@command, *@opts)
53: }
54: if @input
55: start_export
56: end
57: start_import
58: end
# File lib/shell/system-command.rb, line 116
116: def start_export
117: notify "job(%id) start exp-pipe.", @shell.debug?
118: _eop = true
119: th = Thread.start{
120: Thread.critical = true
121: begin
122: Thread.critical = false
123: @input.each{|l| @pipe_out.print l}
124: _eop = false
125: rescue Errno::EPIPE
126: _eop = false
127: ensure
128: if _eop
129: notify("shell: warn: Process finishing...",
130: "wait for Job(%id) to finish pipe exporting.",
131: "You can use Shell#transact or Shell#check_point for more safe execution.")
132: # Tracer.on
133: redo
134: end
135: Thread.exclusive do
136: notify "job(%id) close exp-pipe.", @shell.debug?
137: @pipe_out.close
138: end
139: end
140: }
141: end
# File lib/shell/system-command.rb, line 82
82: def start_import
83: # Thread.critical = true
84: notify "Job(%id) start imp-pipe.", @shell.debug?
85: rs = @shell.record_separator unless rs
86: _eop = true
87: # Thread.critical = false
88: th = Thread.start {
89: Thread.critical = true
90: begin
91: Thread.critical = false
92: while l = @pipe_in.gets
93: @input_queue.push l
94: end
95: _eop = false
96: rescue Errno::EPIPE
97: _eop = false
98: ensure
99: if _eop
100: notify("warn: Process finishing...",
101: "wait for Job[%id] to finish pipe importing.",
102: "You can use Shell#transact or Shell#check_point for more safe execution.")
103: # Tracer.on
104: Thread.current.run
105: redo
106: end
107: Thread.exclusive do
108: notify "job(%id}) close imp-pipe.", @shell.debug?
109: @input_queue.push :EOF
110: @pipe_in.close
111: end
112: end
113: }
114: end