repull_subcontact_task.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. "strings"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/util/sets"
  21. "yunion.io/x/pkg/utils"
  22. "yunion.io/x/sqlchemy"
  23. "yunion.io/x/onecloud/pkg/apis/notify"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
  26. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  27. "yunion.io/x/onecloud/pkg/notify/models"
  28. "yunion.io/x/onecloud/pkg/util/logclient"
  29. )
  30. type RepullSuncontactTask struct {
  31. taskman.STask
  32. }
  33. func init() {
  34. taskman.RegisterTask(RepullSuncontactTask{})
  35. }
  36. func (self *RepullSuncontactTask) taskFailed(ctx context.Context, config *models.SConfig, reason string) {
  37. if !config.Deleted {
  38. logclient.AddActionLogWithContext(ctx, config, logclient.ACT_PULL_SUBCONTACT, reason, self.UserCred, false)
  39. }
  40. self.SetStageFailed(ctx, jsonutils.NewString(reason))
  41. }
  42. type repullFailedReason struct {
  43. ReceiverId string
  44. Reason string
  45. }
  46. func (s repullFailedReason) String() string {
  47. return fmt.Sprintf("receiver %q: %s", s.ReceiverId, s.Reason)
  48. }
  49. func (self *RepullSuncontactTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  50. config := obj.(*models.SConfig)
  51. if !utils.IsInStringArray(config.Type, PullContactType) {
  52. if del, _ := self.GetParams().Bool("deleted"); del {
  53. config.RealDelete(ctx, self.GetUserCred())
  54. }
  55. self.SetStageComplete(ctx, nil)
  56. return
  57. }
  58. subq := models.SubContactManager.Query("receiver_id").Equals("type", config.Type).SubQuery()
  59. q := models.ReceiverManager.Query()
  60. if config.Attribution == notify.CONFIG_ATTRIBUTION_DOMAIN {
  61. q = q.Equals("domain_id", config.DomainId)
  62. } else {
  63. // The system-level config update should not affect the receiver under the domain with config
  64. configq := models.ConfigManager.Query("domain_id").Equals("type", config.Type).Equals("attribution", notify.CONFIG_ATTRIBUTION_DOMAIN).SubQuery()
  65. q = q.NotIn("domain_id", configq)
  66. }
  67. q.Join(subq, sqlchemy.Equals(q.Field("id"), subq.Field("receiver_id")))
  68. rs := make([]models.SReceiver, 0)
  69. err := db.FetchModelObjects(models.ReceiverManager, q, &rs)
  70. if err != nil {
  71. self.taskFailed(ctx, config, fmt.Sprintf("unable to FetchModelObjects: %v", err))
  72. return
  73. }
  74. if del, _ := self.GetParams().Bool("deleted"); del {
  75. config.RealDelete(ctx, self.GetUserCred())
  76. }
  77. var reasons []string
  78. for i := range rs {
  79. r := &rs[i]
  80. func() {
  81. lockman.LockObject(ctx, r)
  82. defer lockman.ReleaseObject(ctx, r)
  83. // unverify
  84. cts, err := r.GetVerifiedContactTypes()
  85. if err != nil {
  86. reasons = append(reasons, repullFailedReason{
  87. ReceiverId: r.Id,
  88. Reason: fmt.Sprintf("unable to GetVerifiedContactTypes: %v", err),
  89. }.String())
  90. return
  91. }
  92. ctSets := sets.NewString(cts...)
  93. if ctSets.Has(config.Type) {
  94. ctSets.Delete(config.Type)
  95. // err = r.SetVerifiedContactTypes(ctSets.UnsortedList())
  96. // if err != nil {
  97. // reasons = append(reasons, repullFailedReason{
  98. // ReceiverId: r.Id,
  99. // Reason: fmt.Sprintf("unable to SetVerifiedContactTypes: %v", err),
  100. // }.String())
  101. // return
  102. // }
  103. }
  104. // pull
  105. params := jsonutils.NewDict()
  106. params.Set("contact_types", jsonutils.NewArray(jsonutils.NewString(config.Type)))
  107. // err = r.StartSubcontactPullTask(ctx, self.UserCred, params, self.Id)
  108. // if err != nil {
  109. // reasons = append(reasons, repullFailedReason{
  110. // ReceiverId: r.Id,
  111. // Reason: fmt.Sprintf("unable to StartSubcontactPullTask: %v", err),
  112. // }.String())
  113. // }
  114. }()
  115. }
  116. if len(reasons) > 0 {
  117. self.taskFailed(ctx, config, strings.Join(reasons, "; "))
  118. return
  119. }
  120. self.SetStageComplete(ctx, nil)
  121. }