modelarts_pool_change_config_task.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 modelarts
  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 ModelartsPoolChangeConfigTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(ModelartsPoolChangeConfigTask{})
  33. }
  34. func (self *ModelartsPoolChangeConfigTask) taskFailed(ctx context.Context, mp *models.SModelartsPool, err error) {
  35. mp.SetStatus(ctx, self.UserCred, api.MODELARTS_POOL_STATUS_CHANGE_CONFIG_FAILED, err.Error())
  36. db.OpsLog.LogEvent(mp, db.ACT_CHANGE_CONFIG, err, self.UserCred)
  37. logclient.AddActionLogWithStartable(self, mp, logclient.ACT_CHANGE_CONFIG, err, self.UserCred, false)
  38. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  39. }
  40. func (self *ModelartsPoolChangeConfigTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  41. pool := obj.(*models.SModelartsPool)
  42. iMp, err := pool.GetIModelartsPool()
  43. if err != nil {
  44. self.taskFailed(ctx, pool, errors.Wrapf(err, "iMp.GetIModelartsPoolById"))
  45. return
  46. }
  47. input := &api.ModelartsPoolChangeConfigInput{}
  48. self.GetParams().Unmarshal(input)
  49. opts := &cloudprovider.ModelartsPoolChangeConfigOptions{}
  50. opts.NodeCount = input.NodeCount
  51. err = iMp.ChangeConfig(opts)
  52. if err != nil {
  53. self.taskFailed(ctx, pool, errors.Wrapf(err, "iMp.ChangeConfig"))
  54. return
  55. }
  56. // withDelay
  57. time.Sleep(30 * time.Second)
  58. err = cloudprovider.WaitMultiStatus(iMp, []string{api.MODELARTS_POOL_STATUS_RUNNING, api.MODELARTS_POOL_STATUS_CHANGE_CONFIG_FAILED}, 15*time.Second, 2*time.Hour)
  59. if err != nil {
  60. pool.SetStatus(ctx, self.UserCred, api.MODELARTS_POOL_STATUS_TIMEOUT, err.Error())
  61. db.OpsLog.LogEvent(pool, db.ACT_CHANGE_CONFIG, err, self.UserCred)
  62. logclient.AddActionLogWithStartable(self, pool, logclient.ACT_CHANGE_CONFIG, err, self.UserCred, false)
  63. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  64. return
  65. }
  66. pool.SyncWithCloudModelartsPool(ctx, self.GetUserCred(), iMp)
  67. self.taskComplete(ctx, pool)
  68. }
  69. func (self *ModelartsPoolChangeConfigTask) taskComplete(ctx context.Context, pool *models.SModelartsPool) {
  70. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  71. Obj: self,
  72. Action: notifyclient.ActionChangeConfig,
  73. })
  74. self.SetStageComplete(ctx, nil)
  75. }