-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
47 lines (39 loc) · 938 Bytes
/
queue.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 archer
import (
"context"
"database/sql"
"time"
"github.com/dyaksa/archer/job"
"github.com/dyaksa/archer/store"
)
type Queue struct {
store.WrapperTx
tx func(*sql.Tx) Tx
now func() time.Time
name string
}
func NewQueue(db *sql.DB, name string) *Queue {
return &Queue{
WrapperTx: *store.NewWrapperTx(db),
now: time.Now,
name: name,
tx: func(tx *sql.Tx) Tx {
return newTx(tx)
},
}
}
func (q *Queue) Poll(ctx context.Context) (*job.Job, error) {
res, err := q.WrapTx(ctx, func(ctx context.Context, tx *sql.Tx) (any, error) {
return q.tx(tx).Poll(ctx, q.name)
})
if err != nil {
return nil, err
}
return res.(*job.Job), nil
}
func (q *Queue) RequeueTimeout(ctx context.Context, timeout time.Duration) error {
_, err := q.WrapTx(ctx, func(ctx context.Context, tx *sql.Tx) (any, error) {
return nil, q.tx(tx).RequeueTimeout(ctx, q.name, q.now().Add(-timeout))
})
return err
}