Skip to content

Commit da28cdf

Browse files
committed
[core] Extend run number validity timeframe to support pre/post hooks
1 parent a94c46e commit da28cdf

4 files changed

Lines changed: 24 additions & 10 deletions

File tree

core/environment/environment.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ package environment
2929
import (
3030
"errors"
3131
"fmt"
32+
"strconv"
3233
"strings"
3334
"sync"
3435
"time"
@@ -127,6 +128,21 @@ func newEnvironment(userVars map[string]string) (env *Environment, err error) {
127128
},
128129
fsm.Callbacks{
129130
"before_event": func(e *fsm.Event) {
131+
// If the event is START_ACTIVITY, we set up a new run number early on.
132+
// This used to be done inside the transition_startactivity, but then the new RN isn't available to the
133+
// before_START_ACTIVITY hooks. By setting it up here, we ensure the run number is available especially
134+
// to plugin hooks.
135+
if e.Event == "START_ACTIVITY" {
136+
runNumber, rnErr := the.ConfSvc().NewRunNumber()
137+
if rnErr != nil {
138+
e.Cancel(rnErr)
139+
return
140+
}
141+
env.currentRunNumber = runNumber
142+
rnString := strconv.FormatUint(uint64(runNumber), 10)
143+
env.workflow.GetVars().Set("run_number", rnString)
144+
env.workflow.GetVars().Set("runNumber", rnString)
145+
}
130146
errHooks := env.handleHooks(env.Workflow(), fmt.Sprintf("before_%s", e.Event))
131147
if errHooks != nil {
132148
e.Cancel(errHooks)
@@ -173,6 +189,13 @@ func newEnvironment(userVars map[string]string) (env *Environment, err error) {
173189
e.Event,
174190
)
175191
}
192+
193+
// If the event is STOP_ACTIVITY, we remove the active run number after all hooks are done.
194+
if e.Event == "STOP_ACTIVITY" {
195+
env.currentRunNumber = 0
196+
env.workflow.GetVars().Del("run_number")
197+
env.workflow.GetVars().Del("runNumber")
198+
}
176199
},
177200
},
178201
)

core/environment/transition_startactivity.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,12 @@ func (t StartActivityTransition) do(env *Environment) (err error) {
5454
return errors.New("cannot transition in NIL environment")
5555
}
5656

57-
var runNumber uint32
58-
runNumber, err = the.ConfSvc().NewRunNumber()
59-
if err != nil {
60-
return
61-
}
57+
runNumber := env.currentRunNumber
6258

6359
log.WithField(infologger.Run, runNumber).
6460
WithField("partition", env.Id().String()).
6561
Info("starting new run")
6662

67-
env.currentRunNumber = runNumber
6863
args := controlcommands.PropertyMap{
6964
"runNumber": strconv.FormatUint(uint64(runNumber), 10),
7065
}

core/environment/transition_stopactivity.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ func (t StopActivityTransition) do(env *Environment) (err error) {
5656
WithField("partition", env.Id().String()).
5757
Info("stopping run")
5858
runNumber := env.currentRunNumber
59-
60-
env.currentRunNumber = 0
6159

6260
taskmanMessage := task.NewTransitionTaskMessage(
6361
env.Workflow().GetTasks(),

core/workflow/callable/call.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ package callable
2626

2727
import (
2828
"fmt"
29-
"strconv"
3029
texttemplate "text/template"
3130
"time"
3231

@@ -129,7 +128,6 @@ func (c *Call) Call() error {
129128
template.WrapPointer(&output),
130129
template.WrapPointer(&returnVar),
131130
}
132-
c.VarStack["run_number"] = strconv.FormatUint(uint64(c.parentRole.GetCurrentRunNumber()), 10 )
133131
c.VarStack["environment_id"] = c.parentRole.GetEnvironmentId().String()
134132
objStack := integration.PluginsInstance().ObjectStack(c)
135133

0 commit comments

Comments
 (0)