Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task pause schedule (CE part) #20476

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions api/task_sched.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package api

type TaskSchedule struct {
Cron *TaskScheduleCron `hcl:"cron,block"`
}

type TaskScheduleCron struct {
Start string `hcl:"start,optional"`
Stop string `hcl:"stop,optional"`
Timezone string `hcl:"timezone,optional"`
}
2 changes: 2 additions & 0 deletions api/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,8 @@ type Task struct {
Identities []*WorkloadIdentity `hcl:"identity,block"`

Actions []*Action `hcl:"action,block"`

Schedule *TaskSchedule `hcl:"schedule,block"`
}

func (t *Task) Canonicalize(tg *TaskGroup, job *Job) {
Expand Down
20 changes: 20 additions & 0 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,11 @@ func ApiTaskToStructsTask(job *structs.Job, group *structs.TaskGroup,
act := ApiActionToStructsAction(job, action)
structsTask.Actions = append(structsTask.Actions, act)
}

if apiTask.Schedule != nil {
sched := apiScheduleToStructsSchedule(apiTask.Schedule)
structsTask.Schedule = sched
}
}

// apiWaitConfigToStructsWaitConfig is a copy and type conversion between the API
Expand Down Expand Up @@ -1475,6 +1480,21 @@ func ApiActionToStructsAction(job *structs.Job, action *api.Action) *structs.Act
}
}

func apiScheduleToStructsSchedule(s *api.TaskSchedule) *structs.TaskSchedule {
if s.Cron == nil {
// Since cron is the only field, drop the whole schedule block if its empty
return nil
}

return &structs.TaskSchedule{
Cron: &structs.TaskScheduleCron{
Start: s.Cron.Start,
Stop: s.Cron.Stop,
Timezone: s.Cron.Timezone,
},
}
}

func ApiResourcesToStructs(in *api.Resources) *structs.Resources {
if in == nil {
return nil
Expand Down
1 change: 1 addition & 0 deletions nomad/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func NewJobEndpoints(s *Server, ctx *RPCContext) *Job {
&jobValidate{srv: s},
&memoryOversubscriptionValidate{srv: s},
jobNumaHook{},
jobSchedHook{},
},
}
}
Expand Down
13 changes: 13 additions & 0 deletions nomad/job_endpoint_hook_sched.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package nomad

// jobSchedHook implements a job Validating admission controller.
//
// The implementation of Validate are in the _ce/_ent files.
type jobSchedHook struct{}

func (jobSchedHook) Name() string {
return "schedule"
}
23 changes: 23 additions & 0 deletions nomad/job_endpoint_hook_sched_ce.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

//go:build !ent

package nomad

import (
"errors"

"github.com/hashicorp/nomad/nomad/structs"
)

func (jobSchedHook) Validate(job *structs.Job) ([]error, error) {
for _, tg := range job.TaskGroups {
for _, task := range tg.Tasks {
if task.Schedule != nil {
return nil, errors.New("task schedules requires Nomad Enterprise")
}
}
}
return nil, nil
}
11 changes: 11 additions & 0 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7700,6 +7700,9 @@ type Task struct {

// Alloc-exec-like runnable commands
Actions []*Action

// Schedule for pausing tasks. Enterprise only.
Schedule *TaskSchedule
}

func (t *Task) UsesCores() bool {
Expand Down Expand Up @@ -8853,6 +8856,10 @@ type TaskState struct {
// Experimental - TaskHandle is based on drivers.TaskHandle and used
// by remote task drivers to migrate task handles between allocations.
TaskHandle *TaskHandle

// Enterprise Only - Paused is set to the paused state of the task. See
// task_sched.go
Paused TaskScheduleState
}

// NewTaskState returns a TaskState initialized in the Pending state.
Expand Down Expand Up @@ -9031,6 +9038,10 @@ const (
// TaskSkippingShutdownDelay indicates that the task operation was
// configured to ignore the shutdown delay value set for the tas.
TaskSkippingShutdownDelay = "Skipping shutdown delay"

// TaskRunning indicates a task is running due to a schedule or schedule
// override. Enterprise only.
TaskRunning = "Running"
)

// TaskEvent is an event that effects the state of a task and contains meta-data
Expand Down
53 changes: 53 additions & 0 deletions nomad/structs/task_sched.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package structs

type TaskScheduleState string

func (t TaskScheduleState) Stop() bool {
switch t {
case TaskScheduleStateForcePause:
return true
case TaskScheduleStateSchedPause:
return true
}

return false
}

func (t TaskScheduleState) Event() *TaskEvent {
switch t {
case TaskScheduleStateForcePause:
return NewTaskEvent(TaskKilling).
SetDisplayMessage("Pausing due to override")
case TaskScheduleStateSchedPause:
return NewTaskEvent(TaskKilling).
SetDisplayMessage("Pausing due to schedule")
case TaskScheduleStateForceRun:
return NewTaskEvent(TaskRunning).
SetDisplayMessage("Running due to override")
case TaskScheduleStateRun:
return NewTaskEvent(TaskRunning).
SetDisplayMessage("Running due to schedule")
}

return nil
}

const (
TaskScheduleStateRun TaskScheduleState = ""
TaskScheduleStateForceRun TaskScheduleState = "force_run"
TaskScheduleStateSchedPause TaskScheduleState = "scheduled_pause"
TaskScheduleStateForcePause TaskScheduleState = "force_pause"
)

type TaskSchedule struct {
Cron *TaskScheduleCron
}

type TaskScheduleCron struct {
Start string
Stop string
Timezone string
}
Loading