-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from reugn/develop
v0.5.0
- Loading branch information
Showing
9 changed files
with
121 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/reugn/go-quartz | ||
|
||
go 1.13 | ||
go 1.18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package quartz | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Function represents an argument-less function which returns a generic type R and a possible error. | ||
type Function[R any] func() (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() { | ||
result, err := (*f.function)() | ||
if err != nil { | ||
f.JobStatus = FAILURE | ||
f.Result = nil | ||
f.Error = err | ||
} else { | ||
f.JobStatus = OK | ||
f.Error = nil | ||
f.Result = &result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package quartz_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/reugn/go-quartz/quartz" | ||
) | ||
|
||
func TestFunctionJob(t *testing.T) { | ||
var n = 2 | ||
funcJob1 := quartz.NewFunctionJob(func() (string, error) { | ||
n += 2 | ||
return "fired1", nil | ||
}) | ||
|
||
funcJob2 := quartz.NewFunctionJob(func() (int, error) { | ||
n += 2 | ||
return 42, nil | ||
}) | ||
|
||
sched := quartz.NewStdScheduler() | ||
sched.Start() | ||
sched.ScheduleJob(funcJob1, quartz.NewRunOnceTrigger(time.Millisecond*300)) | ||
sched.ScheduleJob(funcJob2, quartz.NewRunOnceTrigger(time.Millisecond*800)) | ||
time.Sleep(time.Second) | ||
sched.Clear() | ||
sched.Stop() | ||
|
||
assertEqual(t, funcJob1.JobStatus, quartz.OK) | ||
assertNotEqual(t, funcJob1.Result, nil) | ||
assertEqual(t, *funcJob1.Result, "fired1") | ||
|
||
assertEqual(t, funcJob2.JobStatus, quartz.OK) | ||
assertNotEqual(t, funcJob2.Result, nil) | ||
assertEqual(t, *funcJob2.Result, 42) | ||
|
||
assertEqual(t, n, 6) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters