Skip to content

Commit 87d9f35

Browse files
author
Michal Tichák
committed
[core] split CreateEnvironment to Create and Run functions
1 parent 8b14cc3 commit 87d9f35

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

core/environment/manager.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func (envs *Manager) GetActiveDetectors() system.IDMap {
202202
return response
203203
}
204204

205-
func (envs *Manager) CreateEnvironment(workflowPath string, userVars map[string]string, public bool, newId uid.ID, autoTransition bool) (uid.ID, error) {
205+
func (envs *Manager) CreateEnvironment(workflowPath string, userVars map[string]string, public bool, newId uid.ID) (uid.ID, error) {
206206
// Before we load the workflow, we get the list of currently active detectors. This query must be performed before
207207
// loading the workflow in order to compare the currently used detectors with the detectors required by the newly
208208
// created environment.
@@ -403,7 +403,19 @@ func (envs *Manager) CreateEnvironment(workflowPath string, userVars map[string]
403403
WithField("level", infologger.IL_Devel).
404404
Debug("envman write unlock")
405405

406-
err = env.TryTransition(NewDeployTransition(
406+
return env.id, nil
407+
}
408+
409+
func (envs *Manager) RunEnvironment(workflowPath string, envId uid.ID, autoTransition bool) error {
410+
envs.mu.Lock()
411+
env, ok := envs.m[envId]
412+
envs.mu.Unlock()
413+
414+
if !ok {
415+
return errors.New(fmt.Sprintf("trying to run unknown env id: %v", envId))
416+
}
417+
418+
err := env.TryTransition(NewDeployTransition(
407419
envs.taskman,
408420
nil, // roles,
409421
nil),
@@ -524,7 +536,7 @@ func (envs *Manager) CreateEnvironment(workflowPath string, userVars map[string]
524536
}()
525537
}
526538

527-
return env.id, err
539+
return err
528540
}
529541

530542
// Deployment/configuration failure code path starts here
@@ -567,7 +579,7 @@ func (envs *Manager) CreateEnvironment(workflowPath string, userVars map[string]
567579
Info("environment deployment failed, tasks were cleaned up")
568580
log.WithField("partition", env.Id().String()).Info("environment teardown complete")
569581

570-
return env.id, err
582+
return err
571583
}
572584

573585
func (envs *Manager) TeardownEnvironment(environmentId uid.ID, force bool) error {

core/server.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,13 @@ func (m *RpcServer) doNewEnvironmentAsync(cxt context.Context, userVars map[stri
309309
// we store the last known request user in the environment
310310
lastRequestUserJ, _ := json.Marshal(requestUser)
311311
userVars["last_request_user"] = string(lastRequestUserJ[:])
312-
id, err = m.state.environments.CreateEnvironment(workflowTemplate, userVars, public, id, autoTransition)
312+
err = m.state.environments.RunEnvironment(workflowTemplate, id, autoTransition)
313313
if err != nil {
314314
the.EventWriterWithTopic(topic.Environment).WriteEvent(&evpb.Ev_EnvironmentEvent{
315315
EnvironmentId: id.String(),
316316
State: "ERROR",
317317
Error: err.Error(),
318-
Message: "cannot create new environment", // GUI listens for this concrete string
318+
Message: "cannot run new environment", // GUI listens for this concrete string
319319
LastRequestUser: requestUser,
320320
WorkflowTemplateInfo: &evpb.WorkflowTemplateInfo{
321321
Public: public,
@@ -374,7 +374,26 @@ func (m *RpcServer) NewEnvironmentAsync(cxt context.Context, request *pb.NewEnvi
374374
}
375375
defer setCurrentUnixMilli(&reply.Timestamp)
376376

377-
go m.doNewEnvironmentAsync(cxt, userVars, request.GetRequestUser(), request.GetWorkflowTemplate(), request.GetPublic(), request.GetAutoTransition(), id)
377+
public := request.GetPublic()
378+
workflowTemplate := request.GetWorkflowTemplate()
379+
requestUser := request.GetRequestUser()
380+
381+
id, err = m.state.environments.CreateEnvironment(request.GetWorkflowTemplate(), userVars, request.GetPublic(), id)
382+
if err != nil {
383+
the.EventWriterWithTopic(topic.Environment).WriteEvent(&evpb.Ev_EnvironmentEvent{
384+
EnvironmentId: id.String(),
385+
State: "ERROR",
386+
Error: err.Error(),
387+
Message: "cannot create new environment", // GUI listens for this concrete string
388+
LastRequestUser: requestUser,
389+
WorkflowTemplateInfo: &evpb.WorkflowTemplateInfo{
390+
Public: public,
391+
Path: workflowTemplate,
392+
},
393+
})
394+
return
395+
}
396+
go m.doNewEnvironmentAsync(cxt, userVars, requestUser, workflowTemplate, public, request.GetAutoTransition(), id)
378397

379398
return
380399
}
@@ -421,7 +440,7 @@ func (m *RpcServer) NewEnvironment(cxt context.Context, request *pb.NewEnvironme
421440

422441
// Create new Environment instance with some roles, we get back a UUID
423442
id := uid.New()
424-
id, err = m.state.environments.CreateEnvironment(request.GetWorkflowTemplate(), inputUserVars, request.GetPublic(), id, request.GetAutoTransition())
443+
id, err = m.state.environments.CreateEnvironment(request.GetWorkflowTemplate(), inputUserVars, request.GetPublic(), id)
425444
if err != nil {
426445
st := status.Newf(codes.Internal, "cannot create new environment: %s", utils.TruncateString(err.Error(), MAX_ERROR_LENGTH))
427446
ei := &pb.EnvironmentInfo{
@@ -436,6 +455,20 @@ func (m *RpcServer) NewEnvironment(cxt context.Context, request *pb.NewEnvironme
436455
return
437456
}
438457

458+
err = m.state.environments.RunEnvironment(request.GetWorkflowTemplate(), id, request.GetAutoTransition())
459+
if err != nil {
460+
st := status.Newf(codes.Internal, "cannot run new environment: %s", utils.TruncateString(err.Error(), MAX_ERROR_LENGTH))
461+
ei := &pb.EnvironmentInfo{
462+
Id: id.String(),
463+
CreatedWhen: time.Now().UnixMilli(),
464+
State: "ERROR", // not really, but close
465+
NumberOfFlps: 0,
466+
}
467+
st, _ = st.WithDetails(ei)
468+
err = st.Err()
469+
470+
return
471+
}
439472
newEnv, err := m.state.environments.Environment(id)
440473
if err != nil {
441474
st := status.Newf(codes.Internal, "cannot get newly created environment: %s", utils.TruncateString(err.Error(), MAX_ERROR_LENGTH))

0 commit comments

Comments
 (0)