From f19b2fe5feca5e0f313ca8c9a17e02d251c13cb1 Mon Sep 17 00:00:00 2001 From: Alessio Gottardo Date: Wed, 6 Jun 2018 17:57:56 +0100 Subject: [PATCH] Provide a consumable channel with custom defined execution stats --- crontab.go | 22 +++++++++++++++++++--- example_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/crontab.go b/crontab.go index 42e811e..4422f6c 100644 --- a/crontab.go +++ b/crontab.go @@ -11,10 +11,20 @@ import ( "time" ) +// StatsFunc func representing custom execution statistics +type StatsFunc func() interface{} + +// ExecStats struct representing a standardized wrapper for execution statistics +type ExecStats struct { + JobType string + Stats StatsFunc +} + // Crontab struct representing cron table type Crontab struct { - ticker *time.Ticker - jobs []job + ticker *time.Ticker + jobs []job + statsChan chan ExecStats } // job in cron table @@ -46,7 +56,8 @@ func New() *Crontab { // new creates new crontab, arg provided for testing purpose func new(t time.Duration) *Crontab { c := &Crontab{ - ticker: time.NewTicker(t), + ticker: time.NewTicker(t), + statsChan: make(chan ExecStats), } go func() { @@ -58,6 +69,11 @@ func new(t time.Duration) *Crontab { return c } +// StatsChan returns the channel to consume execution statistics +func (c *Crontab) StatsChan() chan ExecStats { + return c.statsChan +} + // AddJob to cron table // // Returns error if: diff --git a/example_test.go b/example_test.go index 2867ef8..281aaf4 100644 --- a/example_test.go +++ b/example_test.go @@ -3,6 +3,8 @@ package crontab_test import ( "fmt" "log" + "testing" + "time" "github.com/mileusna/crontab" ) @@ -38,3 +40,50 @@ func myFunc3() { func myFunc2(s string, n int) { fmt.Printf("We have params here, string `%s` and nymber %d\n", s, n) } + +// go test -v ./... -run TestExampleExecStats +func TestExampleExecStats(t *testing.T) { + ctab := crontab.New() + + ctab.MustAddJob("* * * * *", myFuncWithStats, ctab.StatsChan()) + log.Println("Waiting a bit for the test to complete...") + + for i := 1; i <= 1; i++ { + myExecStats := <-ctab.StatsChan() + if myExecStats.JobType != "myFuncWithStats" { + t.Errorf("Found an unexpected Job type") + } + customStuff := myExecStats.Stats().(*myCustomStats) + if customStuff.strParam != "foo" { + t.Errorf("Found an unexpected string parameter in the stats") + } + if customStuff.intParam != 42 { + t.Errorf("Found an unexpected integer parameter in the stats") + } + ctab.Shutdown() + log.Println("Done with the test, the received stats:", customStuff) + } +} + +// custom execution stats depending on the scheduled function +type myCustomStats struct { + strParam string + intParam int +} + +func myFuncWithStats(statsChan chan crontab.ExecStats) { + // work a bit... + time.Sleep(1 * time.Second) + // publish the execution stats... + statsChan <- crontab.ExecStats{ + // ID to identify the job + JobType: "myFuncWithStats", + // custom execution stats + Stats: func() interface{} { + return &myCustomStats{ + strParam: "foo", + intParam: 42, + } + }, + } +}