clean-log.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package jobs
  2. import (
  3. "IotAdmin/core/logger"
  4. iotLog "IotAdmin/iot/log"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. // CleanLog
  10. // 新添加的job 必须按照以下格式定义,并实现Exec函数
  11. type CleanLog struct {
  12. }
  13. func (t *CleanLog) Exec(arg interface{}) error {
  14. logger.Info(time.Now().Format(timeFormat) + " [INFO] JobCore CleanLog exec START")
  15. switch arg.(type) {
  16. case string:
  17. if arg.(string) != "" {
  18. str := arg.(string)
  19. arr := strings.Split(str, ",")
  20. p1, err := strconv.Atoi(arr[0])
  21. if err != nil {
  22. logger.Error(time.Now().Format(timeFormat) + " [ERROR] JobCore CleanLog exec error:" + err.Error())
  23. return err
  24. }
  25. if len(arr) > 1 {
  26. p2, err := strconv.Atoi(arr[1])
  27. if err != nil {
  28. logger.Error(time.Now().Format(timeFormat) + " [ERROR] JobCore CleanLog exec error:" + err.Error())
  29. return err
  30. }
  31. err = iotLog.Clean(p1, p2)
  32. if err != nil {
  33. logger.Error(time.Now().Format(timeFormat) + " [ERROR] JobCore CleanLog exec error:" + err.Error())
  34. return err
  35. }
  36. } else {
  37. err = iotLog.Clean(p1, p1)
  38. if err != nil {
  39. logger.Error(time.Now().Format(timeFormat) + " [ERROR] JobCore CleanLog exec error:" + err.Error())
  40. return err
  41. }
  42. }
  43. } else {
  44. logger.Errorf(time.Now().Format(timeFormat) + " [ERROR] JobCore CleanLog exec error: arg is empty")
  45. }
  46. break
  47. case int:
  48. if arg.(int) != 0 {
  49. p := arg.(int)
  50. err := iotLog.Clean(p, p)
  51. if err != nil {
  52. logger.Error(time.Now().Format(timeFormat) + " [ERROR] JobCore CleanLog exec error:" + err.Error())
  53. return err
  54. }
  55. }
  56. break
  57. default:
  58. }
  59. return nil
  60. }