workq

package module
Version: v0.0.0-...-b301b13 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2017 License: MPL-2.0 Imports: 9 Imported by: 0

README

go-workq Build Status Coverage Status GitHub Logo

Go client for Workq.

Table of Contents

Connection Management

Connecting
client, err := workq.Connect("localhost:9922")
if err != nil {
  // ...
}
Closing active connection
err := client.Close()
if err != nil {
  // ...
}

Commands Protocol Doc GoDoc

Client Commands
Add

Protocol Doc | Go Doc

Add a background job. The result can be retrieved through the "result" command.

job := &workq.BgJob{
	ID: "61a444a0-6128-41c0-8078-cc757d3bd2d8",
	Name: "ping",
	TTR: 5000,       // 5 second time-to-run limit
    TTL: 60000,      // Expire after 60 seconds
	Payload: []byte("Ping!"),
	Priority: 10,    // @OPTIONAL Numeric priority, default 0.
	MaxAttempts: 3,  // @OPTIONAL Absolute max num of attempts.
	MaxFails: 1,     // @OPTIONAL Absolute max number of failures.
}
err := client.Add(job)
if err != nil {
	// ...
}
Run

Protocol Doc | Go Doc

Run a job and wait for its result.

job := &workq.FgJob{
	ID: "61a444a0-6128-41c0-8078-cc757d3bd2d8",
	Name: "ping",
	TTR: 5000,          // 5 second time-to-run limit
	Timeout: 60000, // Wait up to 60 seconds for a worker to pick up.
	Payload: []byte("Ping!"),
	Priority: 10,       // @OPTIONAL Numeric priority, default 0.
}
result, err := client.Run(job)
if err != nil {
  // ...
}

fmt.Printf("Success: %t, Result: %s", result.Success, result.Result)
Schedule

Protocol Doc | Go Doc

Schedule a job at a UTC time. The result can be retrieved through the "result" command.

job := &workq.ScheduledJob{
	ID: "61a444a0-6128-41c0-8078-cc757d3bd2d8",
	Name: "ping",
	Time:    "2016-12-01T00:00:00Z", // Start job at this UTC time.
	TTL: 60000,                      // Expire after 60 seconds
	TTR: 5000,                       // 5 second time-to-run limit
	Payload: []byte("Ping!"),
	Priority: 10,                    // @OPTIONAL Numeric priority, default 0.
    MaxAttempts: 3,                  // @OPTIONAL Absolute max num of attempts.
    MaxFails: 1,                     // @OPTIONAL Absolute max number of failures.
}
err := client.Schedule(job)
if err != nil {
	// ...
}
Result

Protocol Doc | Go Doc

Get a job result previously executed by Add or Schedule commands.

// Get a job result, waiting up to 60 seconds if the job is still executing.
result, err := client.Result("61a444a0-6128-41c0-8078-cc757d3bd2d8", 60000)
if err != nil {
	// ...
}

fmt.Printf("Success: %t, Result: %s", result.Success, result.Result)
Worker Commands
Lease

Protocol Doc | Go Doc

Lease a job within a set of one or more names with a wait-timeout (milliseconds).

// Lease the first available job in "ping1", "ping2", "ping3"
// waiting up to 60 seconds.
job, err := client.Lease([]string{"ping1", "ping2", "ping3"}, 60000)
if err != nil {
	// ...
}

fmt.Printf("Leased Job: ID: %s, Name: %s, Payload: %s", job.ID, job.Name, job.Payload)
Complete

Protocol Doc | Go Doc

Mark a job successfully completed with a result.

err := client.Complete("61a444a0-6128-41c0-8078-cc757d3bd2d8", []byte("Pong!"))
if err != nil {
	// ...
}
Fail

Protocol Doc | Go Doc

Mark a job failed with a result.

err := client.Fail("61a444a0-6128-41c0-8078-cc757d3bd2d8", []byte("Failed-Pong!"))
if err != nil {
	// ...
}
Adminstrative Commands
Delete

Protocol Doc | Go Doc

err := client.Delete("61a444a0-6128-41c0-8078-cc757d3bd2d8")
if err != nil {
	// ...
}
Inspect

Protocol Doc

Inspect commands not yet supported yet.

Documentation

Overview

Package workq implements Workq protocol commands: https://github.com/iamduo/workq/blob/master/doc/protocol.md#commands

Index

Constants

View Source
const (

	// Time format for any date times. Compatible with time.Format.
	TimeFormat = "2006-01-02T15:04:05Z"
)

Variables

View Source
var (
	// ErrMalformed is returned when responses from workq can not be parsed
	// due to unrecognized responses.
	ErrMalformed = errors.New("Malformed response")
)

Functions

func NewNetError

