upcall.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/onecloud/pkg/appsrv"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. )
  24. var _saveTaskUpcallWorkMan *appsrv.SWorkerManager
  25. func init() {
  26. _saveTaskUpcallWorkMan = appsrv.NewWorkerManager("SaveTaskUpcallResultWorkerManager", 1, 1024, true)
  27. }
  28. type saveUpcallStatusTask struct {
  29. taskId string
  30. target string
  31. success bool
  32. errMsg string
  33. tm time.Time
  34. }
  35. func (task *saveUpcallStatusTask) Run() {
  36. err := task.runInternal()
  37. if err != nil {
  38. log.Errorf("Task %s save upcall status fail %s", task.Dump(), err)
  39. }
  40. }
  41. func (task *saveUpcallStatusTask) Dump() string {
  42. return fmt.Sprintf("taskId: %s, success: %t, errMsg: %s", task.taskId, task.success, task.errMsg)
  43. }
  44. func saveTaskUpCallStatus(taskId string, target string, success bool, errMsg string) {
  45. _saveTaskUpcallWorkMan.Run(&saveUpcallStatusTask{
  46. taskId: taskId,
  47. target: target,
  48. success: success,
  49. errMsg: errMsg,
  50. tm: time.Now(),
  51. }, nil, nil)
  52. }
  53. func (t *saveUpcallStatusTask) runInternal() error {
  54. task := TaskManager.FetchTaskById(t.taskId)
  55. _, err := db.Update(task, func() error {
  56. params := jsonutils.NewDict()
  57. params.Update(task.Params)
  58. upcalls, _ := params.Get("__upcalls")
  59. if upcalls == nil {
  60. upcalls = jsonutils.NewArray()
  61. params.Add(upcalls, "__upcalls")
  62. }
  63. upcallsList := upcalls.(*jsonutils.JSONArray)
  64. upcallData := jsonutils.NewDict()
  65. upcallData.Add(jsonutils.NewString(t.target), "target")
  66. upcallData.Add(jsonutils.NewBool(t.success), "result")
  67. upcallData.Add(jsonutils.NewString(t.errMsg), "msg")
  68. upcallData.Add(jsonutils.NewTimeString(t.tm), "complete_at")
  69. upcallsList.Add(upcallData)
  70. task.Params = params
  71. return nil
  72. })
  73. if err != nil {
  74. return errors.Wrapf(err, "Update %s", t.taskId)
  75. }
  76. return nil
  77. }