@@ -29,6 +29,7 @@ import (
2929 "encoding/json"
3030 "errors"
3131 "io"
32+ "strconv"
3233 "strings"
3334 "sync"
3435 "time"
@@ -42,6 +43,7 @@ import (
4243 "github.com/AliceO2Group/Control/core/environment"
4344 "github.com/AliceO2Group/Control/core/integration"
4445 lhcevent "github.com/AliceO2Group/Control/core/integration/lhc/event"
46+ "github.com/AliceO2Group/Control/core/workflow/callable"
4547 "github.com/sirupsen/logrus"
4648 "github.com/spf13/viper"
4749)
@@ -175,8 +177,18 @@ func (p *Plugin) GetEnvironmentsShortData(envIds []uid.ID) map[uid.ID]string {
175177func (p * Plugin ) ObjectStack (_ map [string ]string , _ map [string ]string ) (stack map [string ]interface {}) {
176178 return make (map [string ]interface {})
177179}
178- func (p * Plugin ) CallStack (_ interface {}) (stack map [string ]interface {}) {
179- return make (map [string ]interface {})
180+ func (p * Plugin ) CallStack (data interface {}) (stack map [string ]interface {}) {
181+ call , ok := data .(* callable.Call )
182+ if ! ok {
183+ return
184+ }
185+
186+ stack = make (map [string ]interface {})
187+ stack ["UpdateFillInfo" ] = func () (out string ) {
188+ p .updateFillInfo (call )
189+ return
190+ }
191+ return
180192}
181193
182194func (p * Plugin ) Destroy () error {
@@ -248,3 +260,83 @@ func (p *Plugin) readAndInjectLhcUpdates() {
248260 }
249261 }
250262}
263+
264+ // UpdateFillInfo: propagate latest LHC fill info into the environment's global runtime vars
265+ func (p * Plugin ) updateFillInfo (call * callable.Call ) (out string ) {
266+ varStack := call .VarStack
267+ envId , ok := varStack ["environment_id" ]
268+ if ! ok {
269+ err := errors .New ("cannot acquire environment ID" )
270+ log .Error (err )
271+
272+ call .VarStack ["__call_error_reason" ] = err .Error ()
273+ call .VarStack ["__call_error" ] = "LHC plugin Call Stack failed"
274+ return
275+ }
276+
277+ log := log .WithFields (logrus.Fields {
278+ "partition" : envId ,
279+ "call" : "UpdateFillInfo" ,
280+ })
281+
282+ parentRole , ok := call .GetParentRole ().(callable.ParentRole )
283+ if ! ok || parentRole == nil {
284+ log .WithField (infologger .Level , infologger .IL_Support ).
285+ Error ("cannot access parent role to propagate LHC fill info" )
286+ return
287+ }
288+
289+ if p .currentState == nil {
290+ log .WithField (infologger .Level , infologger .IL_Support ).
291+ Warn ("attempted to update environment with fill info, but fill info is not available in plugin" )
292+ return
293+ }
294+
295+ // note: the following was causing very weird behaviours, which could be attributed to memory corruption.
296+ // I did not manage to understand why can't we safely clone such a proto message.
297+ // state := proto.Clone(p.currentState).(*pb.BeamInfo)
298+
299+ p .mu .Lock ()
300+ defer p .mu .Unlock ()
301+ state := p .currentState
302+
303+ parentRole .SetGlobalRuntimeVar ("fill_info_beam_mode" , state .BeamMode .String ())
304+
305+ // If NO_BEAM, clear all other fill info and return
306+ if state .BeamMode == pb .BeamMode_NO_BEAM {
307+ parentRole .DeleteGlobalRuntimeVar ("fill_info_fill_number" )
308+ parentRole .DeleteGlobalRuntimeVar ("fill_info_filling_scheme" )
309+ parentRole .DeleteGlobalRuntimeVar ("fill_info_beam_type" )
310+ parentRole .DeleteGlobalRuntimeVar ("fill_info_stable_beam_start_ms" )
311+ parentRole .DeleteGlobalRuntimeVar ("fill_info_stable_beam_end_ms" )
312+
313+ log .WithField (infologger .Level , infologger .IL_Devel ).
314+ Debug ("NO_BEAM — cleared fill info vars and set beam mode only" )
315+ return
316+ }
317+
318+ // Otherwise, propagate latest known info
319+ parentRole .SetGlobalRuntimeVar ("fill_info_fill_number" , strconv .FormatInt (int64 (state .FillNumber ), 10 ))
320+ parentRole .SetGlobalRuntimeVar ("fill_info_filling_scheme" , state .FillingSchemeName )
321+ parentRole .SetGlobalRuntimeVar ("fill_info_beam_type" , state .BeamType )
322+ if state .StableBeamsStart > 0 {
323+ parentRole .SetGlobalRuntimeVar ("fill_info_stable_beam_start_ms" , strconv .FormatInt (state .StableBeamsStart , 10 ))
324+ } else {
325+ parentRole .DeleteGlobalRuntimeVar ("fill_info_stable_beam_start_ms" )
326+ }
327+ if state .StableBeamsEnd > 0 {
328+ parentRole .SetGlobalRuntimeVar ("fill_info_stable_beam_end_ms" , strconv .FormatInt (state .StableBeamsEnd , 10 ))
329+ } else {
330+ parentRole .DeleteGlobalRuntimeVar ("fill_info_stable_beam_end_ms" )
331+ }
332+
333+ log .WithField ("fillNumber" , state .FillNumber ).
334+ WithField ("fillingScheme" , state .FillingSchemeName ).
335+ WithField ("beamType" , state .BeamType ).
336+ WithField ("beamMode" , state .BeamMode ).
337+ WithField ("stableStartMs" , state .StableBeamsStart ).
338+ WithField ("stableEndMs" , state .StableBeamsEnd ).
339+ WithField (infologger .Level , infologger .IL_Devel ).
340+ Debug ("updated environment fill info from latest snapshot" )
341+ return
342+ }
0 commit comments