task

package
v0.0.0-...-f6ab670 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: Apache-2.0 Imports: 6 Imported by: 193

Documentation

Overview

Package task contains a default implementation of the interfaces in the task package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CancelFlag

type CancelFlag interface {
	// Canceled returns true if a cancel or Shutdown has been requested, false otherwise.
	// This method should be called periodically in the job.
	Canceled() bool

	// Set sets the state of this flag and wakes up waiting callers.
	Set(state State)

	// ShutDown returns true if a ShutDown has been requested, false otherwise.
	// This method should be called periodically in the job.
	ShutDown() bool

	// State returns the current flag state
	State() State

	// Wait blocks the caller until either a cancel has been requested or the
	// task has completed normally. Returns Canceled if cancel has been requested,
	// or Completed if the task completed normally.
	// This is intended to be used to wake up a job that may be waiting on some resources, as follows:
	// The main job starts a go routine that calls Wait. The main job then does its processing.
	// During processing the job may be waiting on certain events/conditions.
	// In the go routine, once Wait returns, if the return value indicates that a cancel
	// request has been received, the go routine wakes up the running job.
	Wait() (state State)
}

CancelFlag is an object that is passed to any job submitted to a task in order to communicated job cancellation. Job cancellation has to be cooperative.

type ChanneledCancelFlag

type ChanneledCancelFlag struct {
	// contains filtered or unexported fields
}

ChanneledCancelFlag is a default implementation of the task.CancelFlag interface.

func NewChanneledCancelFlag

func NewChanneledCancelFlag() *ChanneledCancelFlag

NewChanneledCancelFlag creates a new instance of ChanneledCancelFlag.

func (*ChanneledCancelFlag) Canceled

func (t *ChanneledCancelFlag) Canceled() bool

Canceled returns true if this flag has been set to Cancel state, false otherwise.

func (*ChanneledCancelFlag) Set

func (t *ChanneledCancelFlag) Set(state State)

Set sets the state of this flag and wakes up waiting callers.

func (*ChanneledCancelFlag) ShutDown

func (t *ChanneledCancelFlag) ShutDown() bool

ShutDown returns true if this flag has been set to ShutDown state, false otherwise.

func (*ChanneledCancelFlag) State

func (t *ChanneledCancelFlag) State() State

State returns the current flag state.

func (*ChanneledCancelFlag) Wait

func (t *ChanneledCancelFlag) Wait() (state State)

Wait blocks until the flag is set to either Cancel or Completed state. Returns the state.

type Job

type Job func(CancelFlag)

Job is a function that receives a cancel flag through which it can be canceled.

type JobStore

type JobStore struct {
	// contains filtered or unexported fields
}

JobStore is a collection of jobs.

func NewJobStore

func NewJobStore() *JobStore

NewJobStore creates a new task with no jobs.

func (*JobStore) AddJob

func (t *JobStore) AddJob(jobID string, token *JobToken) error

AddJob adds a job to this task. Returns error if the job already exists.

func (*JobStore) DeleteAllJobs

func (t *JobStore) DeleteAllJobs() map[string]*JobToken

DeleteAllJobs deletes all the jobs of this task. Returns the deleted jobs.

func (*JobStore) DeleteJob

func (t *JobStore) DeleteJob(jobID string)

DeleteJob deletes the job with the given jobID.

func (*JobStore) GetJob

func (t *JobStore) GetJob(jobID string) (token *JobToken, found bool)

GetJob retrieves a job from a task.

type JobToken

type JobToken struct {
	// contains filtered or unexported fields
}

JobToken embeds a job and its associated info

type Pool

type Pool interface {
	// Submit schedules a job to be executed in the associated worker pool.
	// Returns an error if a job with the same name already exists.
	Submit(log log.T, jobID string, job Job) error

	// Cancel cancels the given job. Jobs that have not started yet will never be started.
	// Jobs that are running will have their CancelFlag set to the Canceled state.
	// It is the responsibility of the job to terminate within a reasonable time.
	// If the job fails to terminate after a Cancel, the job may be abandoned.
	// Returns true if the job has been found and canceled, false if the job was not found.
	Cancel(jobID string) bool

	// Shutdown cancels all the jobs and shuts down the workers.
	Shutdown()

	// ShutdownAndWait calls Shutdown then waits until all the workers have exited
	// or until the timeout has elapsed, whichever comes first. Returns true if all
	// workers terminated before the timeout or false if the timeout expired.
	ShutdownAndWait(timeout time.Duration) (finished bool)

	// HasJob returns if jobStore has specified job
	HasJob(jobID string) bool

	// BufferTokensIssued returns the current buffer token size
	BufferTokensIssued() int

	// AcquireBufferToken acquires the buffer token based on job id
	AcquireBufferToken(jobId string) PoolErrorCode

	// ReleaseBufferToken releases the acquired token
	ReleaseBufferToken(jobId string) PoolErrorCode
}

Pool is a pool of jobs.

func NewPool

func NewPool(log log.T, maxParallel int, bufferLimit int, cancelWaitDuration time.Duration, clock times.Clock) Pool

NewPool creates a new task pool and launches maxParallel workers. The cancelWaitDuration parameter defines how long to wait for a job to complete a cancellation request.

type PoolErrorCode

type PoolErrorCode string
var (
	// DuplicateCommand represents duplicate command in the buffer
	DuplicateCommand PoolErrorCode = "DuplicateCommand"

	// InvalidJobId represents invalid job Id
	InvalidJobId PoolErrorCode = "InvalidJobId"

	// UninitializedBuffer represents that the buffer has not been initialized in the pool
	UninitializedBuffer PoolErrorCode = "UninitializedBuffer"

	// JobQueueFull represents that the job queue buffer is full
	JobQueueFull PoolErrorCode = "JobQueueFull"
)

type State

type State int

State represents the state of a job.

const (
	// Canceled indicates a job for which cancellation has been requested.
	Canceled State = 1

	// Completed indicates a completed job.
	Completed State = 2

	// ShutDown indicates a job for which ShutDown has been requested.
	ShutDown State = 3
)

Jump to

Keyboard shortcuts

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