Skip to content

Commit 157f3b3

Browse files
committed
[core] Emit environment events from CreateAutoEnvironment
1 parent d0fc313 commit 157f3b3

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

core/environment/manager.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,21 @@ func (envs *Manager) CreateAutoEnvironment(workflowPath string, userVars map[str
11581158
}
11591159
}
11601160

1161+
lastRequestUser := &evpb.User{}
1162+
lastRequestUserJ, ok := userVars["last_request_user"]
1163+
if ok {
1164+
_ = json.Unmarshal([]byte(lastRequestUserJ), lastRequestUser)
1165+
}
1166+
1167+
the.EventWriterWithTopic(topic.Environment).WriteEvent(&evpb.Ev_EnvironmentEvent{
1168+
EnvironmentId: newId.String(),
1169+
State: "PENDING",
1170+
Transition: "CREATE",
1171+
TransitionStep: "before_CREATE",
1172+
Message: "instantiating",
1173+
LastRequestUser: lastRequestUser,
1174+
})
1175+
11611176
env, err := newEnvironment(envUserVars, newId)
11621177
newEnvId := uid.NilID()
11631178
if err == nil && env != nil {
@@ -1173,6 +1188,15 @@ func (envs *Manager) CreateAutoEnvironment(workflowPath string, userVars map[str
11731188
"partition": newEnvId.String(),
11741189
}).Info("creating new automatic environment")
11751190

1191+
the.EventWriterWithTopic(topic.Environment).WriteEvent(&evpb.Ev_EnvironmentEvent{
1192+
EnvironmentId: newId.String(),
1193+
State: "PENDING",
1194+
Transition: "CREATE",
1195+
TransitionStep: "before_CREATE",
1196+
Message: "running hooks",
1197+
LastRequestUser: lastRequestUser,
1198+
})
1199+
11761200
env.addSubscription(sub)
11771201
defer env.closeStream()
11781202

@@ -1193,6 +1217,15 @@ func (envs *Manager) CreateAutoEnvironment(workflowPath string, userVars map[str
11931217
Warn("parse workflow public info failed.")
11941218
}
11951219

1220+
the.EventWriterWithTopic(topic.Environment).WriteEvent(&evpb.Ev_EnvironmentEvent{
1221+
EnvironmentId: newId.String(),
1222+
State: "PENDING",
1223+
Transition: "CREATE",
1224+
TransitionStep: "CREATE",
1225+
Message: "loading workflow",
1226+
LastRequestUser: lastRequestUser,
1227+
})
1228+
11961229
env.workflow, err = envs.loadWorkflow(workflowPath, env.wfAdapter, workflowUserVars, env.BaseConfigStack)
11971230
if err != nil {
11981231
err = fmt.Errorf("cannot load workflow template: %w", err)
@@ -1202,6 +1235,17 @@ func (envs *Manager) CreateAutoEnvironment(workflowPath string, userVars map[str
12021235

12031236
env.Public, env.Description, _ = parseWorkflowPublicInfo(workflowPath)
12041237

1238+
cvs, _ := env.Workflow().ConsolidatedVarStack()
1239+
the.EventWriterWithTopic(topic.Environment).WriteEvent(&evpb.Ev_EnvironmentEvent{
1240+
EnvironmentId: newId.String(),
1241+
State: env.CurrentState(),
1242+
Transition: "CREATE",
1243+
TransitionStep: "after_CREATE",
1244+
Message: "workflow loaded",
1245+
Vars: cvs, // we push the full var stack of the root role in the workflow loaded event
1246+
LastRequestUser: lastRequestUser,
1247+
})
1248+
12051249
log.WithField("method", "CreateAutoEnvironment").
12061250
WithField("level", infologger.IL_Devel).
12071251
Debug("envman write lock")

core/server.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,16 @@ func (m *RpcServer) NewAutoEnvironment(cxt context.Context, request *pb.NewAutoE
11581158
m.envStreams.add(request.GetId(), ch)
11591159
sub := environment.SubscribeToStream(ch)
11601160
id := uid.New()
1161-
go m.state.environments.CreateAutoEnvironment(request.GetWorkflowTemplate(), request.GetVars(), id, sub)
1161+
1162+
inputUserVars := request.GetVars()
1163+
if len(inputUserVars) == 0 {
1164+
inputUserVars = make(map[string]string)
1165+
}
1166+
// we store the last known request user in the environment
1167+
lastRequestUserJ, _ := json.Marshal(request.RequestUser)
1168+
inputUserVars["last_request_user"] = string(lastRequestUserJ[:])
1169+
1170+
go m.state.environments.CreateAutoEnvironment(request.GetWorkflowTemplate(), inputUserVars, id, sub)
11621171
r := &pb.NewAutoEnvironmentReply{}
11631172
return r, nil
11641173
}

0 commit comments

Comments
 (0)