Support pausing jobs #104313
@ -180,16 +180,22 @@ func (sm *StateMachine) updateJobOnTaskStatusCanceled(ctx context.Context, logge
|
|||||||
return sm.JobStatusChange(ctx, job, api.JobStatusCanceled, "canceled task was last runnable task of job, canceling job")
|
return sm.JobStatusChange(ctx, job, api.JobStatusCanceled, "canceled task was last runnable task of job, canceling job")
|
||||||
}
|
}
|
||||||
|
|
||||||
David-Zhang-10 marked this conversation as resolved
Outdated
|
|||||||
|
// Deal with the special case when the job is in pause-requested status.
|
||||||
|
if job.Status != api.JobStatusPauseRequested {
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
numActive, _, err := sm.persist.CountTasksOfJobInStatus(ctx, job, api.TaskStatusActive)
|
numActive, _, err := sm.persist.CountTasksOfJobInStatus(ctx, job, api.TaskStatusActive)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
David-Zhang-10 marked this conversation as resolved
Outdated
Sybren A. Stüvel
commented
This log entry is giving the wrong information. If The same is true for other copies of this code. Don't make your code lie; in a task-failure-handling part of the code, don't make it log "all tasks completed". This log entry is giving the wrong information. If `updateJobOnTaskStatusCanceled()` is called, a task was cancelled and thus the log message cannot be correct. Same for the job status change reason "all tasks completed".
The same is true for other copies of this code. Don't make your code lie; in a task-failure-handling part of the code, don't make it log "all tasks completed".
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if numActive == 0 && job.Status == api.JobStatusPauseRequested {
|
if numActive == 0 {
|
||||||
// there is no active task, and the job is in pause-requested status, so we can pause the job
|
// there is no active task, and the job is in pause-requested status, so we can pause the job
|
||||||
logger.Info().Msg("all tasks of job are completed, job is paused")
|
logger.Info().Msg("all tasks of job are completed, job is paused")
|
||||||
return sm.JobStatusChange(ctx, job, api.JobStatusPaused, "all tasks completed")
|
return sm.JobStatusChange(ctx, job, api.JobStatusPaused, "all tasks completed")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execution should not reach here.
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -191,8 +191,6 @@ func TestTaskStatusChangeCancelSingleTask(t *testing.T) {
|
|||||||
mocks.persist.EXPECT().CountTasksOfJobInStatus(ctx, job,
|
mocks.persist.EXPECT().CountTasksOfJobInStatus(ctx, job,
|
||||||
api.TaskStatusActive, api.TaskStatusQueued, api.TaskStatusSoftFailed, api.TaskStatusPaused).
|
api.TaskStatusActive, api.TaskStatusQueued, api.TaskStatusSoftFailed, api.TaskStatusPaused).
|
||||||
Return(1, 2, nil)
|
Return(1, 2, nil)
|
||||||
mocks.persist.EXPECT().CountTasksOfJobInStatus(ctx, job,
|
|
||||||
api.TaskStatusActive).Return(0, 2, nil)
|
|
||||||
require.NoError(t, sm.TaskStatusChange(ctx, task, api.TaskStatusCanceled))
|
require.NoError(t, sm.TaskStatusChange(ctx, task, api.TaskStatusCanceled))
|
||||||
|
|
||||||
// T2: queued > cancelled --> J: cancel-requested > canceled
|
// T2: queued > cancelled --> J: cancel-requested > canceled
|
||||||
|
Loading…
Reference in New Issue
Block a user
This code block feels a bit out of place. If I understand correctly, this is the flow that this code would handle:
{queued, soft-failed, paused}
).pause-requested
.To me this doesn't look like a status in which the pausing was complete.
This logic is also repeated quite a bit below. I think it's better to make a new function responsible for answering the question "is the job pausing, and is that process complete now?", and call that function from the places that need this.
Finally, some reordering can be done to optimise the code. Currently every check will count the tasks in the job, even when
job.Status != api.JobStatusPauseRequested
. It's good practice to do the cheapest check first, and the most expensive check last.The comment makes sense to me! The only thing I am confused about is what you meant by "the pausing was complete".
"Pausing is complete" for me means that the work that should be done when the job goes to
pause-requested
is done, and it can be moved to statepaused
.So for the pausing to be complete, we need all tasks to be in
paused
state, including tasks that are runnable but not inactive
state? In other words, instead of checking the number of active tasks here, we should check if the job has any tasks other thanpaused
?