Skip to content

Commit

Permalink
fix waiting for k8s jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
KrKOo committed Mar 27, 2024
1 parent 568ca5e commit 363ba16
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions worker/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (kcmd KubernetesCommand) Run(ctx context.Context) error {
}

defer watcher.Stop()
waitForJobFinnish(ctx, watcher)
waitForJobFinish(ctx, watcher)

pods, err := clientset.CoreV1().Pods(kcmd.Namespace).List(ctx, metav1.ListOptions{LabelSelector: fmt.Sprintf("job-name=%s-%d", taskId, kcmd.JobId)})
if err != nil {
Expand Down Expand Up @@ -145,18 +145,21 @@ func (kcmd KubernetesCommand) Stop() error {
}

// Waits until the job finishes
func waitForJobFinnish(ctx context.Context, watcher watch.Interface) {
func waitForJobFinish(ctx context.Context, watcher watch.Interface) {
for {
select {
case event := <-watcher.ResultChan():
if event.Type == watch.Error || event.Type == watch.Deleted {
return
} else if event.Type == watch.Bookmark {
continue
}

job := event.Object.(*v1.Job)

if job.Status.Succeeded > 0 || job.Status.Failed > 0 {
return
} else if event.Type == watch.Deleted {
return
}

case <-ctx.Done():
return
}
Expand Down

0 comments on commit 363ba16

Please sign in to comment.