job

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package job is the endpoint group for the job queue: the jobs the controller sent to the agents.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is a job that is not there.
	ErrNotFound = errors.New("job not found")
	// ErrNoOperation is a job with no operation to run again.
	ErrNoOperation = errors.New("job has no operation data")
	// ErrOutOfScope is a retry to a target outside the token's target scope.
	ErrOutOfScope = errors.New("target is outside the token's target scope")
)
View Source
var Group = endpoint.Register(endpoint.Define(
	"job", "Manage the jobs in the queue",
	endpoint.Area(area.Fleet),
	endpoint.Title("Job Management API"),
	endpoint.Tag("job_operations", "Job", "Operations related to the job queue."),
	endpoint.Uses[Deps](),
	endpoint.SubDoc("deadletter", "Read the jobs whose delivery to an agent used up its retries"),
	endpoint.Doc[types.JobStatus](
		"The status of a job, which the controller computes from what each agent did: "+
			"submitted, processing, completed, failed, partial_failure (completed on some "+
			"agents and failed on others) or skipped (each agent skipped it).",
	),
	endpoint.Doc[types.AgentJobStatus](
		"The status of a job on one host. The controller records submitted and retried; "+
			"an agent records acknowledged, started, completed, failed or skipped; and timeout "+
			"is a host whose agent did not answer in time.",
	),
	endpoint.Example[ListJobsResponse]("status_counts",
		`{"submitted":5,"processing":2,"completed":30,"failed":3,"partial_failure":2}`),
	endpoint.Get[ListInput, ListJobsResponse]("list").
		Fails("a status that is not a job status", ListInput{Status: "lost"}).
		Fails("a limit below 1", ListInput{Limit: -1}).
		Fails("a limit over 100", ListInput{Limit: 101}).
		Fails("a negative offset", ListInput{Offset: -1}).
		Handle(List).
		ID("GetJobs").
		Describe("List jobs").
		Explain("Retrieve jobs, optionally filtered by status. A token with a target scope gets "+
			"only the jobs whose target is in its scope, and the counts include only those jobs.").
		Returns("A list of jobs.").
		Command("List jobs", "").
		Example("").
		Prints(sampleList).
		Example("--status completed --limit 3"),
	endpoint.Get[DeadLetterListInput, DeadLetterList]("deadletters").
		Fails("a limit below 1", DeadLetterListInput{Limit: -1}).
		Fails("a limit over 100", DeadLetterListInput{Limit: 101}).
		Handle(DeadLetters).
		Sub("deadletter").
		Use("list").
		ID("ListDeadLetters").
		Describe("List the dead letters").
		Explain("Lists the newest jobs whose delivery to an agent used up its retries, from "+
			"the dead letter stream, each with its job's operation, target and error. A job "+
			"goes back to the queue with retry. A token with a target scope gets only the "+
			"dead letters of the jobs whose target is in its scope.").
		Returns("The newest dead letters.").
		Command("List the dead letters",
			"Lists the jobs whose delivery to an agent used up its retries, the newest\n"+
				"first. Send one back to the queue with osapi client job retry --id.").
		Example("").
		Prints(sampleDeadLetters).
		Example("--limit 5"),
	endpoint.Get[GetInput, DetailResponse]("get").
		Fails("an ID that is not a UUID", GetInput{ID: "not-a-uuid"}).
		Handle(Get).
		ID("GetJobByID").
		Describe("Get job detail").
		Explain("Retrieve details of a specific job by its ID. A job whose target is outside the "+
			"target scope of the token is not found.").
		Returns("The job details.").
		Command("Get job details and status", "").
		Example("--id 550e8400-e29b-41d4-a716-446655440000").
		Prints(sampleDetail).
		Errors(endpoint.NotFound(ErrNotFound,
			"Job not found, or its target is outside the token's target scope.")),
	endpoint.Delete[DeleteInput, struct{}]("delete").
		Fails("an ID that is not a UUID", DeleteInput{ID: "not-a-uuid"}).
		Handle(Delete).
		ID("DeleteJobByID").
		Describe("Delete a job").
		Explain("Delete a specific job by its ID. A job whose target is outside the target "+
			"scope of the token is not found.").
		Returns("Job deleted successfully. No content returned.").
		Status(http.StatusNoContent).
		Command("Delete a job", "The job goes from storage permanently.").
		Example("--id 550e8400-e29b-41d4-a716-446655440000").
		CheckFails("client job get", "--id 550e8400-e29b-41d4-a716-446655440000", "not found").
		Errors(endpoint.NotFound(ErrNotFound,
			"Job not found, or its target is outside the token's target scope.")),
	endpoint.Post[RetryInput, CreateJobResponse]("retry").
		Fails("an ID that is not a UUID", RetryInput{ID: "not-a-uuid"}).
		Fails("a target that is not a hostname, a label or _any or _all", RetryInput{ID: "7c9e6679-7425-40de-944b-e07fc1f90ae7", Body: RetryJobRequest{TargetHostname: "web 01!"}}).
		Handle(Retry).
		Action("retry").
		ID("RetryJobByID").
		Describe("Retry a job").
		Explain("Create a new job using the same operation data as an existing job. "+
			"The original job is preserved. Returns the new job ID.").
		Returns("The retry job was created successfully.").
		Status(http.StatusCreated).
		Location("job_id").
		Command("Retry a failed or stuck job",
			"The new job has the same operation as the old job. The original job stays in\n"+
				"the history. Its timeline gets a `retried` event that links to the new job.").
		Example("--id 550e8400-e29b-41d4-a716-446655440000").
		Prints(sampleRetry).
		Check("client job get", "--id {out.job_id}", "id", "{out.job_id}").
		Errors(
			endpoint.NotFound(ErrNotFound,
				"Job not found, or its target is outside the token's target scope."),
			endpoint.Invalid(ErrNoOperation,
				"Invalid request parameters, or a job with no operation data to run again."),
			endpoint.Forbidden(ErrOutOfScope,
				"Insufficient permissions, or a target outside the token's target scope."),
		),
))

