Skip to content

Commit

Permalink
Add task/container restartability
Browse files Browse the repository at this point in the history
  • Loading branch information
jkl73 committed May 2, 2022
1 parent a0a2373 commit ab197f4
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions launcher/container_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ func (r *ContainerRunner) fetchAndWriteToken(ctx context.Context) error {
}

// Run the container
// Doesn't support container restart yet
// Container output will always be redirected to stdio for now
func (r *ContainerRunner) Run(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
Expand All @@ -300,30 +299,36 @@ func (r *ContainerRunner) Run(ctx context.Context) error {
return fmt.Errorf("failed to fetch and write OIDC token: %v", err)
}

task, err := r.container.NewTask(ctx, cio.NewCreator(cio.WithStdio))
if err != nil {
return err
}
defer task.Delete(ctx)

exitStatus, err := task.Wait(ctx)
if err != nil {
return err
}
log.Println("task started")

if err := task.Start(ctx); err != nil {
return err
}
status := <-exitStatus
for true {
task, err := r.container.NewTask(ctx, cio.NewCreator(cio.WithStdio))
if err != nil {
return err
}
exitStatus, err := task.Wait(ctx)
if err != nil {
return err
}
log.Println("task started")

code, _, err := status.Result()
if err != nil {
return err
}
if err := task.Start(ctx); err != nil {
return err
}
status := <-exitStatus

if code != 0 {
return fmt.Errorf("task ended with non-zero return code %d", code)
code, _, err := status.Result()
if err != nil {
return err
}
task.Delete(ctx)

log.Printf("task ended with return code %d \n", code)
if r.launchSpec.RestartPolicy == spec.Always {
log.Println("restarting task")
} else if r.launchSpec.RestartPolicy == spec.OnFailure && code != 0 {
log.Println("restarting task on failure")
} else {
break
}
}

return nil
Expand Down

0 comments on commit ab197f4

Please sign in to comment.