Skip to content

iamduo/go-workq

Repository files navigation

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.