           @workerProcess[name].startReporter "ack"
           @workerProcess[name].stopReporter "add"
           # Update kontrold worker information with our newly created pid.
-          setTimeout =>
+          setTimeout(=>
             @workerProcess[name].fetchWorkerExchange (workerExchange) =>
               @workerProcess[name].worker.message.command = "update"
-              @workerProcess[name].worker.status = 0
-              @workerProcess[name].worker.pid = @list[name].pid
-              @workerProcess[name].worker.timestamp = @getDate()
-              workerExchange.publish "input.worker", {worker: @workerProcess[name].worker},
+              @workerProcess[name].worker.status          = 0
+              @workerProcess[name].worker.pid             = @list[name].pid
+              @workerProcess[name].worker.timestamp       = @getDate()
+              workerExchange.publish "input.worker", {
+                worker: @workerProcess[name].worker
+              }, {
                 appId: "#{@workerProcess[name].worker.uuid}"
-          , 5000
+              }
+          , 5000)
 
     #############
     ### Kill  ###

          msg['uuid'] = @workerProcess[name].worker.uuid
          @workerProcess[name].fetchWorkerExchange (workerExchange) =>
            @workerProcess[name].worker.message.command = "ack"
            workerExchange.publish "input.worker", {monitor: msg},
              appId: "#{@workerProcess[name].worker.uuid}"

    return childObj

  createPidFile : (pid,name)->
    fs.writeFileSync "#{@childProcessesContainerDir}/#{process.pid}.#{pid}",""

  get : (name)->
    if @list[name]
      return @list[name].process
    else
      console.log "[ERROR][Processes] #{name} Can't be found."

  fetchChildren: (parentPid,callback)->
    findChildren = (arr, pid) ->
      findChildrenRec = (pid,root=yes)->
        c={}
        pid = ""+pid
        i = -1
        arr.forEach (item)->
          [parent,child] = item.split(".")
          if parent is pid
            c[child] = findChildrenRec child,no
        return c

      arr = traverse(findChildrenRec pid).paths()
      arr.forEach (group) -> group.reverse()
      arr = _.sortBy arr,(a)-> a.length*-1
      return _.uniq _.flatten arr,yes

    fs.readdir @childProcessesContainerDir,(err,list)->
      if err
        callback err
      else
        callback null,findChildren list,parentPid

  killAllChildren: (parentPid, callback) ->
    @fetchChildren parentPid,(err,orderedKillList)->
      console.log "ordered kill list:"+orderedKillList
      unless err
        pidPair = []
        orderedKillList.forEach (pid)->
          console.log "killing children: #{pid}"
          try
            pidPair.push pid
            process.kill(pid*1)
            if pidPair.length is 2
              pidPair.reverse()
              deleteFile = pidPair.join(".")
              console.log "deleting"+deleteFile
              fs.unlinkSync deleteFile
              pidPair.shift()
          catch e
            console.log "child process #{pid} did not exist. not killed by me."
        callback null

  sendMessage: (name,msg)->
    if @list[name]?.process?.send
      @list[name].process.send msg

  kill: (name,callback=->)->
    unless @list[name]
      console.log "[PROCESSES][#{name}] cant kill #{name}, couldn't find it."
    else
      pid = @list[name].pid
      console.log "[PROCESSES][#{name}] killing parent: #{pid}"
      @killAllChildren pid,(err,res)->
        console.log "[PROCESSES][#{name}] killing #{name} #{pid}"
        process.kill pid

module.exports = Processes

        #else
        #  console.log "previous process tree is deleted."
        
  fork: (options, callback) ->
    options.fork = yes
    @run options, callback, (cb) =>
      callback cb

  spawn: (options, callback) ->
    options.spawn = yes
    @run options, callback

  exec: (cmd, callback) ->
    @spawn
      name    : Date.now()
      cmd     : cmd
      restart : no
      stdout  : process.stdout
      stderr  : process.stderr
      onExit  : callback

  # our main function is this. Either it is started normal, or it is wrapped
  # via kontrold amqp process
  run: (options, callback) ->
    { name, restartTimeout, verbose } = options

    options.name            ?= Date.now()
    options.restartTimeout  ?= 1000
