Skip to content

Commit

Permalink
update readme, examples, naming
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRoesler committed Jan 17, 2024
1 parent 61c121f commit 2d8daf5
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 80 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,12 @@ Multiple instances of gocron can be run.
- [**Elector**](https://pkg.go.dev/github.com/go-co-op/gocron/v2#WithDistributedElector):
An elector can be used to elect a single instance of gocron to run as the primary with the
other instances checking to see if a new leader needs to be elected.
- Implementations: [go-co-op electors](https://github.com/go-co-op?q=-elector&type=all&language=&sort=)
- Implementations: [go-co-op electors](https://github.com/go-co-op?q=-elector&type=all&language=&sort=)
(don't see what you need? request on slack to get a repo created to contribute it!)
- [**Locker**](https://pkg.go.dev/github.com/go-co-op/gocron/v2#WithDistributedLocker):
A locker can be used to lock each run of a job to a single instance of gocron.
- Implementations: [go-co-op lockers](https://github.com/go-co-op?q=-lock&type=all&language=&sort=)
- Implementations: [go-co-op lockers](https://github.com/go-co-op?q=-lock&type=all&language=&sort=)
(don't see what you need? request on slack to get a repo created to contribute it!)

### Events
Job events can trigger actions.
Expand All @@ -141,9 +143,11 @@ The Logger interface can be implemented with your desired logging library.
The provided NewLogger uses the standard library's log package.

### Metrics
Execution jobs metrics can be collect.
Metrics may be collected from the execution of each job.
- [**Monitor**](https://pkg.go.dev/github.com/go-co-op/gocron/v2#Monitor):
A monitor can be used to collect metrics a instance of gocron each run of job.
A monitor can be used to collect metrics for each job from a scheduler.
- Implementations: [go-co-op monitors](https://github.com/go-co-op?q=-monitor&type=all&language=&sort=)
(don't see what you need? request on slack to get a repo created to contribute it!)

### Testing
The gocron library is set up to enable testing.
Expand Down
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ var (
ErrWithLimitConcurrentJobsZero = fmt.Errorf("gocron: WithLimitConcurrentJobs: limit must be greater than 0")
ErrWithLocationNil = fmt.Errorf("gocron: WithLocation: location must not be nil")
ErrWithLoggerNil = fmt.Errorf("gocron: WithLogger: logger must not be nil")
ErrWithMonitorNil = fmt.Errorf("gocron: WithMonitor: monitor must not be nil")
ErrWithNameEmpty = fmt.Errorf("gocron: WithName: name must not be empty")
ErrWithStartDateTimePast = fmt.Errorf("gocron: WithStartDateTime: start must not be in the past")
ErrWithStopTimeoutZeroOrNegative = fmt.Errorf("gocron: WithStopTimeout: timeout must be greater than 0")
ErrWithMonitorNil = fmt.Errorf("gocron: WithMonitor: monitor must not be nil")
)

// internal errors
Expand Down
129 changes: 63 additions & 66 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,69 @@ func ExampleWithLogger() {
)
}

func ExampleWithMonitor() {
//type exampleMonitor struct {
// mu sync.Mutex
// counter map[string]int
// time map[string][]time.Duration
//}
//
//func newExampleMonitor() *exampleMonitor {
// return &exampleMonitor{
// counter: make(map[string]int),
// time: make(map[string][]time.Duration),
//}
//}
//
//func (t *exampleMonitor) JobRunInc(_ uuid.UUID, name string, _ []string, _ JobStatus) {
// t.mu.Lock()
// defer t.mu.Unlock()
// _, ok := t.counter[name]
// if !ok {
// t.counter[name] = 0
// }
// t.counter[name]++
//}
//
//func (t *exampleMonitor) JobRunTiming(startTime, endTime time.Time, _ uuid.UUID, name string, _ []string) {
// t.mu.Lock()
// defer t.mu.Unlock()
// _, ok := t.time[name]
// if !ok {
// t.time[name] = make([]time.Duration, 0)
// }
// t.time[name] = append(t.time[name], endTime.Sub(startTime))
//}
//
//monitor := newExampleMonitor()
//s, _ := NewScheduler(
// WithMonitor(monitor),
//)
//name := "example"
//_, _ = s.NewJob(
// DurationJob(
// time.Second,
// ),
// NewTask(
// func() {
// time.Sleep(1 * time.Second)
// },
// ),
// WithName(name),
// WithStartAt(
// WithStartImmediately(),
// ),
//)
//s.Start()
//time.Sleep(5 * time.Second)
//_ = s.Shutdown()
//
//fmt.Printf("Job %q total execute count: %d\n", name, monitor.counter[name])
//for i, val := range monitor.time[name] {
// fmt.Printf("Job %q execute #%d elapsed %.4f seconds\n", name, i+1, val.Seconds())
//}
}

func ExampleWithName() {
s, _ := NewScheduler()
defer func() { _ = s.Shutdown() }()
Expand Down Expand Up @@ -780,69 +843,3 @@ func ExampleWithTags() {
// Output:
// [tag1 tag2 tag3]
}

/*
type exampleMonitor struct {
mu sync.Mutex
counter map[string]int
time map[string][]time.Duration
}
func newExampleMonitor() *exampleMonitor {
return &exampleMonitor{
counter: make(map[string]int),
time: make(map[string][]time.Duration),
}
}
func (t *exampleMonitor) Inc(_ uuid.UUID, name string, _ []string, _ JobStatus) {
t.mu.Lock()
defer t.mu.Unlock()
_, ok := t.counter[name]
if !ok {
t.counter[name] = 0
}
t.counter[name]++
}
func (t *exampleMonitor) WriteTiming(startTime, endTime time.Time, _ uuid.UUID, name string, _ []string) {
t.mu.Lock()
defer t.mu.Unlock()
_, ok := t.time[name]
if !ok {
t.time[name] = make([]time.Duration, 0)
}
t.time[name] = append(t.time[name], endTime.Sub(startTime))
}
func ExampleWithMonitorer() {
monitor := newExampleMonitor()
s, _ := NewScheduler(
WithMonitor(monitor),
)
name := "example"
_, _ = s.NewJob(
DurationJob(
time.Second,
),
NewTask(
func() {
time.Sleep(1 * time.Second)
},
),
WithName(name),
WithStartAt(
WithStartImmediately(),
),
)
s.Start()
time.Sleep(5 * time.Second)
_ = s.Shutdown()
fmt.Printf("Job %q total execute count: %d\n", name, monitor.counter[name])
for i, val := range monitor.time[name] {
fmt.Printf("Job %q execute #%d elapsed %.4f seconds\n", name, i+1, val.Seconds())
}
}
*/
6 changes: 3 additions & 3 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,17 +354,17 @@ func (e *executor) runJob(j internalJob, shouldSendOut bool) {
startTime := time.Now()
err := callJobFuncWithParams(j.function, j.parameters...)
if e.monitor != nil {
e.monitor.WriteTiming(startTime, time.Now(), j.id, j.name, j.tags)
e.monitor.JobRunTiming(startTime, time.Now(), j.id, j.name, j.tags)
}
if err != nil {
_ = callJobFuncWithParams(j.afterJobRunsWithError, j.id, j.name, err)
if e.monitor != nil {
e.monitor.Inc(j.id, j.name, j.tags, Fail)
e.monitor.JobRunInc(j.id, j.name, j.tags, Fail)
}
} else {
_ = callJobFuncWithParams(j.afterJobRuns, j.id, j.name)
if e.monitor != nil {
e.monitor.Inc(j.id, j.name, j.tags, Success)
e.monitor.JobRunInc(j.id, j.name, j.tags, Success)
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions metrics.go → monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"github.com/google/uuid"
)

// JobStatus is the status of job run that should be collect
// with metric.
// JobStatus is the status of job run that should be collected with the metric.
type JobStatus string

// The different statuses of job that can be used.
Expand All @@ -18,6 +17,6 @@ const (

// Monitor represents the interface to collect jobs metrics.
type Monitor interface {
Inc(id uuid.UUID, name string, tags []string, status JobStatus)
WriteTiming(startTime, endTime time.Time, id uuid.UUID, name string, tags []string)
JobRunInc(id uuid.UUID, name string, tags []string, status JobStatus)
JobRunTiming(startTime, endTime time.Time, id uuid.UUID, name string, tags []string)
}
4 changes: 2 additions & 2 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,7 @@ func newTestMonitor() *testMonitor {
}
}

func (t *testMonitor) Inc(_ uuid.UUID, name string, _ []string, _ JobStatus) {
func (t *testMonitor) JobRunInc(_ uuid.UUID, name string, _ []string, _ JobStatus) {
t.mu.Lock()
defer t.mu.Unlock()
_, ok := t.counter[name]
Expand All @@ -1684,7 +1684,7 @@ func (t *testMonitor) Inc(_ uuid.UUID, name string, _ []string, _ JobStatus) {
t.counter[name]++
}

func (t *testMonitor) WriteTiming(startTime, endTime time.Time, _ uuid.UUID, name string, _ []string) {
func (t *testMonitor) JobRunTiming(startTime, endTime time.Time, _ uuid.UUID, name string, _ []string) {
t.mu.Lock()
defer t.mu.Unlock()
_, ok := t.time[name]
Expand Down

0 comments on commit 2d8daf5

Please sign in to comment.