kafka_delete_task.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 kafka
  15. import (
  16. "context"
  17. "time"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  25. "yunion.io/x/onecloud/pkg/compute/models"
  26. "yunion.io/x/onecloud/pkg/util/logclient"
  27. )
  28. type KafkaDeleteTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(KafkaDeleteTask{})
  33. }
  34. func (self *KafkaDeleteTask) taskFail(ctx context.Context, kafka *models.SKafka, err error) {
  35. kafka.SetStatus(ctx, self.GetUserCred(), api.KAFKA_STATUS_DELETE_FAILED, err.Error())
  36. db.OpsLog.LogEvent(kafka, db.ACT_DELOCATE_FAIL, err, self.UserCred)
  37. logclient.AddActionLogWithStartable(self, kafka, logclient.ACT_DELETE, err, self.UserCred, false)
  38. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  39. }
  40. func (self *KafkaDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  41. kafka := obj.(*models.SKafka)
  42. iKafka, err := kafka.GetIKafka(ctx)
  43. if err != nil {
  44. if errors.Cause(err) == cloudprovider.ErrNotFound {
  45. self.taskComplete(ctx, kafka)
  46. return
  47. }
  48. self.taskFail(ctx, kafka, errors.Wrapf(err, "GetIKafka"))
  49. return
  50. }
  51. err = iKafka.Delete()
  52. if err != nil {
  53. self.taskFail(ctx, kafka, errors.Wrapf(err, "iKafka.Delete"))
  54. return
  55. }
  56. cloudprovider.WaitDeleted(iKafka, time.Second*10, time.Minute*5)
  57. self.taskComplete(ctx, kafka)
  58. }
  59. func (self *KafkaDeleteTask) taskComplete(ctx context.Context, kafka *models.SKafka) {
  60. kafka.RealDelete(ctx, self.GetUserCred())
  61. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  62. Obj: self,
  63. Action: notifyclient.ActionDelete,
  64. })
  65. self.SetStageComplete(ctx, nil)
  66. }