task.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package task
  2. import (
  3. "eta/eta_docs/global"
  4. "eta/eta_docs/services/alarm_msg"
  5. "eta/eta_docs/utils"
  6. "fmt"
  7. "os"
  8. "runtime"
  9. "sync"
  10. "time"
  11. )
  12. type TaskFunc func(params ...interface{})
  13. var taskList chan *Executor //任务列表
  14. var once sync.Once
  15. func GetTaskList() chan *Executor {
  16. once.Do(func() {
  17. taskList = make(chan *Executor, 1000)
  18. })
  19. return taskList
  20. }
  21. type Executor struct {
  22. f TaskFunc
  23. params []interface{}
  24. }
  25. func (e *Executor) Exec() { //执行任务
  26. go func() {
  27. defer func() {
  28. if err := recover(); err != any(nil) {
  29. stack := ""
  30. msg := fmt.Sprintf("当前进程pid:%d; 父进程ppid:%d", os.Getpid(), os.Getppid())
  31. stack += msg + "</br>"
  32. global.LOG.Critical(msg)
  33. msg = fmt.Sprintf("The params data is %v", e.params)
  34. stack += msg + "</br>"
  35. global.LOG.Critical(msg)
  36. msg = fmt.Sprintf("Handler crashed with error %v", err)
  37. stack += msg + "</br>"
  38. global.LOG.Critical(msg)
  39. for i := 1; ; i++ {
  40. _, file, line, ok := runtime.Caller(i)
  41. if !ok {
  42. break
  43. }
  44. global.LOG.Critical(fmt.Sprintf("%s:%d", file, line))
  45. stack = stack + fmt.Sprintln(fmt.Sprintf("%s:%d</br>", file, line))
  46. }
  47. fmt.Println("stack:", stack)
  48. //go services.SendEmail(utils.APPNAME+"崩了"+time.Now().Format("2006-01-02 15:04:05"), stack, utils.EmailSendToUsers)
  49. go alarm_msg.SendAlarmMsg(utils.APPNAME+"崩了"+time.Now().Format("2006-01-02 15:04:05")+";Msg:"+stack, 2)
  50. }
  51. }()
  52. //time.Sleep(60*time.Second)
  53. //fmt.Println("i am here new")
  54. e.f(e.params...)
  55. }()
  56. }
  57. func NewExecutor(f TaskFunc, params []interface{}) *Executor {
  58. return &Executor{f: f, params: params}
  59. }
  60. func Task(f TaskFunc, params ...interface{}) {
  61. taskList <- NewExecutor(f, params)
  62. }