func (db *SQLDB) DeleteTask(logger lager.Logger, taskGuid string) error { logger = logger.Session("delete-task", lager.Data{"task_guid": taskGuid}) logger.Info("starting") defer logger.Info("complete") return db.transact(logger, func(logger lager.Logger, tx *sql.Tx) error { task, err := db.fetchTaskForUpdate(logger, taskGuid, tx) if err != nil { logger.Error("failed-locking-task", err) return err } if task.State != models.Task_Resolving { err = models.NewTaskTransitionError(task.State, models.Task_Resolving) logger.Error("invalid-state-transition", err) return err } _, err = db.delete(logger, tx, tasksTable, "guid = ?", taskGuid) if err != nil { logger.Error("failed-deleting-task", err) return db.convertSQLError(err) } return nil }) }
// The stager calls this when it wants to signal that it has received a completion and is handling it // stagerTaskBBS will retry this repeatedly if it gets a StoreTimeout error (up to N seconds?) // If this fails, the stager should assume that someone else is handling the completion and should bail func (db *ETCDDB) DeleteTask(logger lager.Logger, taskGuid string) error { logger = logger.WithData(lager.Data{"task_guid": taskGuid}) logger.Info("starting") defer logger.Info("finished") task, _, err := db.taskByGuidWithIndex(logger, taskGuid) if err != nil { logger.Error("failed-getting-task", err) return err } if task.State != models.Task_Resolving { err = models.NewTaskTransitionError(task.State, models.Task_Resolving) logger.Error("invalid-state-transition", err) return err } _, err = db.client.Delete(TaskSchemaPathByGuid(taskGuid), false) return ErrorFromEtcdError(logger, err) }
It("only removes the task with the corresponding guid", func() { err := sqlDB.DeleteTask(logger, taskGuid) Expect(err).NotTo(HaveOccurred()) task, err := sqlDB.TaskByGuid(logger, anotherTask.TaskGuid) Expect(err).NotTo(HaveOccurred()) Expect(task).To(BeEquivalentTo(anotherTask)) }) }) }) Context("and the task is not resolving", func() { It("returns an error", func() { err := sqlDB.DeleteTask(logger, taskGuid) expectedErr := models.NewTaskTransitionError(models.Task_Completed, models.Task_Resolving) Expect(err).To(Equal(expectedErr)) }) }) }) Context("when the task does not exist", func() { It("returns a ResourceNotFound error", func() { err := sqlDB.DeleteTask(logger, taskGuid) Expect(err).To(Equal(models.ErrResourceNotFound)) }) }) }) }) func insertTask(db *sql.DB, serializer format.Serializer, task *models.Task, malformedTaskDefinition bool) {
BeforeEach(func() { taskDef = model_helpers.NewValidTaskDefinition() err := etcdDB.DesireTask(logger, taskDef, taskGuid, domain) Expect(err).NotTo(HaveOccurred()) _, err = etcdDB.StartTask(logger, taskGuid, cellId) Expect(err).NotTo(HaveOccurred()) task, err := etcdDB.CompleteTask(logger, taskGuid, cellId, false, "", "") Expect(err).NotTo(HaveOccurred()) Expect(task.TaskGuid).To(Equal(taskGuid)) }) It("returns an error", func() { Expect(cancelError).To(HaveOccurred()) Expect(cancelError).To(Equal(models.NewTaskTransitionError(models.Task_Completed, models.Task_Completed))) }) }) Context("when the task is in resolving state", func() { BeforeEach(func() { taskDef = model_helpers.NewValidTaskDefinition() err := etcdDB.DesireTask(logger, taskDef, taskGuid, domain) Expect(err).NotTo(HaveOccurred()) _, err = etcdDB.StartTask(logger, taskGuid, cellId) Expect(err).NotTo(HaveOccurred()) task, err := etcdDB.CompleteTask(logger, taskGuid, cellId, false, "", "") Expect(err).NotTo(HaveOccurred()) Expect(task.TaskGuid).To(Equal(taskGuid))