Skip to content

Commit 2d237ee

Browse files
miltalexteo
authored andcommitted
[core] handle force destroy request
1 parent b0ec3af commit 2d237ee

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

core/environment/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (envs *Manager) CreateEnvironment(workflowPath string, userVars map[string]
118118
return env.id, err
119119
}
120120

121-
func (envs *Manager) TeardownEnvironment(environmentId uuid.UUID) error {
121+
func (envs *Manager) TeardownEnvironment(environmentId uuid.UUID, force bool) error {
122122
envs.mu.Lock()
123123
defer envs.mu.Unlock()
124124

@@ -127,7 +127,7 @@ func (envs *Manager) TeardownEnvironment(environmentId uuid.UUID) error {
127127
return err
128128
}
129129

130-
if env.CurrentState() != "STANDBY" {
130+
if env.CurrentState() != "STANDBY" && !force {
131131
return errors.New(fmt.Sprintf("cannot teardown environment in state %s", env.CurrentState()))
132132
}
133133

core/server.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,24 @@ func (m *RpcServer) DestroyEnvironment(cxt context.Context, req *pb.DestroyEnvir
299299
return nil, status.Newf(codes.NotFound, "environment not found: %s", err.Error()).Err()
300300
}
301301

302+
// if Force immediately disband the environment (unlocking all tasks) and run the cleanup.
303+
if req.Force {
304+
err = m.state.environments.TeardownEnvironment(env.Id(), req.Force)
305+
if err != nil {
306+
return &pb.DestroyEnvironmentReply{}, status.New(codes.Internal, err.Error()).Err()
307+
}
308+
309+
tasksForEnv := env.Workflow().GetTasks().GetTaskIds()
310+
killed, running, err := m.doCleanupTasks(tasksForEnv)
311+
ctr := &pb.CleanupTasksReply{KilledTasks: killed, RunningTasks: running}
312+
if err != nil {
313+
log.WithError(err).Error("task cleanup error")
314+
return &pb.DestroyEnvironmentReply{CleanupTasksReply: ctr}, status.New(codes.Internal, err.Error()).Err()
315+
}
316+
317+
return &pb.DestroyEnvironmentReply{CleanupTasksReply: ctr}, nil
318+
}
319+
302320
if req.AllowInRunningState && env.CurrentState() == "RUNNING" {
303321
err = env.TryTransition(environment.MakeTransition(m.state.taskman, pb.ControlEnvironmentRequest_STOP_ACTIVITY))
304322
if err != nil {
@@ -328,7 +346,7 @@ func (m *RpcServer) DestroyEnvironment(cxt context.Context, req *pb.DestroyEnvir
328346
}
329347
}
330348

331-
err = m.state.environments.TeardownEnvironment(env.Id())
349+
err = m.state.environments.TeardownEnvironment(env.Id(), req.Force)
332350
if err != nil {
333351
return &pb.DestroyEnvironmentReply{}, status.New(codes.Internal, err.Error()).Err()
334352
}

core/signals.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func manageKillSignals(state *internalState) {
9999
}
100100

101101
// Teardown Enviroment
102-
err = state.environments.TeardownEnvironment(uid)
102+
err = state.environments.TeardownEnvironment(uid, false)
103103
if err != nil {
104104
log.WithPrefix("termination").WithError(err).Error(fmt.Sprintf("cannot teardown enviroment %s", uid.String()))
105105
}

0 commit comments

Comments
 (0)