mirror of https://github.com/harness/drone.git
feat: [CDE-466]: added a transient state during deletion (#2949)
* feat: [CDE-466]: added a transient state during deletion * feat: [CDE-466]: added a transient state during deletionpull/3586/head
parent
cb200c85c3
commit
0469b4352e
|
@ -25,9 +25,6 @@ import (
|
|||
"github.com/harness/gitness/store/database/dbtx"
|
||||
)
|
||||
|
||||
// gitspaceInstanceCleaningTimedOutMins is timeout for which a gitspace instance can be in cleaning state.
|
||||
const gitspaceInstanceCleaningTimedOutMins = 15
|
||||
|
||||
type Controller struct {
|
||||
authorizer authz.Authorizer
|
||||
infraProviderSvc *infraprovider.Service
|
||||
|
|
|
@ -17,11 +17,9 @@ package gitspace
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
apiauth "github.com/harness/gitness/app/api/auth"
|
||||
"github.com/harness/gitness/app/auth"
|
||||
"github.com/harness/gitness/types"
|
||||
"github.com/harness/gitness/types/enum"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
@ -71,40 +69,6 @@ func (c *Controller) Delete(
|
|||
}
|
||||
|
||||
ctxWithoutCancel := context.WithoutCancel(ctx)
|
||||
go c.removeGitspace(ctxWithoutCancel, *gitspaceConfig)
|
||||
go c.gitspaceSvc.RemoveGitspace(ctxWithoutCancel, *gitspaceConfig, true)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Controller) removeGitspace(ctx context.Context, config types.GitspaceConfig) {
|
||||
if config.GitspaceInstance.State == enum.GitspaceInstanceStateRunning {
|
||||
activeTimeEnded := time.Now().UnixMilli()
|
||||
config.GitspaceInstance.ActiveTimeEnded = &activeTimeEnded
|
||||
config.GitspaceInstance.TotalTimeUsed =
|
||||
*(config.GitspaceInstance.ActiveTimeEnded) - *(config.GitspaceInstance.ActiveTimeStarted)
|
||||
config.GitspaceInstance.State = enum.GitspaceInstanceStateStopping
|
||||
err := c.gitspaceSvc.UpdateInstance(ctx, config.GitspaceInstance)
|
||||
if err != nil {
|
||||
log.Ctx(ctx).Err(err).Msgf("failed to update instance %s before triggering delete",
|
||||
config.GitspaceInstance.Identifier)
|
||||
return
|
||||
}
|
||||
} else if config.GitspaceInstance.State == enum.GitSpaceInstanceStateCleaning &&
|
||||
time.Since(time.UnixMilli(config.GitspaceInstance.Updated)).Milliseconds() <=
|
||||
(gitspaceInstanceCleaningTimedOutMins*60*1000) {
|
||||
log.Ctx(ctx).Warn().Msgf("gitspace start/stop is already pending for : %q",
|
||||
config.GitspaceInstance.Identifier)
|
||||
return
|
||||
}
|
||||
if err := c.gitspaceSvc.TriggerDelete(ctx, config); err != nil {
|
||||
log.Ctx(ctx).Err(err).Msgf("error during triggering delete for gitspace instance %s",
|
||||
config.GitspaceInstance.Identifier)
|
||||
config.GitspaceInstance.State = enum.GitspaceInstanceStateError
|
||||
if updateErr := c.gitspaceSvc.UpdateInstance(ctx, config.GitspaceInstance); updateErr != nil {
|
||||
log.Ctx(ctx).Err(updateErr).Msgf("failed to update instance %s after error in triggering delete",
|
||||
config.GitspaceInstance.Identifier)
|
||||
}
|
||||
return
|
||||
}
|
||||
log.Ctx(ctx).Debug().Msgf("successfully triggered delete for gitspace instance %s",
|
||||
config.GitspaceInstance.Identifier)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
// Copyright 2023 Harness, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package gitspace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/harness/gitness/types"
|
||||
"github.com/harness/gitness/types/enum"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// gitspaceInstanceCleaningTimedOutMins is timeout for which a gitspace instance can be in cleaning state.
|
||||
const gitspaceInstanceCleaningTimedOutMins = 15
|
||||
|
||||
func (c *Service) RemoveGitspace(ctx context.Context, config types.GitspaceConfig, canDeleteUserData bool) {
|
||||
if config.GitspaceInstance.State == enum.GitSpaceInstanceStateCleaning &&
|
||||
time.Since(time.UnixMilli(config.GitspaceInstance.Updated)).Milliseconds() <=
|
||||
(gitspaceInstanceCleaningTimedOutMins*60*1000) {
|
||||
log.Ctx(ctx).Warn().Msgf("gitspace start/stop is already pending for : %q",
|
||||
config.GitspaceInstance.Identifier)
|
||||
return
|
||||
}
|
||||
|
||||
if config.GitspaceInstance.State == enum.GitspaceInstanceStateRunning {
|
||||
activeTimeEnded := time.Now().UnixMilli()
|
||||
config.GitspaceInstance.ActiveTimeEnded = &activeTimeEnded
|
||||
config.GitspaceInstance.TotalTimeUsed =
|
||||
*(config.GitspaceInstance.ActiveTimeEnded) - *(config.GitspaceInstance.ActiveTimeStarted)
|
||||
config.GitspaceInstance.State = enum.GitspaceInstanceStateStopping
|
||||
} else {
|
||||
config.GitspaceInstance.State = enum.GitSpaceInstanceStateCleaning
|
||||
}
|
||||
|
||||
err := c.UpdateInstance(ctx, config.GitspaceInstance)
|
||||
if err != nil {
|
||||
log.Ctx(ctx).Err(err).Msgf("failed to update instance %s before triggering delete",
|
||||
config.GitspaceInstance.Identifier)
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.TriggerDelete(ctx, config, canDeleteUserData); err != nil {
|
||||
log.Ctx(ctx).Err(err).Msgf("error during triggering delete for gitspace instance %s",
|
||||
config.GitspaceInstance.Identifier)
|
||||
config.GitspaceInstance.State = enum.GitspaceInstanceStateError
|
||||
if updateErr := c.UpdateInstance(ctx, config.GitspaceInstance); updateErr != nil {
|
||||
log.Ctx(ctx).Err(updateErr).Msgf("failed to update instance %s after error in triggering delete",
|
||||
config.GitspaceInstance.Identifier)
|
||||
}
|
||||
return
|
||||
}
|
||||
log.Ctx(ctx).Debug().Msgf("successfully triggered delete for gitspace instance %s",
|
||||
config.GitspaceInstance.Identifier)
|
||||
}
|
||||
|
||||
func (c *Service) TriggerDelete(
|
||||
ctx context.Context,
|
||||
config types.GitspaceConfig,
|
||||
canDeleteUserData bool,
|
||||
) error {
|
||||
return c.orchestrator.TriggerDeleteGitspace(ctx, config, canDeleteUserData)
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
// Copyright 2023 Harness, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package gitspace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/harness/gitness/store"
|
||||
"github.com/harness/gitness/types"
|
||||
"github.com/harness/gitness/types/enum"
|
||||
)
|
||||
|
||||
func (c *Service) StartGitspaceAction(
|
||||
ctx context.Context,
|
||||
config *types.GitspaceConfig,
|
||||
) error {
|
||||
savedGitspaceInstance, err := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, config.ID)
|
||||
if err != nil && !errors.Is(err, store.ErrResourceNotFound) {
|
||||
return err
|
||||
}
|
||||
config.GitspaceInstance = savedGitspaceInstance
|
||||
err = c.gitspaceBusyOperation(ctx, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if savedGitspaceInstance == nil || savedGitspaceInstance.State.IsFinalStatus() {
|
||||
gitspaceInstance, err := c.buildGitspaceInstance(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if savedGitspaceInstance != nil {
|
||||
gitspaceInstance.HasGitChanges = savedGitspaceInstance.HasGitChanges
|
||||
}
|
||||
|
||||
if err = c.gitspaceInstanceStore.Create(ctx, gitspaceInstance); err != nil {
|
||||
return fmt.Errorf("failed to create gitspace instance for %s %w", config.Identifier, err)
|
||||
}
|
||||
}
|
||||
newGitspaceInstance, err := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, config.ID)
|
||||
newGitspaceInstance.SpacePath = config.SpacePath
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to find gitspace with config ID : %s %w", config.Identifier, err)
|
||||
}
|
||||
config.GitspaceInstance = newGitspaceInstance
|
||||
c.submitAsyncOps(ctx, config, enum.GitspaceActionTypeStart)
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
// Copyright 2023 Harness, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package gitspace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/harness/gitness/types"
|
||||
"github.com/harness/gitness/types/enum"
|
||||
)
|
||||
|
||||
func (c *Service) StopGitspaceAction(
|
||||
ctx context.Context,
|
||||
config *types.GitspaceConfig,
|
||||
now time.Time,
|
||||
) error {
|
||||
savedGitspaceInstance, err := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, config.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to find gitspace with config ID : %s %w", config.Identifier, err)
|
||||
}
|
||||
if savedGitspaceInstance.State.IsFinalStatus() {
|
||||
return fmt.Errorf("gitspace instance cannot be stopped with ID %s", savedGitspaceInstance.Identifier)
|
||||
}
|
||||
config.GitspaceInstance = savedGitspaceInstance
|
||||
err = c.gitspaceBusyOperation(ctx, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
activeTimeEnded := now.UnixMilli()
|
||||
config.GitspaceInstance.ActiveTimeEnded = &activeTimeEnded
|
||||
config.GitspaceInstance.TotalTimeUsed =
|
||||
*(config.GitspaceInstance.ActiveTimeEnded) - *(config.GitspaceInstance.ActiveTimeStarted)
|
||||
config.GitspaceInstance.State = enum.GitspaceInstanceStateStopping
|
||||
if err = c.UpdateInstance(ctx, config.GitspaceInstance); err != nil {
|
||||
return fmt.Errorf("failed to update gitspace config for stopping %s %w", config.Identifier, err)
|
||||
}
|
||||
c.submitAsyncOps(ctx, config, enum.GitspaceActionTypeStop)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Service) GitspaceAutostopAction(
|
||||
ctx context.Context,
|
||||
config *types.GitspaceConfig,
|
||||
now time.Time,
|
||||
) error {
|
||||
c.EmitGitspaceConfigEvent(ctx, config, enum.GitspaceEventTypeGitspaceAutoStop)
|
||||
if err := c.StopGitspaceAction(ctx, config, now); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -16,7 +16,6 @@ package gitspace
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
@ -24,7 +23,6 @@ import (
|
|||
|
||||
"github.com/harness/gitness/app/api/usererror"
|
||||
events "github.com/harness/gitness/app/events/gitspace"
|
||||
"github.com/harness/gitness/store"
|
||||
"github.com/harness/gitness/types"
|
||||
"github.com/harness/gitness/types/enum"
|
||||
|
||||
|
@ -36,48 +34,6 @@ const defaultPasswordRef = "harness_password"
|
|||
const defaultMachineUser = "harness"
|
||||
const AllowedUIDAlphabet = "abcdefghijklmnopqrstuvwxyz0123456789"
|
||||
|
||||
func (c *Service) StopGitspaceAction(
|
||||
ctx context.Context,
|
||||
config *types.GitspaceConfig,
|
||||
now time.Time,
|
||||
) error {
|
||||
savedGitspaceInstance, err := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, config.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to find gitspace with config ID : %s %w", config.Identifier, err)
|
||||
}
|
||||
if savedGitspaceInstance.State.IsFinalStatus() {
|
||||
return fmt.Errorf("gitspace instance cannot be stopped with ID %s", savedGitspaceInstance.Identifier)
|
||||
}
|
||||
config.GitspaceInstance = savedGitspaceInstance
|
||||
err = c.gitspaceBusyOperation(ctx, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
activeTimeEnded := now.UnixMilli()
|
||||
config.GitspaceInstance.ActiveTimeEnded = &activeTimeEnded
|
||||
config.GitspaceInstance.TotalTimeUsed =
|
||||
*(config.GitspaceInstance.ActiveTimeEnded) - *(config.GitspaceInstance.ActiveTimeStarted)
|
||||
config.GitspaceInstance.State = enum.GitspaceInstanceStateStopping
|
||||
if err = c.UpdateInstance(ctx, config.GitspaceInstance); err != nil {
|
||||
return fmt.Errorf("failed to update gitspace config for stopping %s %w", config.Identifier, err)
|
||||
}
|
||||
c.submitAsyncOps(ctx, config, enum.GitspaceActionTypeStop)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Service) GitspaceAutostopAction(
|
||||
ctx context.Context,
|
||||
config *types.GitspaceConfig,
|
||||
now time.Time,
|
||||
) error {
|
||||
c.EmitGitspaceConfigEvent(ctx, config, enum.GitspaceEventTypeGitspaceAutoStop)
|
||||
if err := c.StopGitspaceAction(ctx, config, now); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Service) gitspaceBusyOperation(
|
||||
ctx context.Context,
|
||||
config *types.GitspaceConfig,
|
||||
|
@ -145,43 +101,6 @@ func (c *Service) submitAsyncOps(
|
|||
}()
|
||||
}
|
||||
|
||||
func (c *Service) StartGitspaceAction(
|
||||
ctx context.Context,
|
||||
config *types.GitspaceConfig,
|
||||
) error {
|
||||
savedGitspaceInstance, err := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, config.ID)
|
||||
if err != nil && !errors.Is(err, store.ErrResourceNotFound) {
|
||||
return err
|
||||
}
|
||||
config.GitspaceInstance = savedGitspaceInstance
|
||||
err = c.gitspaceBusyOperation(ctx, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if savedGitspaceInstance == nil || savedGitspaceInstance.State.IsFinalStatus() {
|
||||
gitspaceInstance, err := c.buildGitspaceInstance(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if savedGitspaceInstance != nil {
|
||||
gitspaceInstance.HasGitChanges = savedGitspaceInstance.HasGitChanges
|
||||
}
|
||||
|
||||
if err = c.gitspaceInstanceStore.Create(ctx, gitspaceInstance); err != nil {
|
||||
return fmt.Errorf("failed to create gitspace instance for %s %w", config.Identifier, err)
|
||||
}
|
||||
}
|
||||
newGitspaceInstance, err := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, config.ID)
|
||||
newGitspaceInstance.SpacePath = config.SpacePath
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to find gitspace with config ID : %s %w", config.Identifier, err)
|
||||
}
|
||||
config.GitspaceInstance = newGitspaceInstance
|
||||
c.submitAsyncOps(ctx, config, enum.GitspaceActionTypeStart)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Service) asyncOperation(
|
||||
ctxWithTimedOut context.Context,
|
||||
config types.GitspaceConfig,
|
|
@ -1,28 +0,0 @@
|
|||
// Copyright 2023 Harness, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package gitspace
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/harness/gitness/types"
|
||||
)
|
||||
|
||||
func (c *Service) TriggerDelete(
|
||||
ctx context.Context,
|
||||
config types.GitspaceConfig,
|
||||
) error {
|
||||
return c.orchestrator.TriggerDeleteGitspace(ctx, config, true)
|
||||
}
|
Loading…
Reference in New Issue