Skip to content

Commit

Permalink
Change build exit check
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentaTomas committed Sep 29, 2023
1 parent d2d984a commit 82eb111
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 8 additions & 0 deletions packages/env-build-task-driver/internal/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type taskHandle struct {

exited chan struct{}

ctx context.Context
cancel context.CancelFunc

mu sync.RWMutex
Expand All @@ -47,6 +48,13 @@ func (h *taskHandle) TaskStatus() *drivers.TaskStatus {
}
}

func (h *taskHandle) IsRunning() bool {
h.mu.RLock()
defer h.mu.RUnlock()

return h.procState == drivers.TaskStateRunning
}

func (h *taskHandle) run(ctx context.Context, tracer trace.Tracer, docker *client.Client) {
childCtx, childSpan := tracer.Start(ctx, "run")
defer childSpan.End()
Expand Down
10 changes: 6 additions & 4 deletions packages/env-build-task-driver/internal/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
env: &env,
exited: make(chan struct{}),
cancel: cancel,
ctx: cancellableBuildContext,
}

driverState := TaskState{
Expand Down Expand Up @@ -156,16 +157,13 @@ func (d *Driver) WaitTask(ctx context.Context, taskID string) (<-chan *drivers.E
func (d *Driver) handleWait(ctx context.Context, handle *taskHandle, ch chan *drivers.ExitResult) {
defer close(ch)

ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return
case <-d.ctx.Done():
return
case <-ticker.C:
case <-handle.ctx.Done():
s := handle.TaskStatus()
if s.State == drivers.TaskStateExited {
ch <- handle.exitResult
Expand All @@ -191,6 +189,10 @@ func (d *Driver) DestroyTask(taskID string, force bool) error {
return drivers.ErrTaskNotFound
}

if handle.IsRunning() && !force {
return errors.New("task is still running")
}

handle.cancel()
d.tasks.Delete(taskID)

Expand Down

0 comments on commit 82eb111

Please sign in to comment.