coordinator.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "reflect"
  18. "sync"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/gotypes"
  22. "yunion.io/x/onecloud/pkg/appsrv"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  24. )
  25. var taskWorkerMap map[string]interface{}
  26. var taskWorkManLock *sync.Mutex
  27. func init() {
  28. taskWorkerMap = make(map[string]interface{})
  29. taskWorkManLock = &sync.Mutex{}
  30. }
  31. /*type TaskStageFunc func(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject)
  32. type BatchTaskStageFunc func(ctx context.Context, objs []db.IStandaloneModel, body jsonutils.JSONObject)
  33. */
  34. type IBatchTask interface {
  35. OnInit(ctx context.Context, objs []db.IStandaloneModel, body jsonutils.JSONObject)
  36. ScheduleRun(data jsonutils.JSONObject) error
  37. }
  38. type ISingleTask interface {
  39. OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject)
  40. ScheduleRun(data jsonutils.JSONObject) error
  41. }
  42. var ITaskType reflect.Type
  43. var IBatchTaskType reflect.Type
  44. var taskTable map[string]reflect.Type
  45. func init() {
  46. ITaskType = reflect.TypeOf((*ISingleTask)(nil)).Elem()
  47. IBatchTaskType = reflect.TypeOf((*IBatchTask)(nil)).Elem()
  48. taskTable = make(map[string]reflect.Type)
  49. }
  50. func RegisterTaskAndWorker(task interface{}, workerMan *appsrv.SWorkerManager) {
  51. registerTaskAndWorkerMan(task, workerMan)
  52. }
  53. func RegisterTaskAndHashedWorkerManager(task interface{}, workerMan *appsrv.SHashedWorkerManager) {
  54. registerTaskAndWorkerMan(task, workerMan)
  55. }
  56. func registerTaskAndWorkerMan(task interface{}, workerMan interface{}) {
  57. taskName := gotypes.GetInstanceTypeName(task)
  58. if _, ok := taskTable[taskName]; ok {
  59. log.Fatalf("Task %s already registered!", taskName)
  60. }
  61. taskType := reflect.Indirect(reflect.ValueOf(task)).Type()
  62. taskTable[taskName] = taskType
  63. // log.Infof("Task %s registerd", taskName)
  64. if workerMan != nil && !gotypes.IsNil(workerMan) {
  65. taskWorkerMap[taskName] = workerMan
  66. }
  67. }
  68. func RegisterTask(task interface{}) {
  69. registerTaskAndWorkerMan(task, nil)
  70. }
  71. func isTaskExist(taskName string) bool {
  72. _, ok := taskTable[taskName]
  73. return ok
  74. }