-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_function.go
58 lines (50 loc) · 1.44 KB
/
job_function.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
package job_function
import (
"context"
"fmt"
q "github.com/reugn/go-quartz/quartz"
)
// Function represents an argument-less function which returns a generic type R and a possible error.
type Function[R any] func(context.Context) (R, error)
// FunctionJob represents a Job that invokes the passed Function, implements the quartz.Job interface.
type FunctionJob[R any] struct {
function *Function[R]
desc string
Result *R
Error error
JobStatus q.JobStatus
key int
}
// NewFunctionJob returns a new FunctionJob without an explicit description.
func NewFunctionJobWithKey[R any](key int, function Function[R]) *FunctionJob[R] {
return &FunctionJob[R]{
function: &function,
desc: fmt.Sprintf("FunctionJob:%p", &function),
Result: nil,
Error: nil,
JobStatus: q.NA,
key: key,
}
}
// Description returns the description of the FunctionJob.
func (f *FunctionJob[R]) Description() string {
return f.desc
}
// Key returns the unique FunctionJob key.
func (f *FunctionJob[R]) Key() int {
return f.key
}
// Execute is called by a Scheduler when the Trigger associated with this job fires.
// It invokes the held function, setting the results in Result and Error members.
func (f *FunctionJob[R]) Execute(ctx context.Context) {
result, err := (*f.function)(ctx)
if err != nil {
f.JobStatus = q.FAILURE
f.Result = nil
f.Error = err
} else {
f.JobStatus = q.OK
f.Error = nil
f.Result = &result
}
}