mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-09-15 18:57:01 +00:00
feat: assign a unique random name to each run context
If the run context has a parent, both share the same unique random name. A composite action does not have a run context of its own, it re-uses the run context of the job that calls it: this is when a parent is used and needed. There may be any level of parent / child relationship and ensureRandom name recursively look for the first parent with a non empty random.
This commit is contained in:
parent
023427115f
commit
a197fea4ba
2 changed files with 35 additions and 0 deletions
|
@ -52,6 +52,7 @@ type RunContext struct {
|
||||||
Masks []string
|
Masks []string
|
||||||
cleanUpJobContainer common.Executor
|
cleanUpJobContainer common.Executor
|
||||||
caller *caller // job calling this RunContext (reusable workflows)
|
caller *caller // job calling this RunContext (reusable workflows)
|
||||||
|
randomName string
|
||||||
networkName string
|
networkName string
|
||||||
networkCreated bool
|
networkCreated bool
|
||||||
}
|
}
|
||||||
|
@ -391,6 +392,25 @@ func (rc *RunContext) startHostEnvironment() common.Executor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rc *RunContext) ensureRandomName(ctx context.Context) {
|
||||||
|
if rc.randomName == "" {
|
||||||
|
logger := common.Logger(ctx)
|
||||||
|
if rc.Parent != nil {
|
||||||
|
// composite actions inherit their run context from the parent job
|
||||||
|
rootRunContext := rc
|
||||||
|
for rootRunContext.Parent != nil {
|
||||||
|
rootRunContext = rootRunContext.Parent
|
||||||
|
}
|
||||||
|
rootRunContext.ensureRandomName(ctx)
|
||||||
|
rc.randomName = rootRunContext.randomName
|
||||||
|
logger.Debugf("RunContext inherited random name %s from its parent", rc.Name, rc.randomName)
|
||||||
|
} else {
|
||||||
|
rc.randomName = common.MustRandName(16)
|
||||||
|
logger.Debugf("RunContext %s is assigned random name %s", rc.Name, rc.randomName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (rc *RunContext) getNetworkCreated(ctx context.Context) bool {
|
func (rc *RunContext) getNetworkCreated(ctx context.Context) bool {
|
||||||
rc.ensureNetworkName(ctx)
|
rc.ensureNetworkName(ctx)
|
||||||
return rc.networkCreated
|
return rc.networkCreated
|
||||||
|
|
|
@ -937,3 +937,18 @@ func Test_waitForServiceContainer(t *testing.T) {
|
||||||
m.AssertExpectations(t)
|
m.AssertExpectations(t)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRunContext_ensureRandomName(t *testing.T) {
|
||||||
|
parent := &RunContext{
|
||||||
|
Name: "parentname",
|
||||||
|
}
|
||||||
|
rc := &RunContext{
|
||||||
|
Name: "runname",
|
||||||
|
Parent: parent,
|
||||||
|
}
|
||||||
|
|
||||||
|
parent.ensureRandomName(t.Context())
|
||||||
|
assert.NotEmpty(t, parent.randomName)
|
||||||
|
rc.ensureRandomName(t.Context())
|
||||||
|
assert.Equal(t, parent.randomName, rc.randomName)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue