@@ -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 , autoTransition bool ) (resultEnvId uid.ID , resultErr 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.
@@ -214,6 +214,31 @@ func (envs *Manager) CreateEnvironment(workflowPath string, userVars map[string]
214214 _ = json .Unmarshal ([]byte (lastRequestUserJ ), lastRequestUser )
215215 }
216216
217+ // CreateEnvironment() is not transition from state machine, so we need to emit the same message as in TryTransition
218+ the .EventWriterWithTopic (topic .Environment ).WriteEvent (& evpb.Ev_EnvironmentEvent {
219+ EnvironmentId : newId .String (),
220+ State : "PENDING" ,
221+ Transition : "CREATE" ,
222+ TransitionStatus : evpb .OpStatus_STARTED ,
223+ LastRequestUser : lastRequestUser ,
224+ Message : "transition starting" ,
225+ })
226+
227+ // report error of the CreateEnvironment() in the same way as in TryTransition
228+ defer func () {
229+ if resultErr != nil {
230+ the .EventWriterWithTopic (topic .Environment ).WriteEvent (& evpb.Ev_EnvironmentEvent {
231+ EnvironmentId : newId .String (),
232+ Error : resultErr .Error (),
233+ LastRequestUser : lastRequestUser ,
234+ Message : "transition error" ,
235+ State : "PENDING" ,
236+ Transition : "CREATE" ,
237+ TransitionStatus : evpb .OpStatus_DONE_ERROR ,
238+ })
239+ }
240+ }()
241+
217242 // in case of err==nil, env will be false unless user
218243 // set it to True which will be overwritten in server.go
219244 workflowPublicInfo , err := parseWorkflowPublicInfo (workflowPath )
@@ -222,7 +247,10 @@ func (envs *Manager) CreateEnvironment(workflowPath string, userVars map[string]
222247 WithField ("workflow path" , workflowPath ).
223248 WithError (err ).
224249 Warn ("parse workflow public info failed." )
225- return newId , fmt .Errorf ("workflow public info parsing failed: %w" , err )
250+
251+ resultEnvId = newId
252+ resultErr = fmt .Errorf ("workflow public info parsing failed: %w" , err )
253+ return
226254 }
227255
228256 the .EventWriterWithTopic (topic .Environment ).WriteEvent (& evpb.Ev_EnvironmentEvent {
@@ -305,7 +333,9 @@ func (envs *Manager) CreateEnvironment(workflowPath string, userVars map[string]
305333 log .WithError (err ).
306334 WithField ("partition" , gotEnvId .String ()).
307335 Logf (logrus .FatalLevel , "environment creation failed" )
308- return gotEnvId , err
336+ resultEnvId = gotEnvId
337+ resultErr = err
338+ return
309339 }
310340
311341 log .WithFields (logrus.Fields {
@@ -352,21 +382,27 @@ func (envs *Manager) CreateEnvironment(workflowPath string, userVars map[string]
352382 if err != nil {
353383 err = fmt .Errorf ("cannot load workflow template: %w" , err )
354384
355- return env .id , err
385+ resultEnvId = env .id
386+ resultErr = err
387+ return
356388 }
357389
358390 // Ensure we provide a very defaulty `detectors` variable
359391 detectors , err := the .ConfSvc ().GetDetectorsForHosts (env .GetFLPs ())
360392 if err != nil {
361393 err = fmt .Errorf ("cannot acquire detectors in loaded workflow template: %w" , err )
362394
363- return env .id , err
395+ resultEnvId = env .id
396+ resultErr = err
397+ return
364398 }
365399 detectorsStr , err := SliceToJSONSlice (detectors )
366400 if err != nil {
367401 err = fmt .Errorf ("cannot process detectors in loaded workflow template: %w" , err )
368402
369- return env .id , err
403+ resultEnvId = env .id
404+ resultErr = err
405+ return
370406 }
371407 env .GlobalDefaults .Set ("detectors" , detectorsStr )
372408
@@ -379,7 +415,10 @@ func (envs *Manager) CreateEnvironment(workflowPath string, userVars map[string]
379415 for det := range neededDetectors {
380416 if _ , contains := alreadyActiveDetectors [det ]; contains {
381417 // required detector det is already active in some other environment
382- return env .id , fmt .Errorf ("detector %s is already in use" , det .String ())
418+
419+ resultEnvId = env .id
420+ resultErr = fmt .Errorf ("detector %s is already in use" , det .String ())
421+ return
383422 }
384423 }
385424
@@ -396,6 +435,15 @@ func (envs *Manager) CreateEnvironment(workflowPath string, userVars map[string]
396435 WorkflowTemplateInfo : env .GetWorkflowInfo (),
397436 })
398437
438+ the .EventWriterWithTopic (topic .Environment ).WriteEvent (& evpb.Ev_EnvironmentEvent {
439+ EnvironmentId : newId .String (),
440+ LastRequestUser : lastRequestUser ,
441+ Message : "transition completed successfully" ,
442+ State : env .CurrentState (),
443+ Transition : "CREATE" ,
444+ TransitionStatus : evpb .OpStatus_DONE_OK ,
445+ })
446+
399447 log .WithField ("method" , "CreateEnvironment" ).
400448 WithField ("level" , infologger .IL_Devel ).
401449 Debug ("envman write lock" )
@@ -528,7 +576,9 @@ func (envs *Manager) CreateEnvironment(workflowPath string, userVars map[string]
528576 }()
529577 }
530578
531- return env .id , err
579+ resultEnvId = env .id
580+ resultErr = err
581+ return
532582 }
533583
534584 // Deployment/configuration failure code path starts here
0 commit comments