-
Notifications
You must be signed in to change notification settings - Fork 1
/
task.go
57 lines (49 loc) · 1.55 KB
/
task.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
package backlite
import (
"context"
"database/sql"
"time"
)
type (
// Task represents a task that will be placed in to a queue for execution.
Task interface {
// Config returns the configuration options for the queue that this Task will be placed in.
Config() QueueConfig
}
// TaskAddOp facilitates adding Tasks to the queue.
TaskAddOp struct {
client *Client
ctx context.Context
tasks []Task
wait *time.Time
tx *sql.Tx
}
)
// Ctx sets the request context.
func (t *TaskAddOp) Ctx(ctx context.Context) *TaskAddOp {
t.ctx = ctx
return t
}
// At sets the time the task should not be executed until.
func (t *TaskAddOp) At(processAt time.Time) *TaskAddOp {
t.wait = &processAt
return t
}
// Wait instructs the task to wait a given duration before it is executed.
func (t *TaskAddOp) Wait(duration time.Duration) *TaskAddOp {
t.At(now().Add(duration))
return t
}
// Tx will include the task as part of a given database transaction.
// When using this, it is critical that after you commit the transaction that you call Notify() on the
// client so the dispatcher is aware that a new task has been created, otherwise it may not be executed.
// This is necessary because there is, unfortunately, no way for outsiders to know if or when a transaction
// is committed and since the dispatcher avoids continuous polling, it needs to know when tasks are added.
func (t *TaskAddOp) Tx(tx *sql.Tx) *TaskAddOp {
t.tx = tx
return t
}
// Save saves the task, so it can be queued for execution.
func (t *TaskAddOp) Save() error {
return t.client.save(t)
}