func NewNetError(text string) error

func NewResponseError

func NewResponseError(code string, text string) error

Types

type BgJob

type BgJob struct {
	ID          string
	Name        string
	TTR         int // Time-to-run
	TTL         int // Time-to-live
	Payload     []byte
	Priority    int // Numeric priority
	MaxAttempts int // Absoulute max num of attempts.
	MaxFails    int // Absolute max number of failures.
}

BgJob is executed by the "add" command. Describes a background job specification.

type Client

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

Client represents a single connection to Workq.

func Connect

func Connect(addr string) (*Client, error)

Connect to a Workq server returning a Client

func NewClient

func NewClient(conn net.Conn) *Client

NewClient returns a Client from a net.Conn.

func (*Client) Add

func (c *Client) Add(j *BgJob) error

"add" command: https://github.com/iamduo/workq/blob/master/doc/protocol.md#add

Add background job Returns ResponseError for Workq response errors. Returns NetError on any network errors. Returns ErrMalformed if response can't be parsed.

func (*Client) Close

func (c *Client) Close() error

Close client connection.

func (*Client) Complete

func (c *Client) Complete(id string, result []byte) error

"complete" command: https://github.com/iamduo/workq/blob/master/doc/protocol.md#complete

Mark job successfully complete, @see PROTOCOL_DOC Returns ResponseError for Workq response errors. Returns NetError on any network errors. Returns ErrMalformed if response can't be parsed.

func (*Client) Delete

func (c *Client) Delete(id string) error

"delete" command: https://github.com/iamduo/workq/blob/master/doc/protocol.md#delete

Delete job. Returns ResponseError for Workq response errors. Returns NetError on any network errors. Returns ErrMalformed if response can't be parsed.

func (*Client) Fail

func (c *Client) Fail(id string, result []byte) error

"fail" command: https://github.com/iamduo/workq/blob/master/doc/protocol.md#fail

Mark job as failure. Returns ResponseError for Workq response errors. Returns NetError on any network errors. Returns ErrMalformed if response can't be parsed.

func (*Client) Lease

func (c *Client) Lease(names []string, timeout int) (*LeasedJob, error)

"lease" command: https://github.com/iamduo/workq/blob/master/doc/protocol.md#lease

Lease a job, waiting for available jobs until timeout, @see PROTOCOL_DOC Returns ResponseError for Workq response errors. Returns NetError on any network errors. Returns ErrMalformed if response can't be parsed.

func (*Client) Result

func (c *Client) Result(id string, timeout int) (*JobResult, error)

"result" command: https://github.com/iamduo/workq/blob/master/doc/protocol.md#result

Fetch job result, @see PROTOCOL_DOC Returns ResponseError for Workq response errors. Returns NetError on any network errors. Returns ErrMalformed if response can't be parsed.

func (*Client) Run

func (c *Client) Run(j *FgJob) (*JobResult, error)

"run" command: https://github.com/iamduo/workq/blob/master/doc/protocol.md#run

Submit foreground job and wait for result. Returns ResponseError for Workq response errors Returns NetError on any network errors. Returns ErrMalformed if response can't be parsed.

func (*Client) Schedule

func (c *Client) Schedule(j *ScheduledJob) error

"schedule" command: https://github.com/iamduo/workq/blob/master/doc/protocol.md#schedule

Schedule job at future UTC time. Returns ResponseError for Workq response errors. Returns NetError on any network errors. Returns ErrMalformed if response can't be parsed.

type FgJob

type FgJob struct {
	ID       string
	Name     string
	TTR      int
	Timeout  int // Milliseconds to wait for job completion.
	Payload  []byte
	Priority int // Numeric priority
}

FgJob is executed by the "run" command. Describes a foreground job specification.

type JobResult

type JobResult struct {
	Success bool
	Result  []byte
}

JobResult is returned by the "run" & "result" commands.

type LeasedJob

type LeasedJob struct {
	ID      string
	Name    string
	TTR     int
	Payload []byte
}

LeasedJob is returned by the "lease" command.

type NetError

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

func (*NetError) Error

func (e *NetError) Error() string

type ResponseError

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

func (*ResponseError) Code

func (e *ResponseError) Code() string

func (*ResponseError) Error

func (e *ResponseError) Error() string

func (*ResponseError) Text

func (e *ResponseError) Text() string

type ScheduledJob

type ScheduledJob struct {
	ID          string
	Name        string
	TTR         int
	TTL         int
	Payload     []byte
	Time        string
	Priority    int // Numeric priority
	MaxAttempts int // Absoulute max num of attempts.
	MaxFails    int // Absolute max number of failures.
}

ScheduledJob is executed by the "schedule" command. Describes a scheduled job specification.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL