var spawn = require('child_process').spawn
var Queue = require('firebase-queue'),
    Firebase = require('firebase')

if (process.argv.length < 4) {
  console.log("usage: node worker.js firebaseUrl command [arg1, arg2, ...]")
  console.log("  For authentication, set FIREBASE_TOKEN environment variable.")
  process.exit()
}
var queueUrl = process.argv[2]
var cmdArgs = process.argv.slice(3)

var queueRef = new Firebase(queueUrl)
var start = function () {
  // presence of worker
  queueRef.root().child('.info/connected').on('value', function(snapshot) {
    if (snapshot.val()) {
      var workerRef = queueRef.child("workers").push({hostname: require('os').hostname()})
      workerRef.onDisconnect().remove();
    }
  })
  console.log("WORKER: waiting a runner pushed a task...")
  var queue = new Queue(queueRef, function (data, progress, resolve, reject) {
    // Read and process task data
    var bufferRef = new Firebase(data.bufferUrl)
    var log = function (msg) {
      bufferRef.child('log').push(msg.toString())
      process.stdout.write(msg.toString())
    }
    log("WORKER: start command: " + cmdArgs + "\n\n")
    var env = Object.create(process.env)
    for (var name in data.env) {
      env[name] = data.env[name]
    }
    var cmd = spawn(cmdArgs[0], cmdArgs.slice(1), { env: env })
    cmd.stdout.on('data', log)
    cmd.stderr.on('data', log)
    cmd.on('error', function (err) { reject(err) })
    cmd.on('close', function (code) {
      if (code === 0) {
        log("\nWORKER: command completed.\n\n")
        resolve()
      } else {
        log("\nWORKER: command failed.\n\n")
        reject("exit code: " + code)
      }
      console.log("WORKER: waiting a runner pushed another task...")
    })
  })
  // graceful shutdown
  process.on('SIGINT', function () {
    console.log('WORKER: Starting queue shutdown...');
    queue.shutdown().then(function () {
      console.log('WORKER: Finished queue shutdown.');
      process.exit(1)
    });
  })
}

if (process.env.FIREBASE_TOKEN) {
  queueRef.authWithCustomToken(process.env.FIREBASE_TOKEN, function (error, result) {
    if (error) {
      console.log("WORKER: Authentication Failed:", error)
    } else {
      console.log("WORKER: Authentication succeeded!")
      start()
    }
  })
  delete process.env.FIREBASE_TOKEN
} else {
  start()
}

@@ -1248,16 +1248,153 @@
  var cmd
+%0A    if (os.platform() === 'win32') %7B%0A      cmd = spawn(process.env.comspec, %5B'/c'%5D.concat(cmdArgs), %7B env: env %7D)%0A    %7D else %7B%0A      cmd
  = spawn
@@ -1434,24 +1434,30 @@
 env: env %7D)%0A
+    %7D%0A
     cmd.stdo

Use 'cmd.exe /c' to spawn command for windows.