-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
591 lines (516 loc) · 18.3 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
package relay
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/go-playground/backoff-sys"
"github.com/go-playground/errors/v5"
errorsext "github.com/go-playground/pkg/v5/errors"
httpext "github.com/go-playground/pkg/v5/net/http"
unsafeext "github.com/go-playground/pkg/v5/unsafe"
)
// Job defines all information needed to process a job.
type Job[P any, S any] struct {
// ID is the unique Job ID which is also CAN be used to ensure the Job is a singleton.
ID string `json:"id"`
// Queue is used to differentiate different job types that can be picked up by job runners.
Queue string `json:"queue"`
// Timeout denotes the duration, in seconds, after a Job has started processing or since the last
// heartbeat request occurred before considering the Job failed and being put back into the
// queue.
Timeout int32 `json:"timeout"`
// MaxRetries determines how many times the Job can be retried, due to timeouts, before being considered
// permanently failed.
MaxRetries int32 `json:"max_retries,omitempty"`
// Payload is the raw JSON payload that the job runner will receive.
Payload P `json:"payload"`
// State is the raw JSON payload that the job runner will receive.
State *S `json:"state,omitempty"`
// RunAt can optionally schedule/set a Job to be run only at a specific time in the
// future. This option should mainly be used for one-time jobs and scheduled jobs that have
// the option of being self-perpetuated in combination with the rescheduling endpoint.
RunAt *time.Time `json:"run_at,omitempty"`
// UpdatedAt indicates last time the Job was updated either through enqueue, reschedule or heartbeat.
// This value is for reporting purposes only and will be ignored when enqueuing and rescheduling Jobs.
UpdatedAt *time.Time `json:"updated_at"`
}
// Config contains all information to create a new Relay instance fo use.
type Config struct {
// BasURL of the HTTP server
BaseURL string
// NextBackoff if the backoff used when calling the `next` endpoint and there is no data yet available.
// Optional: If not set a default backoff is used.
NextBackoff backoff.Exponential
// RetryBackoff is the backoff used when calling any of the retryable functions.
RetryBackoff backoff.Exponential
// Client is the HTTP Client to use if using a custom one is desired.
// Optional: If not set it will create a new one cloning the `http.DefaultTransport` and tweaking the settings
// for use with sane limits & Defaults.
Client *http.Client
}
// Client is used to interact with the Client Job Server.
type Client[P any, S any] struct {
baseURL string
nextBo backoff.Exponential
retryBo backoff.Exponential
client *http.Client
}
// New creates a new Client instance for use.
func New[P any, S any](cfg Config) (*Client[P, S], error) {
cfg.BaseURL = strings.TrimRight(strings.TrimSpace(cfg.BaseURL), "/")
_, err := url.Parse(cfg.BaseURL)
if err != nil {
return nil, errors.Wrap(err, "Invalid URL")
}
defaultBackoff := backoff.Exponential{}
if cfg.NextBackoff == defaultBackoff {
cfg.NextBackoff = backoff.NewExponential().Interval(time.Millisecond * 100).Jitter(time.Millisecond * 25).Max(time.Second).Init()
}
if cfg.RetryBackoff == defaultBackoff {
cfg.RetryBackoff = backoff.NewExponential().Interval(time.Millisecond * 100).Jitter(time.Millisecond * 25).Max(time.Second).Init()
}
if cfg.Client == nil {
trans := http.DefaultTransport.(*http.Transport).Clone()
trans.MaxConnsPerHost = 1024
trans.MaxIdleConnsPerHost = 512
trans.IdleConnTimeout = time.Second * 5
cfg.Client = &http.Client{
Transport: trans,
}
}
r := &Client[P, S]{
baseURL: cfg.BaseURL,
nextBo: cfg.NextBackoff,
retryBo: cfg.RetryBackoff,
client: cfg.Client,
}
return r, nil
}
// Enqueue submits the provided Job for processing to the Job Server.
func (r *Client[P, S]) Enqueue(ctx context.Context, job Job[P, S]) error {
return r.EnqueueBatch(ctx, []Job[P, S]{job})
}
// EnqueueBatch submits one or more Jobs for processing to the Job Server in one call.
func (r *Client[P, S]) EnqueueBatch(ctx context.Context, jobs []Job[P, S]) error {
b, err := json.Marshal(jobs)
if err != nil {
return errors.Wrap(err, "failed to marshal jobs")
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, r.baseURL+"/v1/queues/jobs", bytes.NewReader(b))
if err != nil {
return errors.Wrap(err, "failed to create enqueue batch request")
}
req.Header.Set(httpext.ContentType, httpext.ApplicationJSON)
resp, err := r.client.Do(req)
if err != nil {
return errors.Wrap(err, "failed to make enqueue batch request")
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusAccepted:
return nil
case http.StatusConflict:
b, _ := io.ReadAll(resp.Body)
return ErrJobExits{message: unsafeext.BytesToString(b)}
default:
if httpext.IsRetryableStatusCode(resp.StatusCode) {
return retryableErr{err: errors.Newf("Temporary error occurred %d", resp.StatusCode)}
}
b, _ := io.ReadAll(resp.Body)
return errors.New("unexpected error occurred").AddTag("body", unsafeext.BytesToString(b)).AddTag("status_code", resp.StatusCode)
}
}
// Next attempts to retrieve the next Job in the `queue` requested. It will retry and backoff attempting to retrieve
// a Job and will block until retrieving a Job or the Context is cancelled.
func (r *Client[P, S]) Next(ctx context.Context, queue string, num_jobs uint32) ([]*JobHelper[P, S], error) {
nextURL := fmt.Sprintf("%s/v1/queues/%s/jobs?num_jobs=%d", r.baseURL, url.QueryEscape(queue), num_jobs)
var attempt int
for {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, nextURL, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create next request")
}
resp, err := r.client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to make next request")
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
var jobs []*Job[P, S]
err := json.NewDecoder(resp.Body).Decode(&jobs)
if err != nil {
// connection must have been disrupted, continue to retrieve, the Job IF lost will
// be retried.
continue
}
helpers := make([]*JobHelper[P, S], 0, len(jobs))
for _, j := range jobs {
j := j
helpers = append(helpers, &JobHelper[P, S]{
client: r,
httpClient: r.client,
job: j,
})
}
return helpers, nil
default:
if resp.StatusCode == http.StatusNoContent || httpext.IsRetryableStatusCode(resp.StatusCode) {
// includes http.StatusNoContent and http.TooManyRequests
// no new jobs to process
if err := r.nextBo.Sleep(ctx, attempt); err != nil {
// only context.Cancel as error ever
return nil, err
}
attempt++
continue
}
return nil, errors.Newf("invalid request, status code: %d", resp.StatusCode)
}
}
}
// Remove removes the Job from the DB for processing. In fact this function makes a call to the complete endpoint.
//
// NOTE: It does not matter if the Job is in-flight or not it will be removed. All relevant code paths return an
//
// ErrNotFound to handle such events within Job Workers so that they can bail gracefully if desired.
func (r *Client[P, S]) Remove(ctx context.Context, queue, jobID string) error {
removeURL := fmt.Sprintf("%s/v1/queues/%s/jobs/%s", r.baseURL, url.QueryEscape(queue), url.QueryEscape(jobID))
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, removeURL, nil)
if err != nil {
return errors.Wrap(err, "failed to create complete request")
}
resp, err := r.client.Do(req)
if err != nil {
if errors.Is(err, io.EOF) {
return retryableErr{err: errors.New("Temporary error occurred EOF")}
}
return errors.Wrap(err, "failed to make complete request")
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
return nil
case http.StatusNotFound:
b, _ := io.ReadAll(resp.Body)
return ErrNotFound{message: unsafeext.BytesToString(b)}
default:
if httpext.IsRetryableStatusCode(resp.StatusCode) {
return retryableErr{err: errors.Newf("Temporary error occurred %d", resp.StatusCode)}
}
b, _ := io.ReadAll(resp.Body)
return errors.New("unexpected error occurred").AddTag("body", unsafeext.BytesToString(b)).AddTag("status_code", resp.StatusCode)
}
}
func (r *Client[P, S]) removeRetryable(ctx context.Context, queue, jobID string) error {
var attempts int
for {
if attempts > 0 {
if err := r.retryBo.Sleep(ctx, attempts); err != nil {
// can only occur if context cancelled or timout
return err
}
}
err := r.Remove(ctx, queue, jobID)
if err != nil {
if _, isRetryable := errorsext.IsRetryableHTTP(err); isRetryable {
attempts++
continue
}
return errors.Wrap(err, "failed to remove job")
}
return nil
}
}
// Exists checks if a Job exists.
func (r *Client[P, S]) Exists(ctx context.Context, queue, jobID string) (bool, error) {
existsURL := fmt.Sprintf("%s/v1/queues/%s/jobs/%s", r.baseURL, url.QueryEscape(queue), url.QueryEscape(jobID))
req, err := http.NewRequestWithContext(ctx, http.MethodHead, existsURL, nil)
if err != nil {
return false, errors.Wrap(err, "failed to create complete request")
}
resp, err := r.client.Do(req)
if err != nil {
if errors.Is(err, io.EOF) {
return false, retryableErr{err: errors.New("Temporary error occurred EOF")}
}
return false, errors.Wrap(err, "failed to make complete request")
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
return true, nil
case http.StatusNotFound:
return false, nil
default:
if httpext.IsRetryableStatusCode(resp.StatusCode) {
return false, retryableErr{err: errors.Newf("Temporary error occurred %d", resp.StatusCode)}
}
b, _ := io.ReadAll(resp.Body)
return false, errors.New("unexpected error occurred").AddTag("body", unsafeext.BytesToString(b)).AddTag("status_code", resp.StatusCode)
}
}
// ExistsWithRetry is the same as Exits only automatically handles retryable errors.
func (r *Client[P, S]) ExistsWithRetry(ctx context.Context, queue, jobID string) (bool, error) {
var attempts int
for {
if attempts > 0 {
if err := r.retryBo.Sleep(ctx, attempts); err != nil {
// can only occur if context cancelled or timout
return false, err
}
}
exists, err := r.Exists(ctx, queue, jobID)
if err != nil {
if _, isRetryable := errorsext.IsRetryableHTTP(err); isRetryable {
attempts++
continue
}
return false, errors.Wrap(err, "failed to remove job")
}
return exists, nil
}
}
// Get retrieves a Job from the database for debugging or usage of state data.
func (r *Client[P, S]) Get(ctx context.Context, queue, jobID string) (*Job[P, S], error) {
getURL := fmt.Sprintf("%s/v1/queues/%s/jobs/%s", r.baseURL, url.QueryEscape(queue), url.QueryEscape(jobID))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create complete request")
}
resp, err := r.client.Do(req)
if err != nil {
if errors.Is(err, io.EOF) {
return nil, retryableErr{err: errors.New("Temporary error occurred EOF")}
}
return nil, errors.Wrap(err, "failed to make complete request")
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
var job *Job[P, S]
err := json.NewDecoder(resp.Body).Decode(&job)
if err != nil {
// must assume this is retryable, must be a broken connection
return nil, retryableErr{err: err}
}
return job, nil
case http.StatusNotFound:
return nil, ErrNotFound{message: fmt.Sprintf("Job could not be found within queue: %s with id: %s", queue, jobID)}
default:
if httpext.IsRetryableStatusCode(resp.StatusCode) {
return nil, retryableErr{err: errors.Newf("Temporary error occurred %d", resp.StatusCode)}
}
b, _ := io.ReadAll(resp.Body)
return nil, errors.New("unexpected error occurred").AddTag("body", unsafeext.BytesToString(b)).AddTag("status_code", resp.StatusCode)
}
}
// GetWithRetry is the same as Get only automatically handles retryable errors.
func (r *Client[P, S]) GetWithRetry(ctx context.Context, queue, jobID string) (*Job[P, S], error) {
var attempts int
for {
if attempts > 0 {
if err := r.retryBo.Sleep(ctx, attempts); err != nil {
// can only occur if context cancelled or timout
return nil, err
}
}
job, err := r.Get(ctx, queue, jobID)
if err != nil {
if _, isRetryable := errorsext.IsRetryableHTTP(err); isRetryable {
attempts++
continue
}
return nil, errors.Wrap(err, "failed to remove job")
}
return job, nil
}
}
// JobHelper is used to process an individual Job retrieved from the Job Server. It contains a number of helper methods
// to `Heartbeat` and `Complete` Jobs.
type JobHelper[P any, S any] struct {
client *Client[P, S]
httpClient *http.Client
cancel context.CancelFunc
job *Job[P, S]
wg sync.WaitGroup
}
// Job returns the Job to process
func (j *JobHelper[P, S]) Job() *Job[P, S] {
return j.job
}
// HeartbeatAuto automatically calls the Job Runners heartbeat endpoint in a separate goroutine on the
// provided interval. It is convenience to use this when no state needs to be saved but Job kept alive.
func (j *JobHelper[P, S]) HeartbeatAuto(ctx context.Context, interval time.Duration) {
ctx, cancel := context.WithCancel(ctx)
j.cancel = cancel
j.wg.Add(1)
go func() {
defer j.wg.Done()
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
err := j.Heartbeat(ctx, nil)
if err != nil && errors.Is(err, context.Canceled) {
return
}
}
}
}()
}
// Heartbeat calls the Job Runners heartbeat endpoint to keep the job alive.
// Optional: It optionally accepts a state payload if desired to be used in case of failure for
//
// point-in-time restarting.
func (j *JobHelper[P, S]) Heartbeat(ctx context.Context, state *S) error {
var err error
var req *http.Request
heartbeatURL := fmt.Sprintf("%s/v1/queues/%s/jobs/%s", j.client.baseURL, url.QueryEscape(j.Job().Queue), url.QueryEscape(j.Job().ID))
if state != nil {
var b []byte
b, err = json.Marshal(state)
if err != nil {
return errors.Wrap(err, "failed to marshal heartbeat state")
}
req, err = http.NewRequestWithContext(ctx, http.MethodPatch, heartbeatURL, bytes.NewReader(b))
} else {
req, err = http.NewRequestWithContext(ctx, http.MethodPatch, heartbeatURL, nil)
}
if err != nil {
return errors.Wrap(err, "failed to create heartbeat request")
}
req.Header.Set(httpext.ContentType, httpext.ApplicationJSON)
resp, err := j.httpClient.Do(req)
if err != nil {
return errors.Wrap(err, "failed to make heartbeat request")
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusAccepted:
return nil
case http.StatusNotFound:
b, _ := io.ReadAll(resp.Body)
return ErrNotFound{message: unsafeext.BytesToString(b)}
default:
if httpext.IsRetryableStatusCode(resp.StatusCode) {
return retryableErr{err: errors.Newf("Temporary error occurred %d", resp.StatusCode)}
}
b, _ := io.ReadAll(resp.Body)
return errors.New("unexpected error occurred").AddTag("body", unsafeext.BytesToString(b)).AddTag("status_code", resp.StatusCode)
}
}
// Reschedule submits the provided Job for processing by rescheduling an existing Job for another iteration.
func (j *JobHelper[P, S]) Reschedule(ctx context.Context, job Job[P, S]) error {
b, err := json.Marshal(job)
if err != nil {
return errors.Wrap(err, "failed to marshal job")
}
req, err := http.NewRequestWithContext(ctx, http.MethodPut, j.client.baseURL+"/v1/queues/jobs", bytes.NewReader(b))
if err != nil {
return errors.Wrap(err, "failed to create reschedule request")
}
req.Header.Set(httpext.ContentType, httpext.ApplicationJSON)
resp, err := j.httpClient.Do(req)
if err != nil {
return errors.Wrap(err, "failed to make reschedule request")
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusAccepted:
return nil
case http.StatusNotFound:
b, _ := io.ReadAll(resp.Body)
return ErrNotFound{message: unsafeext.BytesToString(b)}
default:
if httpext.IsRetryableStatusCode(resp.StatusCode) {
return retryableErr{err: errors.Newf("Temporary error occurred %d", resp.StatusCode)}
}
b, _ := io.ReadAll(resp.Body)
return errors.New("unexpected error occurred").AddTag("body", unsafeext.BytesToString(b)).AddTag("status_code", resp.StatusCode)
}
}
// RescheduleWithRetry is the same as Reschedule but automatically retries on transient errors.
func (j *JobHelper[P, S]) RescheduleWithRetry(ctx context.Context, job Job[P, S]) error {
var attempts int
for {
if attempts > 0 {
if err := j.client.retryBo.Sleep(ctx, attempts); err != nil {
// can only occur if context cancelled or timout
return err
}
}
err := j.Reschedule(ctx, job)
if err != nil {
if _, isRetryable := errorsext.IsRetryableHTTP(err); isRetryable {
attempts++
continue
}
return errors.Wrap(err, "failed to reschedule job")
}
return nil
}
}
// Complete marks the Job as complete. It does NOT matter to the Job Runner if the job was successful or not.
func (j *JobHelper[P, S]) Complete(ctx context.Context) error {
if j.cancel != nil {
j.cancel()
j.wg.Wait()
}
err := j.client.Remove(ctx, j.Job().Queue, j.Job().ID)
if err != nil {
return errors.Wrap(err, "failed to complete job")
}
return nil
}
// CompleteWithRetry is the same as Complete but also automatically retries on transient errors.
func (j *JobHelper[P, S]) CompleteWithRetry(ctx context.Context) error {
if j.cancel != nil {
j.cancel()
j.wg.Wait()
}
err := j.client.removeRetryable(ctx, j.Job().Queue, j.Job().ID)
if err != nil {
return errors.Wrap(err, "failed to complete job")
}
return nil
}
// denotes a retryable error by implementing the `IsTemporary` function.
type retryableErr struct {
err error
}
// Error returns the error in string form.
func (r retryableErr) Error() string {
return r.err.Error()
}
// Temporary denotes if this error is retryable.
func (r retryableErr) Temporary() bool {
return true
}
// ErrNotFound indicates that the queue and/or Job you specified could not be found on the Job Server.
type ErrNotFound struct {
message string
}
// Error returns the error in string form.
func (e ErrNotFound) Error() string {
return e.message
}
// ErrJobExits denotes that the Job that was attempted to be submitted/enqueued on the Job Server already exists and
// the Job was not accepted because of this.
type ErrJobExits struct {
message string
}
// Error returns the error in string form.
func (e ErrJobExits) Error() string {
return e.message
}