@@ -44,6 +44,7 @@ import (
4444 "time"
4545
4646 "github.com/AliceO2Group/Control/common"
47+ "github.com/AliceO2Group/Control/common/event"
4748 "github.com/AliceO2Group/Control/common/logger"
4849 "github.com/AliceO2Group/Control/executor/executorcmd"
4950 "github.com/AliceO2Group/Control/executor/protos"
@@ -491,10 +492,10 @@ func launch(state *internalState, task mesos.TaskInfo) {
491492 } else {
492493 log .WithField ("state" , response .GetState ()).WithField ("task" , task .Name ).Debug ("task status queried" )
493494 }
495+ // NOTE: we acquire the transitioner-dependent STANDBY equivalent state
494496 reachedState := state .rpcClients [task .TaskID ].FromDeviceState (response .GetState ())
495497 state .mu .RUnlock ()
496498
497- // FIXME: we should compare the reached state against transitioner-dependent IDLE instead of plain IDLE
498499 if reachedState == "STANDBY" && err == nil {
499500 log .WithField ("id" , task .TaskID .Value ).
500501 WithField ("task" , task .Name ).
@@ -519,6 +520,23 @@ func launch(state *internalState, task mesos.TaskInfo) {
519520 }
520521 }
521522
523+ // Set up event stream from task
524+ state .mu .RLock ()
525+ esc , err := state .rpcClients [task .TaskID ].EventStream (context .TODO (), & pb.EventStreamRequest {}, grpc.EmptyCallOption {})
526+ state .mu .RUnlock ()
527+ if err != nil {
528+ log .WithField ("task" , task .Name ).WithError (err ).Error ("cannot set up event stream from task" )
529+ status .State = mesos .TASK_FAILED .Enum ()
530+ status .Message = protoString (err .Error ())
531+
532+ state .mu .Lock ()
533+ state .failedTasks [task .TaskID ] = status
534+ state .rpcClients [task .TaskID ].Close ()
535+ delete (state .rpcClients , task .TaskID )
536+ state .mu .Unlock ()
537+ return
538+ }
539+
522540 log .WithField ("task" , task .Name ).Debug ("notifying of task running state" )
523541 status .State = mesos .TASK_RUNNING .Enum ()
524542
@@ -561,6 +579,44 @@ func launch(state *internalState, task mesos.TaskInfo) {
561579 }
562580 state .mu .Unlock ()
563581
582+ // Process events from task
583+ go func () {
584+ deo := event.DeviceEventOrigin {
585+ AgentId : task .AgentID ,
586+ ExecutorId : task .GetExecutor ().ExecutorID ,
587+ TaskId : task .TaskID ,
588+ }
589+ for {
590+ esr , err := esc .Recv ()
591+ if err == io .EOF {
592+ log .WithError (err ).Warning ("event stream EOF" )
593+ break
594+ }
595+ if err != nil {
596+ log .WithError (err ).Warning ("error receiving event from task" )
597+ continue
598+ }
599+ ev := esr .GetEvent ()
600+
601+ deviceEvent := event .NewDeviceEvent (deo , ev .GetType ())
602+
603+ jsonEvent , err := json .Marshal (deviceEvent )
604+ if err != nil {
605+ log .WithError (err ).Warning ("error marshaling event from task" )
606+ continue
607+ }
608+
609+ state .mu .RLock ()
610+ state .cli .Send (context .TODO (), calls .NonStreaming (calls .Message (jsonEvent )))
611+ log .WithFields (logrus.Fields {
612+ "task" : task .TaskID .Value ,
613+ "event" : string (jsonEvent ),
614+ }).
615+ Debug ("event sent" )
616+ state .mu .RUnlock ()
617+ }
618+ }()
619+
564620 err = taskCmd .Wait ()
565621
566622 state .mu .Lock ()
0 commit comments