/
function_job.go
65 lines (57 loc) · 1.66 KB
/
function_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package quartz
import (
"context"
"fmt"
)
// 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 JobStatus
}
// NewFunctionJob returns a new FunctionJob without an explicit description.
func NewFunctionJob[R any](function Function[R]) *FunctionJob[R] {
return &FunctionJob[R]{
function: &function,
desc: fmt.Sprintf("FunctionJob:%p", &function),
Result: nil,
Error: nil,
JobStatus: NA,
}
}
// NewFunctionJob returns a new FunctionJob with an explicit description.
func NewFunctionJobWithDesc[R any](desc string, function Function[R]) *FunctionJob[R] {
return &FunctionJob[R]{
function: &function,
desc: desc,
Result: nil,
Error: nil,
JobStatus: NA,
}
}
// 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 HashCode(fmt.Sprintf("%s:%p", f.desc, f.function))
}
// 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 = FAILURE
f.Result = nil
f.Error = err
} else {
f.JobStatus = OK
f.Error = nil
f.Result = &result
}
}