support go 1.20+ (#608)

This commit is contained in:
John Roesler 2023-11-09 09:49:21 -06:00 committed by GitHub
parent 584cd8aeca
commit 6f9a8200f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 53 additions and 44 deletions

View File

@ -12,6 +12,7 @@ jobs:
strategy: strategy:
matrix: matrix:
go-version: go-version:
- "1.20"
- "1.21" - "1.21"
name: lint and test name: lint and test
runs-on: ubuntu-latest runs-on: ubuntu-latest

View File

@ -2,7 +2,6 @@ package gocron_test
import ( import (
"fmt" "fmt"
"log/slog"
"sync" "sync"
"time" "time"
@ -550,7 +549,7 @@ func ExampleWithLocation() {
func ExampleWithLogger() { func ExampleWithLogger() {
_, _ = NewScheduler( _, _ = NewScheduler(
WithLogger( WithLogger(
NewJSONSlogLogger(slog.LevelInfo), NewLogger(LogLevelDebug),
), ),
) )
} }

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/go-co-op/gocron/v2 module github.com/go-co-op/gocron/v2
go 1.21 go 1.20
require ( require (
github.com/google/uuid v1.4.0 github.com/google/uuid v1.4.0

2
job.go
View File

@ -5,13 +5,13 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/rand" "math/rand"
"slices"
"strings" "strings"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/jonboulle/clockwork" "github.com/jonboulle/clockwork"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
"golang.org/x/exp/slices"
) )
// internalJob stores the information needed by the scheduler // internalJob stores the information needed by the scheduler

View File

@ -1,8 +1,7 @@
package gocron package gocron
import ( import (
"log/slog" "log"
"os"
) )
// Logger is the interface that wraps the basic logging methods // Logger is the interface that wraps the basic logging methods
@ -12,56 +11,67 @@ import (
// or implement your own Logger. The actual level of Log that is logged // or implement your own Logger. The actual level of Log that is logged
// is handled by the implementation. // is handled by the implementation.
type Logger interface { type Logger interface {
Debug(msg string, args ...any) Debug(msg string, args ...interface{})
Error(msg string, args ...any) Error(msg string, args ...interface{})
Info(msg string, args ...any) Info(msg string, args ...interface{})
Warn(msg string, args ...any) Warn(msg string, args ...interface{})
} }
var _ Logger = (*noOpLogger)(nil) var _ Logger = (*noOpLogger)(nil)
type noOpLogger struct{} type noOpLogger struct{}
func (l noOpLogger) Debug(_ string, _ ...any) {} func (l noOpLogger) Debug(_ string, _ ...interface{}) {}
func (l noOpLogger) Error(_ string, _ ...any) {} func (l noOpLogger) Error(_ string, _ ...interface{}) {}
func (l noOpLogger) Info(_ string, _ ...any) {} func (l noOpLogger) Info(_ string, _ ...interface{}) {}
func (l noOpLogger) Warn(_ string, _ ...any) {} func (l noOpLogger) Warn(_ string, _ ...interface{}) {}
var _ Logger = (*slogLogger)(nil) var _ Logger = (*logger)(nil)
type slogLogger struct { // LogLevel is the level of logging that should be logged
sl *slog.Logger // when using the basic NewLogger.
} type LogLevel int
func NewJSONSlogLogger(level slog.Level) Logger { const (
return NewSlogLogger( LogLevelError LogLevel = iota
slog.New( LogLevelWarn
slog.NewJSONHandler( LogLevelInfo
os.Stdout, LogLevelDebug
&slog.HandlerOptions{
Level: level,
},
),
),
) )
type logger struct {
level LogLevel
} }
func NewSlogLogger(sl *slog.Logger) Logger { // NewLogger returns a new Logger that logs at the given level.
return &slogLogger{sl: sl} func NewLogger(level LogLevel) Logger {
return &logger{level: level}
} }
func (l *slogLogger) Debug(msg string, args ...any) { func (l *logger) Debug(msg string, args ...interface{}) {
l.sl.Debug(msg, args...) if l.level < LogLevelDebug {
return
}
log.Printf("DEBUG: %s, %v\n", msg, args)
} }
func (l *slogLogger) Error(msg string, args ...any) { func (l *logger) Error(msg string, args ...interface{}) {
l.sl.Error(msg, args...) if l.level < LogLevelError {
return
}
log.Printf("ERROR: %s, %v\n", msg, args)
} }
func (l *slogLogger) Info(msg string, args ...any) { func (l *logger) Info(msg string, args ...interface{}) {
l.sl.Info(msg, args...) if l.level < LogLevelInfo {
return
}
log.Printf("INFO: %s, %v\n", msg, args)
} }
func (l *slogLogger) Warn(msg string, args ...any) { func (l *logger) Warn(msg string, args ...interface{}) {
l.sl.Warn(msg, args...) if l.level < LogLevelWarn {
return
}
log.Printf("WARN: %s, %v\n", msg, args)
} }

View File

@ -3,11 +3,11 @@ package gocron
import ( import (
"context" "context"
"reflect" "reflect"
"slices"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/jonboulle/clockwork" "github.com/jonboulle/clockwork"
"golang.org/x/exp/slices"
) )
var _ Scheduler = (*scheduler)(nil) var _ Scheduler = (*scheduler)(nil)

View File

@ -3,7 +3,6 @@ package gocron
import ( import (
"context" "context"
"fmt" "fmt"
"log/slog"
"sync" "sync"
"testing" "testing"
"time" "time"
@ -16,7 +15,7 @@ import (
func newTestScheduler(options ...SchedulerOption) (Scheduler, error) { func newTestScheduler(options ...SchedulerOption) (Scheduler, error) {
// default test options // default test options
out := []SchedulerOption{ out := []SchedulerOption{
WithLogger(NewJSONSlogLogger(slog.LevelDebug)), WithLogger(NewLogger(LogLevelDebug)),
WithStopTimeout(time.Second), WithStopTimeout(time.Second),
} }

View File

@ -3,12 +3,12 @@ package gocron
import ( import (
"context" "context"
"reflect" "reflect"
"slices"
"sync" "sync"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"golang.org/x/exp/maps" "golang.org/x/exp/maps"
"golang.org/x/exp/slices"
) )
func callJobFuncWithParams(jobFunc any, params ...any) error { func callJobFuncWithParams(jobFunc any, params ...any) error {