       )
       when "STOP_REDUCER_OUTPUT" then (
         if (not((currentJobs[message.job] == null or currentJobs[message.job] == -1)))
-          currentJobs[message.job].finishReducer(message.reducer)
-          currentJobs[message.job] = -1
+          if (currentJobs[message.job].finishReducer(message.reducer))
+            currentJobs[message.job] = -1
       )
     fb.child(snapshot.name()).remove()
   )


  fb.on("child_added", newMessage = (snapshot) ->
    message = snapshot.val()
    console.log message
    switch message.name
      when "START_JOB" then (
        if (not (currentJobs[message.job] == -1))

  finishReducer: (reducerid) ->
    @reducers[reducerid].finished = true
    @numReducers++
    console.log(@numReducers + " out of " + @totalReducers)
    if (@numReducers == @totalReducers)
      jobMap = @combineToJobMap()
      str = @serializeJobMap(jobMap)
      @saveToFile(str)
      firebase_db.JOB_STATUS_REF.child(@job).child('output_url')
      .set(constants.OUTPUT_DIR + @job)
      return true
    return false

  combineToJobMap: () ->
    jobMap = {}
