elastic_search_delete_task.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 elastic_search
  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 ElasticSearchDeleteTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(ElasticSearchDeleteTask{})
  33. }
  34. func (self *ElasticSearchDeleteTask) taskFail(ctx context.Context, es *models.SElasticSearch, err error) {
  35. es.SetStatus(ctx, self.GetUserCred(), api.ELASTIC_SEARCH_STATUS_DELETE_FAILED, err.Error())
  36. db.OpsLog.LogEvent(es, db.ACT_DELOCATE_FAIL, err, self.UserCred)
  37. // 记录删除失败日志
  38. logclient.AddActionLogWithStartable(self, es, logclient.ACT_DELETE, err, self.UserCred, false)
  39. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  40. }
  41. func (self *ElasticSearchDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  42. es := obj.(*models.SElasticSearch)
  43. iEs, err := es.GetIElasticSearch(ctx)
  44. if err != nil {
  45. if errors.Cause(err) == cloudprovider.ErrNotFound {
  46. self.taskComplete(ctx, es)
  47. return
  48. }
  49. self.taskFail(ctx, es, errors.Wrapf(err, "GetIElasticSearch"))
  50. return
  51. }
  52. err = iEs.Delete()
  53. if err != nil {
  54. self.taskFail(ctx, es, errors.Wrapf(err, "iEs.Delete"))
  55. return
  56. }
  57. cloudprovider.WaitDeleted(iEs, time.Second*10, time.Minute*5)
  58. self.taskComplete(ctx, es)
  59. }
  60. func (self *ElasticSearchDeleteTask) taskComplete(ctx context.Context, es *models.SElasticSearch) {
  61. es.RealDelete(ctx, self.GetUserCred())
  62. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  63. Obj: es,
  64. Action: notifyclient.ActionDelete,
  65. })
  66. self.SetStageComplete(ctx, nil)
  67. }