|
| 1 | +# go-workq [](https://travis-ci.org/iamduo/go-workq) [](https://coveralls.io/github/iamduo/go-workq?branch=master)  |
| 2 | + |
| 3 | + |
| 4 | +Go client for [Workq](https://github.com/iamduo/workq). |
| 5 | + |
| 6 | +**Table of Contents** |
| 7 | + |
| 8 | +- [Connecting](#connecting) |
| 9 | +- [Closing active connection](#closing-active-connection) |
| 10 | +- [Client Commands](#client-commands) |
| 11 | + - [Add](#add) |
| 12 | + - [Run](#run) |
| 13 | + - [Schedule](#schedule) |
| 14 | + - [Result](#result) |
| 15 | +- [Worker Commands](#worker-commands) |
| 16 | + - [Lease](#lease) |
| 17 | + - [Complete](#complete) |
| 18 | + - [Fail](#fail) |
| 19 | +- [Adminstrative Commands](#adminstrative-commands) |
| 20 | + - [Delete](#delete) |
| 21 | + - [Inspect](#inspect) |
| 22 | + |
| 23 | +## Connection Management |
| 24 | + |
| 25 | +### Connecting |
| 26 | + |
| 27 | +```go |
| 28 | +client, err := workq.Connect("localhost:9922") |
| 29 | +if err != nil { |
| 30 | + // ... |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### Closing active connection |
| 35 | + |
| 36 | +```go |
| 37 | +err := client.Close() |
| 38 | +if err != nil { |
| 39 | + // ... |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +## Commands [](https://github.com/iamduo/workq/blob/master/doc/protocol.md#commands) [](https://godoc.org/github.com/iamduo/go-workq) |
| 44 | + |
| 45 | +### Client Commands |
| 46 | + |
| 47 | +#### Add |
| 48 | + |
| 49 | +[Protocol Doc](https://github.com/iamduo/workq/blob/master/doc/protocol.md#add) | [Go Doc](https://godoc.org/github.com/iamduo/go-workq#Client.Add) |
| 50 | + |
| 51 | +Add a background job. The result can be retrieved through the ["result"](#result) command. |
| 52 | + |
| 53 | +```go |
| 54 | +job := &workq.BgJob{ |
| 55 | + ID: "6ba7b810-9dad-11d1-80b4-00c04fd430c4", |
| 56 | + Name: "ping", |
| 57 | + TTR: 5000, // 5 second time-to-run limit |
| 58 | + TTL: 60000, // Expire after 60 seconds |
| 59 | + Payload: []byte("Ping!"), |
| 60 | + Priority: 10, // @OPTIONAL Numeric priority, default 0. |
| 61 | + MaxAttempts: 3, // @OPTIONAL Absolute max num of attempts. |
| 62 | + MaxFails: 1, // @OPTIONAL Absolute max number of failures. |
| 63 | +} |
| 64 | +err := client.Add(job) |
| 65 | +if err != nil { |
| 66 | + // ... |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +#### Run |
| 71 | + |
| 72 | +[Protocol Doc](https://github.com/iamduo/workq/blob/master/doc/protocol.md#run) | [Go Doc](https://godoc.org/github.com/iamduo/go-workq#Client.Run) |
| 73 | + |
| 74 | +Run a job and wait for its result. |
| 75 | + |
| 76 | +```go |
| 77 | +job := &workq.FgJob{ |
| 78 | + ID: "6ba7b810-9dad-11d1-80b4-00c04fd430c4", |
| 79 | + Name: "ping", |
| 80 | + TTR: 5000, // 5 second time-to-run limit |
| 81 | + Timeout: 60000, // Wait up to 60 seconds for a worker to pick up. |
| 82 | + Payload: []byte("Ping!"), |
| 83 | + Priority: 10, // @OPTIONAL Numeric priority, default 0. |
| 84 | +} |
| 85 | +result, err := client.Run(job) |
| 86 | +if err != nil { |
| 87 | + // ... |
| 88 | +} |
| 89 | + |
| 90 | +fmt.Printf("Success: %t, Result: %s", result.Success, result.Result) |
| 91 | +``` |
| 92 | + |
| 93 | +#### Schedule |
| 94 | + |
| 95 | +[Protocol Doc](https://github.com/iamduo/workq/blob/master/doc/protocol.md#schedule) | [Go Doc](https://godoc.org/github.com/iamduo/go-workq#Client.Schedule) |
| 96 | + |
| 97 | +Schedule a job at a UTC time. The result can be retrieved through the ["result"](#result) command. |
| 98 | + |
| 99 | +```go |
| 100 | +job := &workq.ScheduledJob{ |
| 101 | + ID: "6ba7b810-9dad-11d1-80b4-00c04fd430c4", |
| 102 | + Name: "ping", |
| 103 | + Time: "2016-12-01T00:00:00Z", // Start job at this UTC time. |
| 104 | + TTL: 60000, // Expire after 60 seconds |
| 105 | + TTR: 5000, // 5 second time-to-run limit |
| 106 | + Payload: []byte("Ping!"), |
| 107 | + Priority: 10, // @OPTIONAL Numeric priority, default 0. |
| 108 | + MaxAttempts: 3, // @OPTIONAL Absolute max num of attempts. |
| 109 | + MaxFails: 1, // @OPTIONAL Absolute max number of failures. |
| 110 | +} |
| 111 | +err := client.Schedule(job) |
| 112 | +if err != nil { |
| 113 | + // ... |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +#### Result |
| 118 | + |
| 119 | +[Protocol Doc](https://github.com/iamduo/workq/blob/master/doc/protocol.md#result) | [Go Doc](https://godoc.org/github.com/iamduo/go-workq#Client.Result) |
| 120 | + |
| 121 | +Get a job result previously executed by [Run](#run) or [Schedule](#schedule) commands. |
| 122 | + |
| 123 | +```go |
| 124 | +// Get a job result, waiting up to 60 seconds if the job is still executing. |
| 125 | +result, err := client.Result("6ba7b810-9dad-11d1-80b4-00c04fd430c4", 60000) |
| 126 | +if err != nil { |
| 127 | + // ... |
| 128 | +} |
| 129 | + |
| 130 | +fmt.Printf("Success: %t, Result: %s", result.Success, result.Result) |
| 131 | +``` |
| 132 | + |
| 133 | +### Worker Commands |
| 134 | + |
| 135 | +#### Lease |
| 136 | + |
| 137 | +[Protocol Doc](https://github.com/iamduo/workq/blob/master/doc/protocol.md#lease) | [Go Doc](https://godoc.org/github.com/iamduo/go-workq#Client.Lease) |
| 138 | + |
| 139 | +Lease a job within a set of one or more names with a wait-timeout (milliseconds). |
| 140 | + |
| 141 | +```go |
| 142 | +// Lease the first available job in "ping1", "ping2", "ping3" |
| 143 | +// waiting up to 60 seconds. |
| 144 | +job, err := client.Lease([]string{"ping1", "ping2", "ping3"}, 60000) |
| 145 | +if err != nil { |
| 146 | + // ... |
| 147 | +} |
| 148 | + |
| 149 | +fmt.Printf("Leased Job: ID: %s, Name: %s, Payload: %s", job.ID, job.Name, job.Payload) |
| 150 | +``` |
| 151 | + |
| 152 | +#### Complete |
| 153 | + |
| 154 | +[Protocol Doc](https://github.com/iamduo/workq/blob/master/doc/protocol.md#complete) | [Go Doc](https://godoc.org/github.com/iamduo/go-workq#Client.Complete) |
| 155 | + |
| 156 | +Mark a job successfully completed with a result. |
| 157 | + |
| 158 | +```go |
| 159 | +err := client.Complete("6ba7b810-9dad-11d1-80b4-00c04fd430c4", []byte("Pong!")) |
| 160 | +if err != nil { |
| 161 | + // ... |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +#### Fail |
| 166 | + |
| 167 | +[Protocol Doc](https://github.com/iamduo/workq/blob/master/doc/protocol.md#fail) | [Go Doc](https://godoc.org/github.com/iamduo/go-workq#Client.Fail) |
| 168 | + |
| 169 | +Mark a job failed with a result. |
| 170 | + |
| 171 | +```go |
| 172 | +err := client.Fail("6ba7b810-9dad-11d1-80b4-00c04fd430c4", []byte("Failed-Pong!")) |
| 173 | +if err != nil { |
| 174 | + // ... |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | +### Adminstrative Commands |
| 179 | + |
| 180 | +#### Delete |
| 181 | + |
| 182 | +[Protocol Doc](https://github.com/iamduo/workq/blob/master/doc/protocol.md#delete) | [Go Doc](https://godoc.org/github.com/iamduo/go-workq#Client.Delete) |
| 183 | + |
| 184 | + |
| 185 | +```go |
| 186 | +err := client.Delete("6ba7b810-9dad-11d1-80b4-00c04fd430c4") |
| 187 | +if err != nil { |
| 188 | + // ... |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | + |
| 193 | +#### Inspect |
| 194 | + |
| 195 | +[Protocol Doc](https://github.com/iamduo/workq/blob/master/doc/protocol.md#inspect) |
| 196 | + |
| 197 | +Inspect commands not yet supported yet. |
0 commit comments