Group is the job group's definition.

Functions

func Delete

func Delete(
	ctx context.Context,
	d Deps,
	in DeleteInput,
) (struct{}, error)

Delete removes a job from the queue.

Types

type AgentState

type AgentState = struct {
	Hostname string               `json:"hostname"`
	Status   types.AgentJobStatus `json:"status,omitempty"   render:"status"   doc:"How far the agent got."`
	Duration string               `json:"duration,omitempty" render:"duration"`
	Error    string               `json:"error,omitempty"`
}

AgentState is how far one agent got, in place in the spec.

type CreateJobResponse

type CreateJobResponse struct {
	JobID     string          `` /* 134-byte string literal not displayed */
	Status    types.JobStatus `json:"status"                              doc:"Initial status of the job."             example:"submitted"`
	Revision  int64           `json:"revision,omitempty"                  doc:"The KV revision number."                example:"1"`
	Timestamp string          `` /* 145-byte string literal not displayed */
}

CreateJobResponse is the job that a request created.

func Retry

func Retry(
	ctx context.Context,
	d Deps,
	in RetryInput,
) (CreateJobResponse, error)

Retry sends a new job with the operation of an old one. The old job stays.

type DeadLetter

type DeadLetter struct {
	Seq        uint64     `` /* 128-byte string literal not displayed */
	Time       time.Time  `json:"time"                doc:"When NATS gave up on the delivery."`
	JobID      string     `` /* 161-byte string literal not displayed */
	Operation  *Operation `` /* 171-byte string literal not displayed */
	Target     string     `` /* 129-byte string literal not displayed */
	Error      string     `json:"error,omitempty"     doc:"The error of the job, when it has one."`
	Deliveries uint64     `` /* 126-byte string literal not displayed */
	Consumer   string     `` /* 171-byte string literal not displayed */
}

DeadLetter is a job whose delivery to an agent used up its retries.

type DeadLetterList

type DeadLetterList struct {
	TotalItems int          `                     json:"total_items" doc:"The number of dead letters that the stream holds."`
	Items      []DeadLetter `                     json:"items"`
	// contains filtered or unexported fields
}

DeadLetterList is the newest dead letters, with the count of them all.

func DeadLetters

func DeadLetters(
	ctx context.Context,
	d Deps,
	in DeadLetterListInput,
) (DeadLetterList, error)

DeadLetters is the newest dead letters, each with its job. A scoped token sees only the dead letters of the jobs whose target is in its scope, and the total counts only those.

type DeadLetterListInput

type DeadLetterListInput struct {
	Limit int `` /* 134-byte string literal not displayed */
}

DeadLetterListInput limits the dead letters.

type DeleteInput

type DeleteInput struct {
	ID string `json:"id" in:"path" validate:"uuid" doc:"UUID of the job to delete."`
}

DeleteInput names the job to delete.

type Deps

type Deps struct {
	// Jobs reads, deletes and retries the jobs in the queue.
	Jobs Jobs
}

Deps is what the job group's handlers use.

type DetailResponse

type DetailResponse struct {
	ID          string          `` /* 227-byte string literal not displayed */
	Status      types.JobStatus `` /* 244-byte string literal not displayed */
	Created     string          `` /* 242-byte string literal not displayed */
	Target      string          `` /* 204-byte string literal not displayed */
	Operation   *Operation      `json:"operation,omitempty"                    doc:"What the job asked for, with each secret masked."`
	Hostname    string          `` /* 254-byte string literal not displayed */
	UpdatedAt   string          `` /* 254-byte string literal not displayed */
	Changed     *bool           `` /* 254-byte string literal not displayed */
	Error       string          `` /* 254-byte string literal not displayed */
	Result      any             `` /* 254-byte string literal not displayed */
	Responses   []Response      `` /* 254-byte string literal not displayed */
	Chain       []string        `` /* 214-byte string literal not displayed */
	AgentStates []AgentState    `` /* 254-byte string literal not displayed */
	Timeline    []TimelineEvent `` /* 254-byte string literal not displayed */
	// contains filtered or unexported fields
}

DetailResponse is one job, with what each agent did with it. The fields of a list come first, in the order of its columns.

func Get

func Get(
	ctx context.Context,
	d Deps,
	in GetInput,
) (DetailResponse, error)

Get is one job, with what each agent did with it.

type GetInput

type GetInput struct {
	ID string `json:"id" in:"path" validate:"uuid" doc:"UUID of the job to retrieve."`
}

GetInput names the job to read.

type Jobs

type Jobs interface {
	ListJobs(
		ctx context.Context,
		statusFilter types.JobStatus,
		limit int,
		offset int,
		keep func(target string) bool,
	) (*client.ListJobsResult, error)
	GetJobStatus(ctx context.Context, jobID string) (*job.QueuedJob, error)
	DeleteJob(ctx context.Context, jobID string) error
	ListDeadLetters(ctx context.Context, limit int) ([]job.DeadLetter, int, error)
	RetryJob(
		ctx context.Context,
		jobID string,
		targetHostname string,
	) (*client.CreateJobResult, error)
}

Jobs is the part of the job client that the job group uses.

type ListInput

type ListInput struct {
	Status types.JobStatus `json:"status" in:"query" validate:"omitempty,enum"          doc:"Filter jobs by status."`
	Limit  int             `json:"limit"  in:"query" validate:"omitempty,min=1,max=100" doc:"Maximum number of jobs per page (1-100)." default:"10"`
	Offset int             `json:"offset" in:"query" validate:"omitempty,min=0"         doc:"Number of jobs to skip for pagination."   default:"0"`
}

ListInput is a page of the job queue, and the status to filter by.

type ListJobsResponse

type ListJobsResponse struct {
	TotalItems   int                     `` /* 160-byte string literal not displayed */
	StatusCounts map[types.JobStatus]int `` /* 147-byte string literal not displayed */
	Items        []DetailResponse        `             json:"items,omitempty"`
	// contains filtered or unexported fields
}

ListJobsResponse is a page of the job queue.

func List

func List(
	ctx context.Context,
	d Deps,
	in ListInput,
) (ListJobsResponse, error)

List is a page of the job queue, newest first, with the count of each status.

type Operation

type Operation = struct {
	Type string `json:"type"           doc:"The operation, such as node.hostname.get."`
	Data any    `json:"data,omitempty" doc:"The data of the operation, with each secret masked." table:"-" label:"Operation data"`
}

Operation is what a job asked for: its type, such as node.hostname.get, and its data, in place in the spec.

type Response

type Response = struct {
	Hostname string               `json:"hostname"`
	Status   types.AgentJobStatus `json:"status,omitempty"  render:"status"`
	Changed  *bool                `json:"changed,omitempty"`
	Error    string               `json:"error,omitempty"`
	Data     any                  `json:"data,omitempty"                    doc:"Agent result data."`
	Steps    []types.Step         `` /* 131-byte string literal not displayed */
}

Response is what one agent answered, in place in the spec.

type RetryInput

type RetryInput struct {
	ID   string          `json:"id" in:"path" validate:"uuid"      doc:"UUID of the job to retry."`
	Body RetryJobRequest `          in:"body" validate:"omitempty"`
}

RetryInput names the job to retry, and where the new job goes.

type RetryJobRequest

type RetryJobRequest struct {
	TargetHostname string `` /* 221-byte string literal not displayed */
}

RetryJobRequest is where a retried job goes.

type TimelineEvent

type TimelineEvent = struct {
	Timestamp string               `json:"timestamp,omitempty" doc:"ISO 8601 timestamp of the event."          render:"time"`
	Event     types.AgentJobStatus `json:"event,omitempty"     doc:"The status event."`
	Hostname  string               `json:"hostname,omitempty"  doc:"Agent or source that generated the event."`
	Message   string               `json:"message,omitempty"   doc:"Human-readable description of the event."`
	Error     string               `json:"error,omitempty"     doc:"Error details if applicable."`
}

TimelineEvent is one event of a job's life, in place in the spec.

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL