You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.5 KiB

4 years ago
  1. package models
  2. import (
  3. "leit.com/LAPP_GAAS_GFrame/db"
  4. "time"
  5. "github.com/go-xorm/xorm"
  6. )
  7. type TaskType int8
  8. // 任务执行日志
  9. type TaskLog struct {
  10. Id int64 `json:"id" xorm:"bigint pk autoincr"`
  11. TaskId int `json:"task_id" xorm:"int notnull index default 0"` // 任务id
  12. Name string `json:"name" xorm:"varchar(32) notnull"` // 任务名称
  13. Spec string `json:"spec" xorm:"varchar(64) notnull"` // crontab
  14. Protocol TaskProtocol `json:"protocol" xorm:"tinyint notnull index"` // 协议 1:http 2:RPC
  15. Command string `json:"command" xorm:"varchar(256) notnull"` // URL地址或shell命令
  16. Timeout int `json:"timeout" xorm:"mediumint notnull default 0"` // 任务执行超时时间(单位秒),0不限制
  17. RetryTimes int8 `json:"retry_times" xorm:"tinyint notnull default 0"` // 任务重试次数
  18. Hostname string `json:"hostname" xorm:"varchar(128) notnull default '' "` // RPC主机名,逗号分隔
  19. StartTime time.Time `json:"start_time" xorm:"datetime created"` // 开始执行时间
  20. EndTime time.Time `json:"end_time" xorm:"datetime updated"` // 执行完成(失败)时间
  21. Status Status `json:"status" xorm:"tinyint notnull index default 1"` // 状态 0:执行失败 1:执行中 2:执行完毕 3:任务取消(上次任务未执行完成) 4:异步执行
  22. Result string `json:"result" xorm:"mediumtext notnull "` // 执行结果
  23. TotalTime int `json:"total_time" xorm:"-"` // 执行总时长
  24. }
  25. func (taskLog *TaskLog) Create() (insertId int64, err error) {
  26. e := db.Eloquent.Master()
  27. _, err = e.Insert(taskLog)
  28. if err == nil {
  29. insertId = taskLog.Id
  30. }
  31. return
  32. }
  33. // 更新
  34. func (taskLog *TaskLog) Update(id int64, data CommonMap) (int64, error) {
  35. e := db.Eloquent.Master()
  36. return e.Table(taskLog).ID(id).Update(data)
  37. }
  38. func (taskLog *TaskLog) List(params CommonMap,pageSize int, pageIndex int) ([]TaskLog, error) {
  39. e := db.Eloquent.Master()
  40. Offset := (pageIndex - 1) * pageSize
  41. list := make([]TaskLog, 0)
  42. session := e.Desc("id")
  43. taskLog.parseWhere(session, params)
  44. err := session.Limit(pageSize, Offset).Find(&list)
  45. if len(list) > 0 {
  46. for i, item := range list {
  47. endTime := item.EndTime
  48. if item.Status == Running {
  49. endTime = time.Now()
  50. }
  51. execSeconds := endTime.Sub(item.StartTime).Seconds()
  52. list[i].TotalTime = int(execSeconds)
  53. }
  54. }
  55. return list, err
  56. }
  57. // 清空表
  58. func (taskLog *TaskLog) Clear() (int64, error) {
  59. e := db.Eloquent.Master()
  60. return e.Where("1=1").Delete(taskLog)
  61. }
  62. // 删除N个月前的日志
  63. func (taskLog *TaskLog) Remove(id int) (int64, error) {
  64. e := db.Eloquent.Master()
  65. t := time.Now().AddDate(0, -id, 0)
  66. return e.Where("start_time <= ?", t.Format(DefaultTimeFormat)).Delete(taskLog)
  67. }
  68. func (taskLog *TaskLog) Total(params CommonMap) (int64, error) {
  69. e := db.Eloquent.Master()
  70. session := e.NewSession()
  71. defer session.Close()
  72. taskLog.parseWhere(session, params)
  73. return session.Count(taskLog)
  74. }
  75. // 解析where
  76. func (taskLog *TaskLog) parseWhere(session *xorm.Session, params CommonMap) {
  77. if len(params) == 0 {
  78. return
  79. }
  80. taskId, ok := params["TaskId"]
  81. if ok && taskId.(int) > 0 {
  82. session.And("task_id = ?", taskId)
  83. }
  84. protocol, ok := params["Protocol"]
  85. if ok && protocol.(int) > 0 {
  86. session.And("protocol = ?", protocol)
  87. }
  88. status, ok := params["Status"]
  89. if ok && status.(int) > -1 {
  90. session.And("status = ?", status)
  91. }
  92. }