forked from vgarvardt/gue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
99 lines (85 loc) · 2.7 KB
/
job.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
package guex
import (
"context"
"database/sql"
"errors"
"sync/atomic"
"time"
"github.com/2tvenom/guex/database"
)
// Job is a single unit of work for Gue to perform.
type Job struct {
database.Job
processed int32
db *database.Queries
backoff Backoff
}
// Fail marks this job as failed
//
// You must also later call Done() to return this job's database connection to
// the pool. If you got the job from the worker - it will take care of cleaning up the job and resources,
// no need to do this manually in a WorkFunc.
func (j *Job) Fail(ctx context.Context) (err error) {
return j.setProcessed(ctx, database.JobStatusFailed)
}
// Done commits transaction that marks job as done. If you got the job from the worker - it will take care of
// cleaning up the job and resources, no need to do this manually in a WorkFunc.
func (j *Job) Done(ctx context.Context) error {
j.LastError.Valid = false
return j.setProcessed(ctx, database.JobStatusFinished)
}
// Error marks the job as failed and schedules it to be reworked. An error
// message or backtrace can be provided as msg, which will be saved on the job.
// It will also increase the error count.
//
// This call marks job as done and releases (commits) transaction,
// so calling Done() is not required, although calling it will not cause any issues.
// If you got the job from the worker - it will take care of cleaning up the job and resources,
// no need to do this manually in a WorkFunc.
func (j *Job) Error(ctx context.Context, jErr error) (err error) {
var (
errorCount = j.ErrorCount + 1
newRunAt = j.calculateErrorRunAt(jErr, errorCount)
)
if newRunAt.IsZero() {
j.LastError = sql.NullString{String: jErr.Error(), Valid: true}
return j.Fail(ctx)
}
return j.db.UpdateStatusError(ctx, database.UpdateStatusErrorParams{
ID: j.ID,
ErrorCount: errorCount,
RunAt: newRunAt,
LastError: sql.NullString{String: jErr.Error(), Valid: true},
})
}
func (j *Job) setProcessed(ctx context.Context, status database.JobStatus) (err error) {
if atomic.LoadInt32(&j.processed) == 1 {
return nil
}
if err = j.db.UpdateStatus(ctx, database.UpdateStatusParams{
ID: j.ID,
Status: status,
LastError: j.LastError,
}); err != nil {
return err
}
atomic.AddInt32(&j.processed, 1)
return nil
}
func (j *Job) calculateErrorRunAt(err error, errorCount int32) time.Time {
if errReschedule, ok := err.(ErrJobReschedule); ok {
return errReschedule.RescheduleJobAt()
}
if errors.Is(err, ErrDiscard) {
j.LastError = sql.NullString{
String: err.Error(),
Valid: true,
}
return time.Time{}
}
var backoff = j.backoff(j, int(errorCount))
if backoff < 0 {
return time.Time{}
}
return time.Now().UTC().Add(backoff)
}