worker.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "context"
  17. "fmt"
  18. "runtime/debug"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. api "yunion.io/x/onecloud/pkg/apis/notify"
  22. "yunion.io/x/onecloud/pkg/appsrv"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  25. )
  26. var _taskWorkMan *appsrv.SWorkerManager
  27. func getTaskWorkMan(task *STask) *appsrv.SWorkerManager {
  28. taskWorkManLock.Lock()
  29. defer taskWorkManLock.Unlock()
  30. if worker, ok := taskWorkerMap[task.TaskName]; ok {
  31. switch workerMan := worker.(type) {
  32. case *appsrv.SWorkerManager:
  33. return workerMan
  34. case *appsrv.SHashedWorkerManager:
  35. key := task.ObjId
  36. if key == MULTI_OBJECTS_ID {
  37. key = task.TaskName
  38. }
  39. return workerMan.GetWorkerManager(key)
  40. }
  41. }
  42. if _taskWorkMan != nil {
  43. return _taskWorkMan
  44. }
  45. log.Infof("TaskWorkerManager %d", consts.TaskWorkerCount())
  46. _taskWorkMan = appsrv.NewWorkerManager("TaskWorkerManager", consts.TaskWorkerCount(), 1024, true)
  47. return _taskWorkMan
  48. }
  49. /*func UpdateWorkerCount(workerCount int) error {
  50. if workerCount != DEFAULT_WORKER_COUNT {
  51. log.Infof("update task work count: %d", workerCount)
  52. return taskWorkMan.UpdateWorkerCount(workerCount)
  53. }
  54. return nil
  55. }*/
  56. type taskTask struct {
  57. taskId string
  58. data jsonutils.JSONObject
  59. }
  60. func (t *taskTask) Run() {
  61. TaskManager.execTask(t.taskId, t.data)
  62. }
  63. func (t *taskTask) Dump() string {
  64. return jsonutils.Marshal(t).PrettyString()
  65. }
  66. func runTask(taskId string, data jsonutils.JSONObject) error {
  67. baseTask := TaskManager.fetchTask(taskId)
  68. if baseTask == nil {
  69. return fmt.Errorf("no such task??? task_id=%s", taskId)
  70. }
  71. worker := getTaskWorkMan(baseTask)
  72. task := &taskTask{
  73. taskId: taskId,
  74. data: data,
  75. }
  76. isOk := worker.Run(task, nil, func(err error) {
  77. data := jsonutils.NewDict()
  78. data.Add(jsonutils.NewString(baseTask.TaskName), "task_name")
  79. data.Add(jsonutils.NewString(taskId), "task_id")
  80. data.Add(jsonutils.NewString(string(debug.Stack())), "stack")
  81. data.Add(jsonutils.NewString(err.Error()), "error")
  82. notifyclient.SystemExceptionNotify(context.TODO(), api.ActionSystemPanic, api.TOPIC_RESOURCE_TASK, data)
  83. })
  84. if !isOk {
  85. return fmt.Errorf("worker %s(%s) not running may be dropped", baseTask.TaskName, taskId)
  86. }
  87. return nil
  88. }