-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclaim.go
47 lines (38 loc) · 1.1 KB
/
claim.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
package pgpq
import (
"context"
"database/sql"
"github.com/benbjohnson/clock"
)
// Claim contains a claim on a task. The owner of the claim has an exclusive
// lock on the task. You must call either Release, Update or Done to release the
// claim.
type Claim struct {
TaskDetails
tx *sql.Tx
clock clock.Clock
}
// Release releases the claim and returns the task back to the queue.
func (tc *Claim) Release(_ context.Context) error {
return tc.tx.Rollback()
}
// Update updates Namespace, Payload, Priority, UpdatedAt and returns the task
// back to the queue.
func (tc *Claim) Update(ctx context.Context) error {
if err := tc.validate(); err != nil {
return err
}
_, err := tc.tx.ExecContext(ctx, stmtUpdate, tc.Namespace, tc.Priority, unsafeString(tc.Payload), coalesceTime(tc.NotBefore, unixZero), tc.clock.Now(), tc.ID)
if err != nil {
return err
}
return tc.tx.Commit()
}
// Done marks the task as done and removes it from the queue.
func (tc *Claim) Done(ctx context.Context) error {
_, err := tc.tx.ExecContext(ctx, stmtDone, tc.ID)
if err != nil {
return err
}
return tc.tx.Commit()
}