@@ -28,20 +28,24 @@ import (
2828 "context"
2929 "encoding/json"
3030 "errors"
31- "github.com/AliceO2Group/Control/common/event/topic"
32- "github.com/AliceO2Group/Control/common/logger/infologger"
33- pb "github.com/AliceO2Group/Control/common/protos"
3431 "io"
32+ "strconv"
3533 "strings"
3634 "sync"
3735 "time"
3836
37+ "github.com/AliceO2Group/Control/common/event/topic"
38+ "github.com/AliceO2Group/Control/common/logger/infologger"
39+ pb "github.com/AliceO2Group/Control/common/protos"
40+ "github.com/gogo/protobuf/proto"
41+
3942 cmnevent "github.com/AliceO2Group/Control/common/event"
4043 "github.com/AliceO2Group/Control/common/logger"
4144 "github.com/AliceO2Group/Control/common/utils/uid"
4245 "github.com/AliceO2Group/Control/core/environment"
4346 "github.com/AliceO2Group/Control/core/integration"
4447 lhcevent "github.com/AliceO2Group/Control/core/integration/lhc/event"
48+ "github.com/AliceO2Group/Control/core/workflow/callable"
4549 "github.com/sirupsen/logrus"
4650 "github.com/spf13/viper"
4751)
@@ -125,8 +129,18 @@ func (p *Plugin) GetEnvironmentsShortData(envIds []uid.ID) map[uid.ID]string {
125129func (p * Plugin ) ObjectStack (_ map [string ]string , _ map [string ]string ) (stack map [string ]interface {}) {
126130 return make (map [string ]interface {})
127131}
128- func (p * Plugin ) CallStack (_ interface {}) (stack map [string ]interface {}) {
129- return make (map [string ]interface {})
132+ func (p * Plugin ) CallStack (data interface {}) (stack map [string ]interface {}) {
133+ call , ok := data .(* callable.Call )
134+ if ! ok {
135+ return
136+ }
137+
138+ stack = make (map [string ]interface {})
139+ stack ["UpdateFillInfo" ] = func () (out string ) {
140+ p .updateFillInfo (call )
141+ return ""
142+ }
143+ return
130144}
131145
132146func (p * Plugin ) Destroy () error {
@@ -198,3 +212,74 @@ func (p *Plugin) readAndInjectLhcUpdates() {
198212 }
199213 }
200214}
215+
216+ // UpdateFillInfo: propagate latest LHC fill info into the environment's global runtime vars
217+ func (p * Plugin ) updateFillInfo (call * callable.Call ) (out string ) {
218+ varStack := call .VarStack
219+ envId , ok := varStack ["environment_id" ]
220+ if ! ok {
221+ err := errors .New ("cannot acquire environment ID" )
222+ log .Error (err )
223+
224+ call .VarStack ["__call_error_reason" ] = err .Error ()
225+ call .VarStack ["__call_error" ] = "LHC plugin Call Stack failed"
226+ return
227+ }
228+
229+ log := log .WithFields (logrus.Fields {
230+ "partition" : envId ,
231+ "call" : "UpdateFillInfo" ,
232+ })
233+
234+ parentRole , ok := call .GetParentRole ().(callable.ParentRole )
235+ if ! ok || parentRole == nil {
236+ log .WithField (infologger .Level , infologger .IL_Support ).
237+ Error ("cannot access parent role to propagate LHC fill info" )
238+ return
239+ }
240+
241+ // snapshot current state under lock
242+ p .mu .Lock ()
243+ state := proto .Clone (p .currentState ).(* pb.BeamInfo )
244+ p .mu .Unlock ()
245+
246+ parentRole .SetGlobalRuntimeVar ("fill_info_beam_mode" , state .BeamMode .String ())
247+
248+ // If NO_BEAM, clear all other fill info and return
249+ if state .BeamMode == pb .BeamMode_NO_BEAM {
250+ parentRole .DeleteGlobalRuntimeVar ("fill_info_fill_number" )
251+ parentRole .DeleteGlobalRuntimeVar ("fill_info_filling_scheme" )
252+ parentRole .DeleteGlobalRuntimeVar ("fill_info_beam_type" )
253+ parentRole .DeleteGlobalRuntimeVar ("fill_info_stable_beam_start_ms" )
254+ parentRole .DeleteGlobalRuntimeVar ("fill_info_stable_beam_end_ms" )
255+
256+ log .WithField (infologger .Level , infologger .IL_Devel ).
257+ Debug ("LHC UpdateFillInfo: NO_BEAM — cleared fill info vars and set beam mode only" )
258+ return
259+ }
260+
261+ // Otherwise, propagate latest known info
262+ parentRole .SetGlobalRuntimeVar ("fill_info_fill_number" , strconv .FormatInt (int64 (state .FillNumber ), 10 ))
263+ parentRole .SetGlobalRuntimeVar ("fill_info_filling_scheme" , state .FillingSchemeName )
264+ parentRole .SetGlobalRuntimeVar ("fill_info_beam_type" , state .BeamType )
265+ if state .StableBeamsStart > 0 {
266+ parentRole .SetGlobalRuntimeVar ("fill_info_stable_beam_start_ms" , strconv .FormatInt (state .StableBeamsStart , 10 ))
267+ } else {
268+ parentRole .DeleteGlobalRuntimeVar ("fill_info_stable_beam_start_ms" )
269+ }
270+ if state .StableBeamsEnd > 0 {
271+ parentRole .SetGlobalRuntimeVar ("fill_info_stable_beam_end_ms" , strconv .FormatInt (state .StableBeamsEnd , 10 ))
272+ } else {
273+ parentRole .DeleteGlobalRuntimeVar ("fill_info_stable_beam_end_ms" )
274+ }
275+
276+ log .WithField ("fillNumber" , state .FillNumber ).
277+ WithField ("fillingScheme" , state .FillingSchemeName ).
278+ WithField ("beamType" , state .BeamType ).
279+ WithField ("beamMode" , state .BeamMode ).
280+ WithField ("stableStartMs" , state .StableBeamsStart ).
281+ WithField ("stableEndMs" , state .StableBeamsEnd ).
282+ WithField (infologger .Level , infologger .IL_Devel ).
283+ Debug ("LHC UpdateFillInfo: updated environment fill info from latest snapshot" )
284+ return
285+ }
0 commit comments