-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
258 lines (222 loc) · 6.17 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
package pgpq
import (
"context"
"database/sql"
"encoding/json"
"errors"
"time"
"github.com/benbjohnson/clock"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgconn"
_ "github.com/jackc/pgx/v5/stdlib" // support pgx connections
)
// Client implements a queue client.
type Client struct {
db *sql.DB
opt *scopeOptions
ownDB bool
clock clock.Clock
}
// Connect connects to a PG instance using a URL.
// Example:
//
// postgres://user:[email protected]:5432/mydb?sslmode=verify-ca
func Connect(ctx context.Context, url string, opts ...ScopeOption) (*Client, error) {
db, err := sql.Open("pgx", url)
if err != nil {
return nil, err
}
client, err := Wrap(ctx, db, opts...)
if err != nil {
_ = db.Close()
return nil, err
}
client.ownDB = true
return client, nil
}
// Wrap wraps an existing database/sql.DB instance. Please note that calling
// Close() will not close the underlying connection.
func Wrap(ctx context.Context, db *sql.DB, opts ...ScopeOption) (*Client, error) {
opt := &scopeOptions{}
opt.set(opts...)
if err := opt.validate(); err != nil {
return nil, err
}
if err := validateConn(ctx, db); err != nil {
return nil, err
}
c := &Client{db: db, opt: opt, clock: clock.New()}
return c, nil
}
// Truncate truncates the queue and deletes all tasks. Intended for testing,
// please use with care.
func (c *Client) Truncate(ctx context.Context, opts ...ScopeOption) error {
opt := &scopeOptions{Namespace: c.opt.Namespace}
opt.set(opts...)
if err := opt.validate(); err != nil {
return err
}
_, err := c.db.ExecContext(ctx, `DELETE FROM pgpq_tasks WHERE namespace = $1`, opt.Namespace)
return err
}
// Len returns the queue length. This counts all the non-delayed tasks.
func (c *Client) Len(ctx context.Context, opts ...ScopeOption) (int64, error) {
var cnt int64
opt := &scopeOptions{Namespace: c.opt.Namespace}
opt.set(opts...)
if err := opt.validate(); err != nil {
return cnt, err
}
if err := c.db.
QueryRowContext(ctx, `SELECT COUNT(*) FROM pgpq_tasks WHERE namespace = $1 AND not_before <= $2`,
opt.Namespace,
c.clock.Now(),
).
Scan(&cnt); err != nil {
return cnt, err
}
return cnt, nil
}
// MinCreatedAt returns created timestamp of the oldest non-delayed task in the queue.
// It may return ErrNoTask.
func (c *Client) MinCreatedAt(ctx context.Context, opts ...ScopeOption) (time.Time, error) {
var ts sql.NullTime
opt := &scopeOptions{Namespace: c.opt.Namespace}
opt.set(opts...)
if err := opt.validate(); err != nil {
return ts.Time, err
}
if err := c.db.
QueryRowContext(ctx, `SELECT MIN(created_at) FROM pgpq_tasks WHERE namespace = $1 AND not_before <= $2`,
opt.Namespace,
c.clock.Now(),
).
Scan(&ts); err != nil {
return ts.Time, err
} else if !ts.Valid {
return ts.Time, ErrNoTask
}
return ts.Time, nil
}
// Push pushes a task into the queue. It may return ErrDuplicateID.
func (c *Client) Push(ctx context.Context, task *Task) error {
if err := task.validate(); err != nil {
return err
}
if task.Namespace == "" && c.opt.Namespace != "" {
task.Namespace = string(c.opt.Namespace)
}
if len(task.Payload) == 0 {
task.Payload = json.RawMessage{'{', '}'}
}
now := c.clock.Now()
var row *sql.Row
if task.ID == uuid.Nil {
row = c.db.QueryRowContext(ctx, stmtPush, task.Namespace, task.Priority, unsafeString(task.Payload), coalesceTime(task.NotBefore, unixZero), now, now)
} else {
row = c.db.QueryRowContext(ctx, stmtPushWithID, task.ID, task.Namespace, task.Priority, unsafeString(task.Payload), coalesceTime(task.NotBefore, unixZero), now, now)
}
if err := row.Scan(&task.ID); err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) && pgErr.Code == "23505" && pgErr.ConstraintName == "pgpq_tasks_pkey" {
return ErrDuplicateID
}
return err
}
return nil
}
// Get returns a task by ID. It may return ErrNoTask.
func (c *Client) Get(ctx context.Context, id uuid.UUID) (*TaskDetails, error) {
td := new(TaskDetails)
row := c.db.QueryRowContext(ctx, stmtGet, id)
if err := td.scan(row); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNoTask
}
return nil, err
}
return td, nil
}
// Claim locks and returns the task with the given ID. It may return ErrNoTask.
func (c *Client) Claim(ctx context.Context, id uuid.UUID) (*Claim, error) {
tx, err := c.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
claim := c.newClaim(tx)
row := tx.QueryRowContext(ctx, stmtClaim, id)
if err := claim.TaskDetails.scan(row); err != nil {
_ = tx.Rollback()
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNoTask
}
return nil, err
}
return claim, nil
}
// Shift locks and returns the non-delayed task with the highest priority.
// It may return ErrNoTask.
func (c *Client) Shift(ctx context.Context, opts ...ScopeOption) (*Claim, error) {
opt := &scopeOptions{Namespace: c.opt.Namespace}
opt.set(opts...)
if err := opt.validate(); err != nil {
return nil, err
}
tx, err := c.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
claim := c.newClaim(tx)
row := tx.
QueryRowContext(ctx, stmtShift, opt.Namespace, c.clock.Now())
if err := claim.TaskDetails.scan(row); err != nil {
_ = tx.Rollback()
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNoTask
}
return nil, err
}
return claim, nil
}
// List lists all tasks (incl. delayed) in the queue.
func (c *Client) List(ctx context.Context, opts ...ListOption) ([]*TaskDetails, error) {
opt := &listOptions{Namespace: c.opt.Namespace}
opt.set(opts...)
if err := opt.validate(); err != nil {
return nil, err
}
limit := opt.getLimit()
rows, err := c.db.QueryContext(ctx, stmtList, opt.Namespace, limit, opt.Offset)
if err != nil {
return nil, err
}
defer rows.Close()
tasks := make([]*TaskDetails, 0, limit)
for rows.Next() {
task := new(TaskDetails)
if err := task.scan(rows); err != nil {
return nil, err
}
tasks = append(tasks, task)
}
if err := rows.Err(); err != nil {
return nil, err
}
return tasks, nil
}
// Close closes the client connection.
func (c *Client) Close() error {
var err error
if c.ownDB {
if e := c.db.Close(); e != nil {
err = e
}
}
return err
}
func (c *Client) newClaim(tx *sql.Tx) *Claim {
return &Claim{
tx: tx,
clock: c.clock,
}
}