task

package
v0.0.0-...-a1f36d2 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2017 License: Apache-2.0 Imports: 6 Imported by: 0

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 MockCancelFlag

type MockCancelFlag struct {
	mock.Mock
}

MockCancelFlag mocks a cancel flag.

func NewMockDefault

func NewMockDefault() *MockCancelFlag

NewMockDefault returns a mocked cancel flag.

func (*MockCancelFlag) Canceled

func (flag *MockCancelFlag) Canceled() bool

Canceled mocks the method with the same name.

func (*MockCancelFlag) Set

func (flag *MockCancelFlag) Set(state State)

func (*MockCancelFlag) ShutDown

func (flag *MockCancelFlag) ShutDown() bool

ShutDown mocks the method with the same name.

func (*MockCancelFlag) State

func (flag *MockCancelFlag) State() State

func (*MockCancelFlag) Wait

func (flag *MockCancelFlag) Wait() (state State)

Wait mocks the method with the same name.

type MockedPool

type MockedPool struct {
	mock.Mock
}

MockedPool stands for a mock pool.

func (*MockedPool) Cancel

func (mockPool *MockedPool) Cancel(jobID string) bool

Cancel mocks the method with the same name.

func (*MockedPool) HasJob

func (mockPool *MockedPool) HasJob(jobID string) bool

ShutdownAndWait mocks the method with the same name.

func (*MockedPool) Shutdown

func (mockPool *MockedPool) Shutdown()

Shutdown mocks the method with the same name.

func (*MockedPool) ShutdownAndWait

func (mockPool *MockedPool) ShutdownAndWait(timeout time.Duration) (finished bool)

ShutdownAndWait mocks the method with the same name.

func (*MockedPool) Submit

func (mockPool *MockedPool) Submit(log log.T, jobID string, job Job) error

Submit mocks the method with the same name.

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
}

Pool is a pool of jobs.

func NewPool

func NewPool(log log.T, maxParallel 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 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