Class: Helper::AsyncCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/helper.rb

Overview

外部コマンド実行中の待機ループの処理を書けるクラス

response = Helper::AsyncCommand.exec(“処理に時間がかかる外部コマンド”) do

print "*"

end if response.success?

puts "成功しました"

end

Class Method Summary collapse

Class Method Details

.exec(command, sleep_time = 0.5, &block) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/helper.rb', line 299

def self.exec(command, sleep_time = 0.5, &block)
  Thread.new {
    loop do
      block.call if block
      sleep(sleep_time)
    end
  }.tap { |th|
    begin
      if Helper.engine_jruby?
        # MEMO:
        #   Open3.capture3 - 全く動かない
        #   `` バッククウォート - 出力が文字化けする
        res = Open3.popen3(command) { |i, o, e|
          i.close
          `cd`   # create dummy Process::Status object to $?
          [o.read, e.read, $?]
        }
      else
        res = Open3.capture3(command)
      end
    ensure
      th.kill
    end
    return res
  }
end