func (m *JobManager) killJob(id string) { m.jobLock.Lock() job, ex := m.jobMap[id] if ex { isFail := false for tid, task := range job.TaskMap { if task.State != comp.TASK_COMPLETE && task.State != comp.TASK_FAIL { task.State = comp.TASK_FAIL task.FinishTime = time.Now() job.TaskMap[tid] = task isFail = true } } job.FinishTime = time.Now() var update map[string]interface{} //update job in db if isFail { update = bson.M{"$set": bson.M{models.JOB_STATUS: -1}} } else { update = bson.M{"$set": bson.M{models.JOB_STATUS: 100}} } models.UpdateJobSketchInDB(id, update) models.UpdateJobInDB(id, job) delete(m.jobMap, id) } m.jobLock.Unlock() }
//Start find finished job cyclically func (m *JobManager) updateJobInDB() { log.Println("Start update job state.") for { //update job status var finishedJobs []string = make([]string, 0) m.jobLock.Lock() for jid, job := range m.jobMap { all := 0 pro := 0 for _, ts := range job.TaskMap { all = all + 2 if ts.State == comp.TASK_COMPLETE || ts.State == comp.TASK_FAIL { pro = pro + 2 } else if ts.State == comp.TASK_RUN { pro = pro + 1 } } rate := 100 if all != 0 { rate = pro * 100 / all } //log.Println(all, " : ", pro) if all == pro { finishedJobs = append(finishedJobs, jid) job.FinishTime = time.Now() } update := bson.M{"$set": bson.M{models.JOB_STATUS: rate}} models.UpdateJobSketchInDB(jid, update) models.UpdateJobInDB(jid, job) } m.jobLock.Unlock() //delete finished job for _, id := range finishedJobs { log.Println("Delete finished job: ", id) m.deleteJob(id) } time.Sleep(comm.HEARTTIME) } }