mod_tasks.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. "time"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  21. "yunion.io/x/onecloud/pkg/mcclient/modules"
  22. )
  23. type TasksManager struct {
  24. modulebase.ResourceManager
  25. }
  26. func NewTaskManagers(taskManager func(string, string, []string, []string) modulebase.ResourceManager) (TasksManager, TasksManager) {
  27. task := TasksManager{
  28. ResourceManager: taskManager("task", "tasks",
  29. []string{},
  30. []string{"Id", "Obj_name", "Obj_Id", "Task_name", "Stage", "Created_at"}),
  31. }
  32. modules.Register(&task)
  33. archivedTask := TasksManager{
  34. ResourceManager: taskManager("archivedtask", "archivedtasks",
  35. []string{},
  36. []string{"Id", "task_id", "Obj_name", "Obj_Id", "Task_name", "Stage", "Start_at"}),
  37. }
  38. modules.Register(&archivedTask)
  39. return task, archivedTask
  40. }
  41. func (man *TasksManager) TaskComplete(session *mcclient.ClientSession, taskId string, params jsonutils.JSONObject) {
  42. for i := 0; i < 3; i++ {
  43. _, err := man.PerformClassAction(session, taskId, params)
  44. if err == nil {
  45. log.Infof("Sync task %s complete succ", taskId)
  46. break
  47. }
  48. log.Errorf("Sync task %s complete error: %v", taskId, err)
  49. time.Sleep(5 * time.Second)
  50. }
  51. }
  52. func (man *TasksManager) TaskFailed(session *mcclient.ClientSession, taskId string, err error) {
  53. man.TaskFailed2(session, taskId, err.Error())
  54. }
  55. func (man *TasksManager) TaskFailed2(session *mcclient.ClientSession, taskId string, reason string) {
  56. man.TaskFailed3(session, taskId, reason, nil)
  57. }
  58. func (man *TasksManager) TaskFailed3(session *mcclient.ClientSession, taskId string, reason string, params *jsonutils.JSONDict) {
  59. if params == nil {
  60. params = jsonutils.NewDict()
  61. }
  62. params.Add(jsonutils.NewString("error"), "__status__")
  63. params.Add(jsonutils.NewString(reason), "__reason__")
  64. man.TaskComplete(session, taskId, params)
  65. }
  66. /*func (self *TasksManager) getManager(session *mcclient.ClientSession, params jsonutils.JSONObject) (*modulebase.ResourceManager, error) {
  67. serviceType := apis.SERVICE_TYPE_REGION
  68. if params.Contains("service_type") {
  69. serviceType, _ = params.GetString("service_type")
  70. }
  71. version := ""
  72. switch serviceType {
  73. case apis.SERVICE_TYPE_KEYSTONE:
  74. version = "v3"
  75. case apis.SERVICE_TYPE_REGION, apis.SERVICE_TYPE_NOTIFY:
  76. version = "v2"
  77. case apis.SERVICE_TYPE_IMAGE:
  78. version = "v1"
  79. }
  80. _, err := session.GetServiceURL(serviceType, "")
  81. if err != nil {
  82. return nil, httperrors.NewNotFoundError("service %s not found error: %v", serviceType, err)
  83. }
  84. return &modulebase.ResourceManager{
  85. BaseManager: *modulebase.NewBaseManager(serviceType, "", version, []string{}, []string{}),
  86. Keyword: "task", KeywordPlural: "tasks",
  87. }, nil
  88. }
  89. func (this *TasksManager) List(session *mcclient.ClientSession, params jsonutils.JSONObject) (*printutils.ListResult, error) {
  90. man, err := this.getManager(session, params)
  91. if err != nil {
  92. return nil, err
  93. }
  94. return man.List(session, params)
  95. }*/