refactor: tidy runner actions manager

This commit is contained in:
Marc 2024-09-02 22:55:29 -04:00
parent e25735ae01
commit 09f2a1c4b2
Signed by: marc
GPG key ID: 048E042F22B5DC79
3 changed files with 22 additions and 15 deletions

View file

@ -14,8 +14,8 @@ type ActionsManager struct {
cacheRoot string
}
func NewActionsManager(cacheRoot string) ActionsManager {
return ActionsManager{
func NewActionsManager(cacheRoot string) *ActionsManager {
return &ActionsManager{
git: GitClient{},
cacheRoot: cacheRoot,
}

View file

@ -12,13 +12,19 @@ import (
"sync"
)
// Supervisor structure that handles running workflows, triggering
// side-effects along the lifecycle of each run.
type Runner struct {
// Image labels that can be used by workflows.
Labels map[string]string
// Handler for container operations.
Driver driver.ContainerDriver
Cache cache.CacheManager
Runs int
// Deferred tasks, in order their were scheduled.
deferred *DeferredTaskManager
// Runner's cache.
Cache *cache.CacheManager
// Deferred tasks that can be queued during runs.
Deferred *DeferredTaskManager
// Actions handling.
Actions *actions.ActionsManager
}
func NewRunner(driver driver.ContainerDriver, labels map[string]string, cacheRoot string) Runner {
@ -26,9 +32,10 @@ func NewRunner(driver driver.ContainerDriver, labels map[string]string, cacheRoo
return Runner{
Driver: driver,
Cache: *cacheManager,
Cache: cacheManager,
Labels: labels,
deferred: NewDeferredTaskManager(),
Deferred: NewDeferredTaskManager(),
Actions: actions.NewActionsManager(cacheManager.Root),
}
}
@ -43,13 +50,13 @@ func (r *Runner) RunWorkflow(workflow workflows.Workflow) TaskTracker {
workflowContext := context.WithValue(context.Background(), "workflow", workflow)
defer r.deferred.RunAllDeferredTasks()
defer r.Deferred.RunAllDeferredTasks()
workflow.Walk(func(n interface{}) {
switch n.(type) {
case workflows.Step:
if useAction := n.(workflows.Step).Uses; useAction != "" {
actions.NewActionsManager(r.Cache.Root).PrefetchAction(useAction)
r.Actions.PrefetchAction(useAction)
}
}
})
@ -86,7 +93,7 @@ func (r Runner) runJob(jobContext context.Context, jobTracker *TaskTracker, jobW
containerName := fmt.Sprintf("runner-%s", jobTracker.TaskId)
defer jobWaitGroup.Done()
defer r.deferred.RunDeferredTasksInScope(fmt.Sprintf("job-%s", containerName))
defer r.Deferred.RunDeferredTasksInScope(fmt.Sprintf("job-%s", containerName))
jobTracker.SetStatus("started")
runnerImage := jobContext.Value("runnerImageUri").(string)
@ -139,7 +146,7 @@ func (r *Runner) RunCommandInContainer(containerId string, command string, optio
func (r *Runner) RunJobInContainer(imageUri string, containerId string, jobContext context.Context) error {
r.Driver.Start(imageUri, containerId, r.Cache.Root)
r.deferred.Queue(fmt.Sprintf("job-%s", containerId), func() {
r.Deferred.Queue(fmt.Sprintf("job-%s", containerId), func() {
logger.Info("Started cleaning up %s", containerId)
r.Driver.Stop(containerId)
})
@ -174,7 +181,7 @@ func (r *Runner) RunJobInContainer(imageUri string, containerId string, jobConte
Env: stepEnv,
Shell: stepShell,
}
stepError = actions.NewActionsManager(r.Cache.Root).UseAction(step.Uses, containerId, commandOptions)
stepError = r.Actions.UseAction(step.Uses, containerId, commandOptions)
}
if stepError != nil && !step.ContinueOnError {

View file

@ -55,7 +55,7 @@ func TestRunJobInContainerSchedulesStoppingContainers(t *testing.T) {
runner.RunJobInContainer("uri", "container", jobCtx)
if len(runner.deferred.GetAllTasks()) != 1 {
t.Errorf("Expected 1 deferred task, found %d instead.", len(runner.deferred.GetAllTasks()))
if len(runner.Deferred.GetAllTasks()) != 1 {
t.Errorf("Expected 1 deferred task, found %d instead.", len(runner.Deferred.GetAllTasks()))
}
}