worker.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 tasks
  15. import (
  16. "context"
  17. "fmt"
  18. "runtime/debug"
  19. "time"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/log"
  22. "yunion.io/x/pkg/errors"
  23. "yunion.io/x/pkg/util/version"
  24. "yunion.io/x/onecloud/pkg/appsrv"
  25. "yunion.io/x/onecloud/pkg/baremetal/options"
  26. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  27. "yunion.io/x/onecloud/pkg/mcclient/modules/yunionconf"
  28. )
  29. var baremetalTaskWorkerMan *appsrv.SWorkerManager
  30. func GetWorkManager() *appsrv.SWorkerManager {
  31. if baremetalTaskWorkerMan == nil {
  32. baremetalTaskWorkerMan = appsrv.NewWorkerManager("BaremetalTaskWorkerManager", options.Options.BaremetalTaskWorkerCount, 1024, false)
  33. }
  34. return baremetalTaskWorkerMan
  35. }
  36. func OnStop() {
  37. for GetWorkManager().ActiveWorkerCount() > 0 {
  38. log.Warningf("Busy workers count %d, waiting them finish", GetWorkManager().ActiveWorkerCount())
  39. time.Sleep(5 * time.Second)
  40. }
  41. }
  42. type baremtalTask struct {
  43. task ITask
  44. args interface{}
  45. }
  46. func (t *baremtalTask) Run() {
  47. executeTask(t.task, t.args)
  48. }
  49. func (t *baremtalTask) Dump() string {
  50. return fmt.Sprintf("Task %s(%s) params: %v", t.task.GetName(), t.task.GetTaskId(), t.args)
  51. }
  52. func ExecuteTask(task ITask, args interface{}) {
  53. t := &baremtalTask{
  54. task: task,
  55. args: args,
  56. }
  57. GetWorkManager().Run(t, nil, nil)
  58. }
  59. func executeTask(task ITask, args interface{}) {
  60. if task == nil {
  61. return
  62. }
  63. curStage := task.GetStage()
  64. if curStage == nil {
  65. return
  66. }
  67. defer func() {
  68. if err := recover(); err != nil {
  69. log.Errorf("Execute task panic: %v", err)
  70. debug.PrintStack()
  71. SetTaskFail(task, fmt.Errorf("%v", err))
  72. yunionconf.BugReport.SendBugReport(context.Background(), version.GetShortString(), string(debug.Stack()), errors.Errorf("%s", err))
  73. }
  74. }()
  75. err := curStage(context.Background(), args)
  76. if err != nil {
  77. log.Errorf("Execute task %s error: %v", task.GetName(), err)
  78. SetTaskFail(task, err)
  79. }
  80. }
  81. func SetTaskComplete(task ITask, data jsonutils.JSONObject) {
  82. taskId := task.GetTaskId()
  83. if taskId != "" {
  84. session := task.GetClientSession()
  85. modules.ComputeTasks.TaskComplete(session, taskId, data)
  86. }
  87. onTaskEnd(task)
  88. }
  89. func SetTaskFail(task ITask, err error) {
  90. taskId := task.GetTaskId()
  91. if taskId != "" {
  92. session := task.GetClientSession()
  93. modules.ComputeTasks.TaskFailed(session, taskId, err)
  94. }
  95. onTaskEnd(task)
  96. }
  97. func onTaskEnd(task ITask) {
  98. task.SetStage(nil)
  99. ExecuteTask(task.GetTaskQueue().PopTask(), nil)
  100. }
  101. func OnInitStage(task ITask) error {
  102. log.Infof("Start task %s", task.GetName())
  103. return nil
  104. }