localtaskworker.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package taskman
  15. import (
  16. "fmt"
  17. "runtime/debug"
  18. "sync"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/util/version"
  23. "yunion.io/x/onecloud/pkg/appsrv"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  25. "yunion.io/x/onecloud/pkg/mcclient/modules/yunionconf"
  26. "yunion.io/x/onecloud/pkg/util/ctx"
  27. )
  28. var localTaskWorkerMan *appsrv.SWorkerManager
  29. var localTaskWorkerManLock *sync.Mutex
  30. func init() {
  31. localTaskWorkerManLock = &sync.Mutex{}
  32. }
  33. func Error2TaskData(err error) jsonutils.JSONObject {
  34. errJson := jsonutils.NewDict()
  35. errJson.Add(jsonutils.NewString("ERROR"), "__status__")
  36. errJson.Add(jsonutils.NewString(err.Error()), "__reason__")
  37. return errJson
  38. }
  39. type localTask struct {
  40. task ITask
  41. proc func() (jsonutils.JSONObject, error)
  42. }
  43. func (t *localTask) Run() {
  44. defer func() {
  45. if r := recover(); r != nil {
  46. yunionconf.BugReport.SendBugReport(ctx.CtxWithTime(), version.GetShortString(), string(debug.Stack()), errors.Errorf("%s", r))
  47. log.Errorf("LocalTaskRun error: %s", r)
  48. debug.PrintStack()
  49. t.task.ScheduleRun(Error2TaskData(fmt.Errorf("LocalTaskRun error: %s stack: %s", r, string(debug.Stack()))))
  50. }
  51. }()
  52. data, err := t.proc()
  53. if err != nil {
  54. t.task.ScheduleRun(Error2TaskData(err))
  55. } else {
  56. t.task.ScheduleRun(data)
  57. }
  58. }
  59. func (t *localTask) Dump() string {
  60. return fmt.Sprintf("StartTime: %s TaskId: %s Params: %s", t.task.GetStartTime(), t.task.GetTaskId(), t.task.GetParams())
  61. }
  62. func LocalTaskRunWithWorkers(task ITask, proc func() (jsonutils.JSONObject, error), wm *appsrv.SWorkerManager) {
  63. t := localTask{
  64. task: task,
  65. proc: proc,
  66. }
  67. wm.Run(&t, nil, nil)
  68. }
  69. func getLocalTaskWorkerMan() *appsrv.SWorkerManager {
  70. localTaskWorkerManLock.Lock()
  71. defer localTaskWorkerManLock.Unlock()
  72. if localTaskWorkerMan != nil {
  73. return localTaskWorkerMan
  74. }
  75. log.Infof("LocalTaskWorkerManager %d", consts.LocalTaskWorkerCount())
  76. localTaskWorkerMan = appsrv.NewWorkerManager("LocalTaskWorkerManager", consts.LocalTaskWorkerCount(), 1024, false)
  77. return localTaskWorkerMan
  78. }
  79. func LocalTaskRun(task ITask, proc func() (jsonutils.JSONObject, error)) {
  80. LocalTaskRunWithWorkers(task, proc, getLocalTaskWorkerMan())
  81. }