added crons web apis and ui listing
This commit is contained in:
+1
-1
@@ -21,8 +21,8 @@ type Cron struct {
|
||||
timezone *time.Location
|
||||
ticker *time.Ticker
|
||||
startTimer *time.Timer
|
||||
jobs []*Job
|
||||
tickerDone chan bool
|
||||
jobs []*Job
|
||||
interval time.Duration
|
||||
mux sync.RWMutex
|
||||
}
|
||||
|
||||
+18
-2
@@ -1,5 +1,7 @@
|
||||
package cron
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// Job defines a single registered cron job.
|
||||
type Job struct {
|
||||
fn func()
|
||||
@@ -12,8 +14,8 @@ func (j *Job) Id() string {
|
||||
return j.id
|
||||
}
|
||||
|
||||
// Expr returns the plain cron job schedule expression.
|
||||
func (j *Job) Expr() string {
|
||||
// Expression returns the plain cron job schedule expression.
|
||||
func (j *Job) Expression() string {
|
||||
return j.schedule.rawExpr
|
||||
}
|
||||
|
||||
@@ -23,3 +25,17 @@ func (j *Job) Run() {
|
||||
j.fn()
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalJSON implements [json.Marshaler] and export the current
|
||||
// jobs data into valid JSON.
|
||||
func (j Job) MarshalJSON() ([]byte, error) {
|
||||
plain := struct {
|
||||
Id string `json:"id"`
|
||||
Expression string `json:"expression"`
|
||||
}{
|
||||
Id: j.Id(),
|
||||
Expression: j.Expression(),
|
||||
}
|
||||
|
||||
return json.Marshal(plain)
|
||||
}
|
||||
|
||||
+25
-3
@@ -1,6 +1,9 @@
|
||||
package cron
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestJobId(t *testing.T) {
|
||||
expected := "test"
|
||||
@@ -22,8 +25,8 @@ func TestJobExpr(t *testing.T) {
|
||||
|
||||
j := Job{schedule: s}
|
||||
|
||||
if j.Expr() != expected {
|
||||
t.Fatalf("Expected job with cron expression %q, got %q", expected, j.Expr())
|
||||
if j.Expression() != expected {
|
||||
t.Fatalf("Expected job with cron expression %q, got %q", expected, j.Expression())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,3 +50,22 @@ func TestJobRun(t *testing.T) {
|
||||
t.Fatalf("Expected calls %q, got %q", expected, calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJobMarshalJSON(t *testing.T) {
|
||||
s, err := NewSchedule("1 2 3 4 5")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
j := Job{id: "test_id", schedule: s}
|
||||
|
||||
raw, err := json.Marshal(j)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expected := `{"id":"test_id","expression":"1 2 3 4 5"}`
|
||||
if str := string(raw); str != expected {
|
||||
t.Fatalf("Expected\n%s\ngot\n%s", expected, str)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user