jobs

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmpty is returned when the job-server has no more dates available.
	ErrEmpty = errors.New("no dates available")
	// ErrWait is returned when the job-server may have more dates in the future.
	ErrWait = errors.New("more dates may become available")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	Server *url.URL
	*http.Client
}

Client manages requests to the job-server.

func NewClient

func NewClient(server *url.URL, client *http.Client) *Client

NewClient creates a new job client.

func (*Client) Complete

func (c *Client) Complete(ctx context.Context, date string) error

Complete accepts a previously leased date and marks it as complete.

func (*Client) Lease

func (c *Client) Lease(ctx context.Context) (string, error)

Lease attempts to claim a new job and returns the leased date if successful. Lease may also return ErrWait, if more jobs may be available in the future; clients should try again after a delay. Lease may also return ErrEmpty if no more jobs are available.

func (*Client) Update

func (c *Client) Update(ctx context.Context, date string) error

Update accepts a previously leased date and updates the date. Dates in progress should be updated more frequently than the job-server lease timeout.

type Handler

type Handler struct {
	// JobsStateFile is the file name where the handler will periodically write the job state.
	JobsStateFile string
	// Timeout is the maximum time a leased job should remain leased without
	// updates before being returned to the pending queue.
	Timeout time.Duration
	// contains filtered or unexported fields
}

Handler is an HTTP handler for a jobs server.

func (*Handler) Complete

func (h *Handler) Complete(w http.ResponseWriter, r *http.Request)

Complete moves a leased job to the set of completed jobs. If the job is not leased, Complete returns an error.

func (*Handler) Init

func (h *Handler) Init(w http.ResponseWriter, r *http.Request)

Init populates the set of Pending jobs with all dates between the given "start" and "end" (exclusive) date parameters. These dates should use the format YYYY-MM-DD.

func (*Handler) Lease

func (h *Handler) Lease(w http.ResponseWriter, r *http.Request)

Lease takes a job from the pending queue. If no jobs are currently pending, but still leased, the Lease handler will return "Status Too Early" to signal a client to retry after waiting. If no jobs are currently pending, the Lease handler will return "Status No Content" to signal a client to stop attempting leases.

func (*Handler) Load

func (h *Handler) Load(file string) error

Load reads the content of the named file to initialize the Handler jobs.

func (*Handler) Save

func (h *Handler) Save(ctx context.Context, t *time.Ticker)

Save periodically seriaizes the collection of Jobs to Handler.JobsStateFile, and periodically checks for leased jobs that have been processing longer than Handler.Timeout and returns them to Pending.

func (*Handler) Update

func (h *Handler) Update(w http.ResponseWriter, r *http.Request)

Update sets the "Updated" timestamp of a leased job to the current time.

type Job

type Job struct {
	Date    string
	Updated time.Time
}

Job represents a date task.

type Jobs

type Jobs struct {
	// Pending is the list of dates to be processed.
	Pending Sequence
	// Leased is the set of currently leased jobs.
	Leased map[string]Job
	// Completed is the set of all completed jobs.
	Completed map[string]Job
}

Jobs represents a collection of pending, currently leased, and completed Jobs. A given date will only be in one of Pending, Leased, or Completed. A Jobs instance can be Marshalled to JSON.

type Sequence

type Sequence []string

Sequence contains a list of strings.

func (*Sequence) LPop

func (s *Sequence) LPop() string

LPop removes the first element of Sequence and returns it.

Jump to

Keyboard shortcuts

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