Skip to content

Commit

Permalink
refactor: replace scheduler with robfig/cron
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed Apr 9, 2021
1 parent 0b3943f commit 62cab3c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 37 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/macrat/ayd
go 1.16

require (
github.com/go-co-op/gocron v1.2.0
github.com/go-ping/ping v0.0.0-20210402232549-1726e5ede5b6
github.com/robfig/cron v1.2.0
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect
)
17 changes: 2 additions & 15 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-co-op/gocron v1.2.0 h1:5FQwO7dPnHr8JCPqjfN1WKgXSXIO8hmwnfcx5AtJ9nM=
github.com/go-co-op/gocron v1.2.0/go.mod h1:6szfspLBBaQ4QHpJrKEbKQnYYa/S8pct51SjLRMefMM=
github.com/go-ping/ping v0.0.0-20210402232549-1726e5ede5b6 h1:n9JlStpjoY3CbO31jfwsAiHqCMbOk/Sk4Unq2pGjc2A=
github.com/go-ping/ping v0.0.0-20210402232549-1726e5ede5b6/go.mod h1:35JbSyV/BYqHwwRA6Zr1uVDm1637YlNOU61wI797NPI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -28,7 +19,3 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
24 changes: 12 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
"os"
"sync"
"sync/atomic"
"time"

"github.com/go-co-op/gocron"
"github.com/macrat/ayd/exporter"
"github.com/macrat/ayd/probe"
"github.com/macrat/ayd/store"
"github.com/robfig/cron"
)

var (
Expand Down Expand Up @@ -77,19 +76,19 @@ func Usage() {
}

type Task struct {
Interval time.Duration
Schedule Schedule
Probe probe.Probe
}

func ParseArgs(args []string) ([]Task, []error) {
var result []Task
var errors []error

interval := 5 * time.Minute
schedule := DEFAULT_SCHEDULE

for _, a := range args {
if d, err := time.ParseDuration(a); err == nil {
interval = d
if s, err := ParseSimpleSchedule(a); err == nil {
schedule = s
continue
}

Expand All @@ -100,7 +99,7 @@ func ParseArgs(args []string) ([]Task, []error) {
}

result = append(result, Task{
Interval: interval,
Schedule: schedule,
Probe: p,
})
}
Expand Down Expand Up @@ -134,14 +133,14 @@ func RunOneshot(tasks []Task) {
}

func RunServer(tasks []Task) {
scheduler := gocron.NewScheduler(time.UTC)
scheduler := cron.New()
store := store.New(*storePath)

for _, t := range tasks {
f := t.Probe.Check
scheduler.Every(t.Interval).Do(func() {
scheduler.Schedule(t.Schedule, cron.FuncJob(func() {
store.Append(f())
})
}))
}

fmt.Printf("restore check history from %s...\n", *storePath)
Expand All @@ -150,8 +149,9 @@ func RunServer(tasks []Task) {
os.Exit(1)
}

fmt.Printf("start status checking to %d targets...\n", scheduler.Len())
scheduler.StartAsync()
fmt.Printf("start status checking to %d targets...\n", len(tasks))
scheduler.Start()
defer scheduler.Stop()

listen := fmt.Sprintf("0.0.0.0:%d", *listenPort)
fmt.Printf("start status page on http://%s...\n", listen)
Expand Down
17 changes: 8 additions & 9 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package main_test

import (
"testing"
"time"

"github.com/macrat/ayd"
)

func TestParseArgs(t *testing.T) {
type WantTask struct {
Interval time.Duration
Schedule string
Target string
}

Expand All @@ -20,16 +19,16 @@ func TestParseArgs(t *testing.T) {
{
Args: []string{"hoge", "2m", "http://example.com"},
Want: []WantTask{
{5 * time.Minute, "ping:hoge"},
{2 * time.Minute, "http://example.com"},
{"5m0s", "ping:hoge"},
{"2m0s", "http://example.com"},
},
},
{
Args: []string{"hoge", "fuga", "2m", "1h", "http://example.com"},
Want: []WantTask{
{5 * time.Minute, "ping:hoge"},
{5 * time.Minute, "ping:fuga"},
{1 * time.Hour, "http://example.com"},
{"5m0s", "ping:hoge"},
{"5m0s", "ping:fuga"},
{"1h0m0s", "http://example.com"},
},
},
}
Expand All @@ -53,8 +52,8 @@ func TestParseArgs(t *testing.T) {
t.Errorf("%#v: unexpected target at %d: expected %#v but got %#v", tt.Args, i, tt.Want[i].Target, result[i].Probe.Target().String())
}

if result[i].Interval != tt.Want[i].Interval {
t.Errorf("%#v: unexpected interval at %d: expected %s but got %s", tt.Args, i, tt.Want[i].Interval, result[i].Interval)
if result[i].Schedule.String() != tt.Want[i].Schedule {
t.Errorf("%#v: unexpected interval at %d: expected %s but got %s", tt.Args, i, tt.Want[i].Schedule, result[i].Schedule)
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions schedule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"time"

"github.com/robfig/cron"
)

var (
DEFAULT_SCHEDULE = SimpleSchedule(cron.Every(5 * time.Minute))
)

type Schedule interface {
cron.Schedule
fmt.Stringer
}

type SimpleSchedule cron.ConstantDelaySchedule

func ParseSimpleSchedule(s string) (SimpleSchedule, error) {
if d, err := time.ParseDuration(s); err != nil {
return SimpleSchedule{}, err
} else {
return SimpleSchedule(cron.Every(d)), nil
}
}

func (s SimpleSchedule) Next(t time.Time) time.Time {
return cron.ConstantDelaySchedule(s).Next(t)
}

func (s SimpleSchedule) String() string {
return s.Delay.String()
}

0 comments on commit 62cab3c

Please sign in to comment.