Method: ALib::Util#lspawn

Defined in:
lib/alib-0.5.1/util.rb

#lspawn(cmd, opts = {}) ⇒ Object

–}}}



1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
# File 'lib/alib-0.5.1/util.rb', line 1377

def lspawn cmd, opts = {}
#--{{{
  begin
    Thread.critical = true

    stdin = ALib::Util::getopt ['stdin', 'i', 'in', '0', 0], opts 
    stdout = ALib::Util::getopt ['stdout', 'o', 'out', '1', 1], opts
    stderr = ALib::Util::getopt ['stderr', 'e', 'err', '2', 2], opts
    expect = ALib::Util::getopt ['expect', 'status', 'success'], opts, 0
    log = Alib::Util::getopt ['logger', 'log'], opts 

    log ||= (self.logger rescue (defined?(@logger) ? @logger : LSPAWN_LOG[0]))

    stdout = LSPAWN_IO.new log, :o, stdout
    stderr = LSPAWN_IO.new log, :e, stderr

    cmdno = LSPAWN_CMDNO[0]

    log.info{ "cmd[#{ cmdno }] <#{ cmd }>" }

    status = Alib::Util::spawn(cmd, 'quiet'=>true, 'raise'=>false, 'stdin'=>stdin, 'stdout'=>stdout, 'stderr'=>stderr)

    exitstatus = ( (status ? (status.exitstatus || 127) : 127) rescue 127 )

    log.send(exitstatus == 0 ? 'info' : 'warn'){ "status[#{ cmdno }] <#{ exitstatus }>" }

    if expect
      codes = [*expect].flatten.compact.map{|code| Integer code}
      success = codes.detect{|code| exitstatus == code}
      unless success
        log.fatal{ "cmd[#{ cmdno }] failure" }
        exit exitstatus
      end
    end

    if status and status.signaled?
      if status.termsig
        log.warn{ "termsig[#{ cmdno }] <#{ status.termsig }>" }
      end
      if status.stopsig
        log.warn{ "stopsig[#{ cmdno }] <#{ status.stopsig }>" }
      end
    end

    exitstatus
  ensure
    LSPAWN_CMDNO[0] = LSPAWN_CMDNO[0] + 1
    Thread.critical = false
  end
#--}}}
end