-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob.go
167 lines (143 loc) · 5.33 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
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
package jobqueue
import (
"encoding/json"
"errors"
"fmt"
"time"
"github.com/domonda/go-types/notnull"
"github.com/domonda/go-types/nullable"
"github.com/domonda/go-types/uu"
)
type Job struct {
ID uu.ID `db:"id" json:"id"`
BundleID uu.NullableID `db:"bundle_id" json:"bundleId"`
Type string `db:"type" json:"type"` // CHECK(length("type") > 0 AND length("type") <= 100)
Payload notnull.JSON `db:"payload" json:"payload"`
Priority int64 `db:"priority" json:"priority"`
Origin string `db:"origin" json:"origin"` // CHECK(length(origin) > 0 AND length(origin) <= 100)
StartAt nullable.Time `db:"start_at" json:"startAt"` // If not NULL, earliest time to start the job
StartedAt nullable.Time `db:"started_at" json:"startedAt"` // Time when started working on the job, or NULL when not started
StoppedAt nullable.Time `db:"stopped_at" json:"stoppedAt"` // Time when working on job was stoped because of a decision question or an error, or NULL
ErrorMsg nullable.NonEmptyString `db:"error_msg" json:"errorMsg"` // If there was an error working off the job
ErrorData nullable.JSON `db:"error_data" json:"errorData"` // Optional error metadata
Result nullable.JSON `db:"result" json:"result"` // Result if the job returned one
UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
CreatedAt time.Time `db:"created_at" json:"createdAt"`
}
// IsStarted returns if StartedAt is not null.
// Valid to call on a nil receiver.
func (j *Job) IsStarted() bool {
if j == nil {
return false
}
return j.StartedAt.IsNotNull()
}
// IsStopped returns if StoppedAt is not null.
// Valid to call on a nil receiver.
func (j *Job) IsStopped() bool {
if j == nil {
return false
}
return j.StoppedAt.IsNotNull()
}
// IsFinished returns if a job has been finished without an error.
// Valid to call on a nil receiver.
func (j *Job) IsFinished() bool {
return j.IsStopped() && !j.HasError()
}
// HasError returns true if the receiver is not nil
// and has an ErrorMsg.
// Valid to call on a nil receiver.
func (j *Job) HasError() bool {
return j != nil && j.ErrorMsg.IsNotNull()
}
// String implements the fmt.Stringer interface.
// Valid to call on a nil receiver.
func (j *Job) String() string {
if j == nil {
return "nil Job"
}
return fmt.Sprintf("Job %s, type %s, priority %d, created at %s from origin '%s'", j.ID, j.Type, j.Priority, j.CreatedAt, j.Origin)
}
// NewJobWithPriority creates a Job but does not add it to the queue.
// The passed payload will be marshalled to JSON or directly interpreted as JSON if possible.
// If startAt is not null then the job will not start before that time.
func NewJobWithPriority(id uu.ID, jobType, origin string, payload any, priority int64, startAt nullable.Time) (*Job, error) {
if jobType == "" {
return nil, errors.New("empty jobType")
}
if payload == nil {
return nil, errors.New("nil job payload")
}
var (
payloadJSON notnull.JSON
err error
)
switch x := payload.(type) {
case notnull.JSON:
payloadJSON = x
if !payloadJSON.Valid() {
return nil, fmt.Errorf("job payload is not valid JSON: %#v", string(x))
}
case nullable.JSON:
payloadJSON = notnull.JSON(x)
if !payloadJSON.Valid() {
return nil, fmt.Errorf("job payload is not valid JSON: %#v", string(x))
}
case json.RawMessage:
payloadJSON = notnull.JSON(x)
if !payloadJSON.Valid() {
return nil, fmt.Errorf("job payload is not valid JSON: %#v", string(x))
}
case []byte:
payloadJSON = notnull.JSON(x)
if !payloadJSON.Valid() {
return nil, fmt.Errorf("job payload is not valid JSON: %#v", string(x))
}
case string:
payloadJSON = notnull.JSON(x)
if !payloadJSON.Valid() {
return nil, fmt.Errorf("job payload is not valid JSON: %#v", x)
}
case json.Marshaler:
payloadJSON, err = x.MarshalJSON()
if err != nil {
return nil, fmt.Errorf("job payload is not valid JSON: %#v, error: %w", x, err)
}
default:
payloadJSON, err = notnull.MarshalJSON(x)
if err != nil {
return nil, fmt.Errorf("job payload is not valid JSON: %#v, error: %w", x, err)
}
}
now := time.Now()
job := &Job{
ID: id,
Type: jobType,
Payload: payloadJSON,
Origin: origin,
Priority: priority,
StartAt: startAt,
UpdatedAt: now,
CreatedAt: now,
}
return job, nil
}
// NewJob creates a Job but does not add it to the queue.
// The passed payload will be marshalled to JSON or directly interpreted as JSON if possible.
// If startAt is not null then the job will not start before that time.
func NewJob(id uu.ID, jobType, origin string, payload any, startAt nullable.Time) (*Job, error) {
return NewJobWithPriority(id, jobType, origin, payload, 0, startAt)
}
// NewJobReflectType creates a Job but does not add it to the queue.
// The passed payload will be marshalled to JSON or directly interpreted as JSON if possible.
// If startAt is not null then the job will not start before that time.
//
// ReflectJobTypeOfPayload(payload) is used to
// create a job type string by using reflection on payload.
// The job type string starts with the package import path of the type
// followed by a point and the type name.
// Pointer types will be dereferenced.
func NewJobReflectType(id uu.ID, origin string, payload any, startAt nullable.Time) (*Job, error) {
return NewJob(id, ReflectJobTypeOfPayload(payload), origin, payload, startAt)
}