mysql_task.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package dao
  2. import (
  3. "go-common/app/admin/ep/merlin/model"
  4. pkgerr "github.com/pkg/errors"
  5. )
  6. // FindDeleteMachineTasks find delete machine tasks.
  7. func (d *Dao) FindDeleteMachineTasks() (tasks []*model.Task, err error) {
  8. tasks = []*model.Task{}
  9. err = pkgerr.WithStack(d.db.Where("status=? AND type=? AND DATEDIFF(NOW(),execute_time)=0 AND execute_time < NOW()", model.TaskInit, model.DeleteMachine).Find(&tasks).Error)
  10. return
  11. }
  12. // UpdateTaskStatusByMachines update task status by machines.
  13. func (d *Dao) UpdateTaskStatusByMachines(machineIDs []int64, status int) (err error) {
  14. return pkgerr.WithStack(d.db.Model(&model.Task{}).Where("machine_id IN (?)", machineIDs).Update("status", status).Error)
  15. }
  16. // UpdateTaskStatusByTaskID update task status by taskId.
  17. func (d *Dao) UpdateTaskStatusByTaskID(taskID int64, status int) (err error) {
  18. return pkgerr.WithStack(d.db.Model(&model.Task{}).Where("id IN (?)", taskID).Update("status", status).Error)
  19. }
  20. // InsertDeleteMachinesTasks insert delete machines tasks.
  21. // TODO:这块逻辑不是很好,如果一个写入失败希望继续写入失败的话最好返回失败list,后续联调时优化
  22. func (d *Dao) InsertDeleteMachinesTasks(ms []*model.Machine) (err error) {
  23. for _, machine := range ms {
  24. mrTask := &model.Task{
  25. TYPE: model.DeleteMachine,
  26. ExecuteTime: machine.EndTime,
  27. MachineID: machine.ID,
  28. Status: model.TaskInit,
  29. }
  30. if tempErr := d.db.Create(mrTask).Error; tempErr != nil {
  31. err = pkgerr.WithStack(tempErr)
  32. }
  33. }
  34. return
  35. }