2021-05-06 14:54:10 -07:00
|
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-02-02 17:15:17 -08:00
|
|
|
|
|
2021-05-06 14:54:10 -07:00
|
|
|
|
package scheduler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/boundary/internal/errors"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Job defines an interface for jobs that can be invoked by the scheduler.
|
|
|
|
|
|
type Job interface {
|
|
|
|
|
|
// Status reports the job’s current status. The status is periodically persisted by
|
|
|
|
|
|
// the scheduler when a job is running, and will be used to verify a job is making progress.
|
|
|
|
|
|
Status() JobStatus
|
|
|
|
|
|
|
|
|
|
|
|
// Run performs the required work depending on the implementation.
|
|
|
|
|
|
// The context is used to notify the job that it should exit early.
|
|
|
|
|
|
Run(ctx context.Context) error
|
|
|
|
|
|
|
|
|
|
|
|
// NextRunIn returns the duration until the next job run should be scheduled. This
|
|
|
|
|
|
// method is invoked after a run has successfully completed and the next run time
|
2021-06-11 11:36:14 -07:00
|
|
|
|
// is being persisted by the scheduler. If an error is returned, the error will be logged
|
|
|
|
|
|
// but the duration returned will still be used in scheduling. If a zero duration is returned
|
|
|
|
|
|
// the job will be scheduled to run again immediately.
|
2022-01-05 14:38:38 -05:00
|
|
|
|
NextRunIn(context.Context) (time.Duration, error)
|
2021-05-06 14:54:10 -07:00
|
|
|
|
|
2021-06-11 11:36:14 -07:00
|
|
|
|
// Name is the unique name of the job.
|
2021-05-06 14:54:10 -07:00
|
|
|
|
Name() string
|
|
|
|
|
|
|
|
|
|
|
|
// Description is the human readable description of the job.
|
|
|
|
|
|
Description() string
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// JobStatus defines the struct that must be returned by the Job.Status() method.
|
|
|
|
|
|
type JobStatus struct {
|
|
|
|
|
|
// Completed and Total are used to indicate job progress,
|
|
|
|
|
|
// each job implementation will determine the definition of
|
|
|
|
|
|
// progress by calculating both Completed and Total.
|
|
|
|
|
|
Completed, Total int
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-06 17:09:08 -04:00
|
|
|
|
func validateJob(ctx context.Context, j Job) error {
|
2021-05-06 14:54:10 -07:00
|
|
|
|
const op = "scheduler.validateJob"
|
|
|
|
|
|
if j == nil {
|
2023-06-06 17:09:08 -04:00
|
|
|
|
return errors.New(ctx, errors.InvalidParameter, op, "missing job")
|
2021-05-06 14:54:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
if j.Name() == "" {
|
2023-06-06 17:09:08 -04:00
|
|
|
|
return errors.New(ctx, errors.InvalidParameter, op, "missing name")
|
2021-05-06 14:54:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
if j.Description() == "" {
|
2023-06-06 17:09:08 -04:00
|
|
|
|
return errors.New(ctx, errors.InvalidParameter, op, "missing description")
|
2021-05-06 14:54:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|