-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.go
More file actions
54 lines (43 loc) · 1.33 KB
/
functions.go
File metadata and controls
54 lines (43 loc) · 1.33 KB
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
package work
import "log"
type WorkFunction func(job Job, workerContext *Context) error
type JobErrorFunction func(job Job, workerContext *Context, err error)
type LogFunction func(format string, a ...interface{}) (n int, err error)
func NoLogFunction(format string, a ...interface{}) (n int, err error) {
return 0, nil
}
func PrintlnFunction(format string, a ...interface{}) (n int, err error) {
log.Printf(format+"\n", a...)
return 0, nil
}
func JobErrorsIgnoreFunction(job Job, err error) {
}
func JobErrorsFatalLogFunction(job Job, workerContext *Context, err error) {
log.Fatal("job encountered fatal error: " + err.Error())
}
// MutexFunction is a function that will only ever be run at most once at any given time
type MutexFunction struct {
dispatcher *Dispatcher
}
func NewMutexFunction(
handler func(data interface{}) error,
errFn func(interface{}, error),
) *MutexFunction {
s := MutexFunction{
dispatcher: NewDispatcher(1, 1, func(job Job, workerContext *Context) error {
return handler(job.Context)
}).WithJobErrFn(func(job Job, workerContext *Context, err error) {
errFn(job.Context, err)
}),
}
return &s
}
func (m *MutexFunction) Call(data interface{}) error {
m.dispatcher.EnqueueJobAllowWait(Job{
Context: data,
})
return nil
}
func (m *MutexFunction) WaitUntilIdle() {
m.dispatcher.WaitUntilIdle()
}