-
Notifications
You must be signed in to change notification settings - Fork 1
/
job.go
42 lines (33 loc) · 1.2 KB
/
job.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
package gearman // import "github.com/nathanaelle/gearman/v2"
import (
"errors"
"io"
)
type (
// Job describe a job exposed by Worker
Job interface {
Serve(payload io.Reader, reply, data io.Writer, progress chan<- int) (success bool, err error)
}
// JobHandler is a light version of a Job
JobHandler func(payload io.Reader, reply io.Writer) (err error)
// LongJobHandler is the heavy version (and maybe long in time) of a Job
LongJobHandler func(payload io.Reader, reply, data io.Writer, progress chan<- int) (success bool, err error)
)
// FailJob is a Failed Job
var FailJob Job = JobHandler(func(payload io.Reader, reply io.Writer) (err error) {
return errors.New("job doesn't exist")
})
// Serve is the the implementation of Job.Serve
func (jh JobHandler) Serve(payload io.Reader, reply, data io.Writer, progress chan<- int) (success bool, err error) {
close(progress)
err = jh(payload, reply)
if err == nil {
return true, nil
}
return false, err
}
// Serve is the the implementation of Job.Serve
func (jh LongJobHandler) Serve(payload io.Reader, reply io.Writer, data io.Writer, progress chan<- int) (success bool, err error) {
defer close(progress)
return jh(payload, reply, data, progress)
}