-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
63 lines (49 loc) · 1.37 KB
/
common.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
package beanbroker
import (
"time"
"golang.org/x/net/context"
)
const (
Bury JobResult = iota
Delete
Release
Touch
)
// JobResult represents the expected action done by broker after Do() returns
type JobResult int
// JobId represents beanstalkd job id
type JobId uint64
// JobData represents data bytes
type JobData []byte
// JobType represents tube name in beanstalkd
type JobType string
// Worker provides common interface for worker implementation
type Worker interface {
// Do performs whatever the worker supposed to do
// upon finish or intentional break, return value will determine the job state
Do(context.Context, *Job) JobResult
}
type WorkerFunc func(context.Context, *Job) JobResult
func (f WorkerFunc) Do(c context.Context, job *Job) JobResult {
return f(c, job)
}
// Job represents the beanstalkd job
type Job struct {
Id uint64
Data JobData
}
// JobRequest represents job request
type JobRequest struct {
Type JobType
Data JobData
Priority uint32
Delay time.Duration
TTR time.Duration
}
// JobBroker represents central contact point for worker and job poster
type JobBroker interface {
// RegisterWorker registers the worker and a new connection to a tube is created
RegisterWorker(w Worker, jobType JobType, reservationTimeout time.Duration)
// PostJob puts a job request to be dispatched to matched workers
PostJob(*JobRequest) error
}