From 31a0f0f64de44ae7709064e85c034d0766272045 Mon Sep 17 00:00:00 2001 From: "R.I.Pienaar" Date: Sun, 18 Dec 2022 12:14:00 +0100 Subject: [PATCH] (misc) update to go 1.18 Signed-off-by: R.I.Pienaar --- .github/workflows/test.yaml | 2 +- README.md | 2 +- ajc/task_cron_command.go | 2 +- ajc/util.go | 2 +- client.go | 2 +- client_examples_test.go | 6 ++--- client_test.go | 7 +++--- docs/content/_index.md | 2 +- docs/content/overview/golang-overview.md | 4 ++-- docs/content/overview/handlers-docker.md | 2 +- .../reference/routing-concurrency-retry.md | 2 +- docs/content/reference/terminology.md | 2 +- election/election.go | 2 +- election/election_test.go | 7 +++--- election/options.go | 4 ++-- generators/godocker.go | 2 +- go.mod | 3 +++ go.sum | 13 ++++++++++ lifecycle.go | 2 +- logger.go | 24 +++++++++---------- mux.go | 6 ++--- mux_test.go | 10 ++++---- processor.go | 2 +- processor_test.go | 10 ++++---- request_reply_handler.go | 2 +- request_reply_handler_test.go | 2 +- scheduled_task.go | 2 +- task.go | 8 +++---- tools.go | 19 +++++++++++++++ 29 files changed, 93 insertions(+), 60 deletions(-) create mode 100644 tools.go diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index e5412b2..0e40880 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -5,7 +5,7 @@ jobs: test: strategy: matrix: - go: [ 1.17, 1.18 ] + go: [ 1.18, 1.19 ] runs-on: ubuntu-latest steps: diff --git a/README.md b/README.md index 32eae25..36a3b05 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ client, _ := asyncjobs.NewClient( asyncjobs.RetryBackoffPolicy(asyncjobs.RetryLinearTenMinutes)) router := asyncjobs.NewTaskRouter() -router.Handler("email:new", func(ctx context.Context, log asyncjobs.Logger, task *asyncjobs.Task) (interface{}, error) { +router.Handler("email:new", func(ctx context.Context, log asyncjobs.Logger, task *asyncjobs.Task) (any, error) { log.Printf("Processing task %s", task.ID) // do work here using task.Payload diff --git a/ajc/task_cron_command.go b/ajc/task_cron_command.go index 85ac842..289642c 100644 --- a/ajc/task_cron_command.go +++ b/ajc/task_cron_command.go @@ -201,7 +201,7 @@ func (c *taskCronCommand) addAction(_ *fisk.ParseContext) error { opts = append(opts, aj.TaskMaxTries(c.maxtries)) } - var payload interface{} + var payload any if c.payload != "" { payload = c.payload } diff --git a/ajc/util.go b/ajc/util.go index 37e4843..7903836 100644 --- a/ajc/util.go +++ b/ajc/util.go @@ -105,7 +105,7 @@ func humanizeDuration(d time.Duration) string { return fmt.Sprintf("%.2fs", d.Seconds()) } -func dumpJSON(d interface{}) { +func dumpJSON(d any) { j, err := json.MarshalIndent(d, "", " ") if err != nil { panic(fmt.Sprintf("could not JSON render: %v", err)) diff --git a/client.go b/client.go index 7f9e40e..f333652 100644 --- a/client.go +++ b/client.go @@ -186,7 +186,7 @@ func (c *Client) saveOrDiscardTaskIfDesired(ctx context.Context, t *Task) error return c.storage.DeleteTaskByID(t.ID) } -func (c *Client) setTaskSuccess(ctx context.Context, t *Task, payload interface{}) error { +func (c *Client) setTaskSuccess(ctx context.Context, t *Task, payload any) error { t.LastTriedAt = nowPointer() t.State = TaskStateCompleted t.LastErr = "" diff --git a/client_examples_test.go b/client_examples_test.go index d30c52a..1fc109a 100644 --- a/client_examples_test.go +++ b/client_examples_test.go @@ -7,8 +7,8 @@ import ( "time" ) -func newEmail(to, subject, body string) map[string]interface{} { - return map[string]interface{}{ +func newEmail(to, subject, body string) map[string]any { + return map[string]any{ "to": to, "subject": subject, "body": body, @@ -71,7 +71,7 @@ func ExampleClient_consumer() { panicIfErr(err) router := NewTaskRouter() - err = router.HandleFunc("email:send", func(_ context.Context, _ Logger, t *Task) (interface{}, error) { + err = router.HandleFunc("email:send", func(_ context.Context, _ Logger, t *Task) (any, error) { log.Printf("Processing task: %s", t.ID) // handle task.Payload which is a JSON encoded email diff --git a/client_test.go b/client_test.go index 21680f2..b91ddd9 100644 --- a/client_test.go +++ b/client_test.go @@ -8,7 +8,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "log" "os" "sync" @@ -29,7 +28,7 @@ func TestAsyncJobs(t *testing.T) { } func withJetStream(cb func(nc *nats.Conn, mgr *jsm.Manager)) { - d, err := ioutil.TempDir("", "jstest") + d, err := os.MkdirTemp("", "jstest") Expect(err).ToNot(HaveOccurred()) defer os.RemoveAll(d) @@ -152,7 +151,7 @@ var _ = Describe("Client", func() { handled := int32(0) router := NewTaskRouter() - router.HandleFunc("test", func(ctx context.Context, log Logger, t *Task) (interface{}, error) { + router.HandleFunc("test", func(ctx context.Context, log Logger, t *Task) (any, error) { if t.Tries > 1 { log.Infof("Try %d for task %s", t.Tries, t.ID) } @@ -212,7 +211,7 @@ var _ = Describe("Client", func() { var tries []time.Time router := NewTaskRouter() - router.HandleFunc("ginkgo", func(ctx context.Context, log Logger, t *Task) (interface{}, error) { + router.HandleFunc("ginkgo", func(ctx context.Context, log Logger, t *Task) (any, error) { tries = append(tries, time.Now()) log.Infof("Trying task %s on try %d\n", t.ID, t.Tries) diff --git a/docs/content/_index.md b/docs/content/_index.md index f99f70d..4c81d57 100644 --- a/docs/content/_index.md +++ b/docs/content/_index.md @@ -40,7 +40,7 @@ client, _ := asyncjobs.NewClient( asyncjobs.RetryBackoffPolicy(asyncjobs.RetryLinearTenMinutes)) router := asyncjobs.NewTaskRouter() -router.Handler("email:new", func(ctx context.Context, log asyncjobs.Logger, task *asyncjobs.Task) (interface{}, error) { +router.Handler("email:new", func(ctx context.Context, log asyncjobs.Logger, task *asyncjobs.Task) (any, error) { log.Printf("Processing task %s", task.ID) // do work here using task.Payload diff --git a/docs/content/overview/golang-overview.md b/docs/content/overview/golang-overview.md index 90b5eba..f27b731 100644 --- a/docs/content/overview/golang-overview.md +++ b/docs/content/overview/golang-overview.md @@ -59,7 +59,7 @@ Any number of producers can create tasks from any number of different processes. First we have a simplistic helper to create a map that describes an email: ```go -func newEmail(to, subject, body string) interface{} { +func newEmail(to, subject, body string) any { return map[string]string{"to": to, "subject": subject, "body": body} } ``` @@ -105,7 +105,7 @@ client, err := asyncjobs.NewClient( panicIfErr(err) router := asyncjobs.NewTaskRouter() -err = router.Handler("email:new", func(ctx context.Context, log asyncjobs.Logger, task *asyncjobs.Task) (interface{}, error) { +err = router.Handler("email:new", func(ctx context.Context, log asyncjobs.Logger, task *asyncjobs.Task) (any, error) { log.Printf("Processing task %s", task.ID) // do work here using task.Payload diff --git a/docs/content/overview/handlers-docker.md b/docs/content/overview/handlers-docker.md index a737724..24ab719 100644 --- a/docs/content/overview/handlers-docker.md +++ b/docs/content/overview/handlers-docker.md @@ -19,7 +19,7 @@ import ( aj "github.com/choria-io/asyncjobs" ) -func AsyncJobHandler(ctx context.Context, log aj.Logger, task *aj.Task) (interface{}, error) { +func AsyncJobHandler(ctx context.Context, log aj.Logger, task *aj.Task) (any, error) { // process your email } ``` diff --git a/docs/content/reference/routing-concurrency-retry.md b/docs/content/reference/routing-concurrency-retry.md index ff842dd..3027d2b 100644 --- a/docs/content/reference/routing-concurrency-retry.md +++ b/docs/content/reference/routing-concurrency-retry.md @@ -17,7 +17,7 @@ Below is a handler that sends an email, the task Payload is a serialized object The Task handler then is a single-purpose piece of code capable of handling 1 type of Task. ```go -func emailNewHandler(ctx context.Context, log asycjobs.Logger, task *asyncjobs.Task) (interface{}, error) { +func emailNewHandler(ctx context.Context, log asycjobs.Logger, task *asyncjobs.Task) (any, error) { // Parse the task payload into an email email, err := parseEmail(task.Payload) if err != nil { return nil, err } diff --git a/docs/content/reference/terminology.md b/docs/content/reference/terminology.md index 94d5765..8208c22 100644 --- a/docs/content/reference/terminology.md +++ b/docs/content/reference/terminology.md @@ -24,7 +24,7 @@ Connects to JetStream and manages the enqueueing and routing of tasks. ## Handler -Handlers are functions that can process a task with the signature `func(context.Context, *asyncjobs.Task) (interface{}, error)`. +Handlers are functions that can process a task with the signature `func(context.Context, *asyncjobs.Task) (any, error)`. ## Router diff --git a/election/election.go b/election/election.go index 684d1da..ac662ba 100644 --- a/election/election.go +++ b/election/election.go @@ -111,7 +111,7 @@ func NewElection(name string, key string, bucket nats.KeyValue, opts ...Option) return e, nil } -func (e *election) debugf(format string, a ...interface{}) { +func (e *election) debugf(format string, a ...any) { if e.opts.debug == nil { return } diff --git a/election/election_test.go b/election/election_test.go index 1005c7e..f5d254e 100644 --- a/election/election_test.go +++ b/election/election_test.go @@ -7,7 +7,6 @@ package election import ( "context" "fmt" - "io/ioutil" "os" "sync" "testing" @@ -31,7 +30,7 @@ var _ = Describe("Leader Election", func() { js nats.KeyValueManager kv nats.KeyValue err error - debugger func(f string, a ...interface{}) + debugger func(f string, a ...any) ) BeforeEach(func() { @@ -45,7 +44,7 @@ var _ = Describe("Leader Election", func() { TTL: 750 * time.Millisecond, }) Expect(err).ToNot(HaveOccurred()) - debugger = func(f string, a ...interface{}) { + debugger = func(f string, a ...any) { fmt.Fprintf(GinkgoWriter, fmt.Sprintf("%s\n", f), a...) } }) @@ -208,7 +207,7 @@ var _ = Describe("Leader Election", func() { func startJSServer(t GinkgoTInterface) (*server.Server, *nats.Conn) { t.Helper() - d, err := ioutil.TempDir("", "jstest") + d, err := os.MkdirTemp("", "jstest") if err != nil { t.Fatalf("temp dir could not be made: %s", err) } diff --git a/election/options.go b/election/options.go index 178bb0e..34fdb34 100644 --- a/election/options.go +++ b/election/options.go @@ -23,7 +23,7 @@ type options struct { lostCb func() campaignCb func(s State) bo Backoff - debug func(format string, a ...interface{}) + debug func(format string, a ...any) } // WithBackoff will use the provided Backoff timer source to decrease campaign intervals over time @@ -47,6 +47,6 @@ func OnCampaign(cb func(s State)) Option { } // WithDebug sets a function to do debug logging with -func WithDebug(cb func(format string, a ...interface{})) Option { +func WithDebug(cb func(format string, a ...any)) Option { return func(o *options) { o.debug = cb } } diff --git a/generators/godocker.go b/generators/godocker.go index 7000540..53e4d76 100644 --- a/generators/godocker.go +++ b/generators/godocker.go @@ -63,7 +63,7 @@ func (g *GoContainer) RenderToDirectory(target string) error { } } - funcs := map[string]interface{}{ + funcs := map[string]any{ "RetryNamesList": func() string { return strings.Join(aj.RetryPolicyNames(), ", ") }, diff --git a/go.mod b/go.mod index d6be2c5..0b2f2f3 100644 --- a/go.mod +++ b/go.mod @@ -24,8 +24,10 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/go-logr/logr v1.2.3 // indirect + github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/google/go-cmp v0.5.9 // indirect + github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/klauspost/compress v1.15.13 // indirect github.com/kr/pretty v0.1.0 // indirect @@ -45,5 +47,6 @@ require ( golang.org/x/sys v0.3.0 // indirect golang.org/x/text v0.5.0 // indirect golang.org/x/time v0.3.0 // indirect + golang.org/x/tools v0.4.0 // indirect google.golang.org/protobuf v1.28.1 // indirect ) diff --git a/go.sum b/go.sum index 41cb2fd..c92862c 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,9 @@ github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/choria-io/fisk v0.2.1 h1:Ful0jPqo5LvDMnkQfTnRWl29NGA2TmL0Qj6UMyQcHyU= github.com/choria-io/fisk v0.2.1/go.mod h1:S+NQ8qIofXvjz4zT4t69xRudzMYNfOG3uC0lOzGNjHA= +github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI= github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -17,6 +20,8 @@ github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4 github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I= +github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= @@ -25,8 +30,11 @@ github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE= +github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog= github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68= +github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/klauspost/compress v1.15.13 h1:NFn1Wr8cfnenSJSA46lLq4wHCcBzKTSjnBIexDMMOV0= @@ -83,6 +91,7 @@ github.com/segmentio/ksuid v1.0.4/go.mod h1:/XUiZBD3kVx5SmUOl55voK5yeAbBNNIed+2O github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= @@ -97,6 +106,7 @@ golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190130150945-aca44879d564/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -113,6 +123,8 @@ golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.4.0 h1:7mTAgkunk3fr4GAloyyCasadO6h9zSsQZbwvcaIciV4= +golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= @@ -120,6 +132,7 @@ google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175 google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/lifecycle.go b/lifecycle.go index 9c47ceb..9a0a369 100644 --- a/lifecycle.go +++ b/lifecycle.go @@ -58,7 +58,7 @@ const ( ) // ParseEventJSON parses event bytes returning the parsed Event and its event type -func ParseEventJSON(event []byte) (interface{}, string, error) { +func ParseEventJSON(event []byte) (any, string, error) { var base BaseEvent err := json.Unmarshal(event, &base) if err != nil { diff --git a/logger.go b/logger.go index c268bb4..8974975 100644 --- a/logger.go +++ b/logger.go @@ -10,35 +10,35 @@ import ( // Logger is a pluggable logger interface type Logger interface { - Debugf(format string, v ...interface{}) - Infof(format string, v ...interface{}) - Warnf(format string, v ...interface{}) - Errorf(format string, v ...interface{}) + Debugf(format string, v ...any) + Infof(format string, v ...any) + Warnf(format string, v ...any) + Errorf(format string, v ...any) } // Default console logger type defaultLogger struct{} -func (l *defaultLogger) Infof(format string, v ...interface{}) { +func (l *defaultLogger) Infof(format string, v ...any) { log.Printf(format, v...) } -func (l *defaultLogger) Warnf(format string, v ...interface{}) { +func (l *defaultLogger) Warnf(format string, v ...any) { log.Printf(format, v...) } -func (l *defaultLogger) Errorf(format string, v ...interface{}) { +func (l *defaultLogger) Errorf(format string, v ...any) { log.Printf(format, v...) } -func (l *defaultLogger) Debugf(format string, v ...interface{}) { +func (l *defaultLogger) Debugf(format string, v ...any) { log.Printf(format, v...) } // Logger placeholder type noopLogger struct{} -func (l *noopLogger) Infof(format string, v ...interface{}) {} -func (l *noopLogger) Warnf(format string, v ...interface{}) {} -func (l *noopLogger) Errorf(format string, v ...interface{}) {} -func (l *noopLogger) Debugf(format string, v ...interface{}) {} +func (l *noopLogger) Infof(format string, v ...any) {} +func (l *noopLogger) Warnf(format string, v ...any) {} +func (l *noopLogger) Errorf(format string, v ...any) {} +func (l *noopLogger) Debugf(format string, v ...any) {} diff --git a/mux.go b/mux.go index 0113658..9f36f82 100644 --- a/mux.go +++ b/mux.go @@ -24,7 +24,7 @@ type entryHandler struct { } // HandlerFunc handles a single task, the response bytes will be stored in the original task -type HandlerFunc func(ctx context.Context, log Logger, t *Task) (interface{}, error) +type HandlerFunc func(ctx context.Context, log Logger, t *Task) (any, error) // Mux routes messages // @@ -44,7 +44,7 @@ func NewTaskRouter() *Mux { } } -func notFoundHandler(_ context.Context, _ Logger, t *Task) (interface{}, error) { +func notFoundHandler(_ context.Context, _ Logger, t *Task) (any, error) { return nil, fmt.Errorf("%w %q", ErrNoHandlerForTaskType, t.Type) } @@ -98,7 +98,7 @@ func (m *Mux) RequestReply(taskType string, client *Client) error { // The task will be passed in JSON format on STDIN, any STDOUT/STDERR output will become the task // result. Any non 0 exit code will be treated as a task failure. func (m *Mux) ExternalProcess(taskType string, command string) error { - return m.HandleFunc(taskType, func(ctx context.Context, log Logger, task *Task) (interface{}, error) { + return m.HandleFunc(taskType, func(ctx context.Context, log Logger, task *Task) (any, error) { stat, err := os.Stat(command) if err != nil || stat.IsDir() { return nil, ErrExternalCommandNotFound diff --git a/mux_test.go b/mux_test.go index 3947e7b..a31e2e9 100644 --- a/mux_test.go +++ b/mux_test.go @@ -50,7 +50,7 @@ var _ = Describe("Router", func() { Describe("Handler", func() { It("Should support default handler", func() { router := NewTaskRouter() - router.HandleFunc("x", func(_ context.Context, _ Logger, _ *Task) (interface{}, error) { + router.HandleFunc("x", func(_ context.Context, _ Logger, _ *Task) (any, error) { return "x", nil }) @@ -64,16 +64,16 @@ var _ = Describe("Router", func() { It("Should find the correct handler", func() { router := NewTaskRouter() - router.HandleFunc("", func(_ context.Context, _ Logger, _ *Task) (interface{}, error) { + router.HandleFunc("", func(_ context.Context, _ Logger, _ *Task) (any, error) { return "custom default", nil }) - router.HandleFunc("things:", func(_ context.Context, _ Logger, _ *Task) (interface{}, error) { + router.HandleFunc("things:", func(_ context.Context, _ Logger, _ *Task) (any, error) { return "things:", nil }) - router.HandleFunc("things:very:specific", func(_ context.Context, _ Logger, _ *Task) (interface{}, error) { + router.HandleFunc("things:very:specific", func(_ context.Context, _ Logger, _ *Task) (any, error) { return "things:very:specific", nil }) - router.HandleFunc("things:specific", func(_ context.Context, _ Logger, _ *Task) (interface{}, error) { + router.HandleFunc("things:specific", func(_ context.Context, _ Logger, _ *Task) (any, error) { return "things:specific", nil }) diff --git a/processor.go b/processor.go index 0da67d5..84f5132 100644 --- a/processor.go +++ b/processor.go @@ -40,7 +40,7 @@ type ProcessItem struct { Kind ItemKind `json:"kind"` JobID string `json:"job"` - storageMeta interface{} + storageMeta any } func newProcessItem(kind ItemKind, id string) ([]byte, error) { diff --git a/processor_test.go b/processor_test.go index 9470a78..3070a6b 100644 --- a/processor_test.go +++ b/processor_test.go @@ -43,7 +43,7 @@ var _ = Describe("Processor", func() { wg.Add(1) router := NewTaskRouter() - router.HandleFunc("ginkgo", func(_ context.Context, _ Logger, t *Task) (interface{}, error) { + router.HandleFunc("ginkgo", func(_ context.Context, _ Logger, t *Task) (any, error) { wg.Done() return nil, fmt.Errorf("simulated failure: %w", ErrTerminateTask) }) @@ -88,7 +88,7 @@ var _ = Describe("Processor", func() { wg.Add(1) router := NewTaskRouter() - router.HandleFunc("ginkgo", func(_ context.Context, _ Logger, t *Task) (interface{}, error) { + router.HandleFunc("ginkgo", func(_ context.Context, _ Logger, t *Task) (any, error) { wg.Done() return nil, fmt.Errorf("simulated failure") }) @@ -129,7 +129,7 @@ var _ = Describe("Processor", func() { wg.Add(1) router := NewTaskRouter() - router.HandleFunc("ginkgo", func(_ context.Context, _ Logger, t *Task) (interface{}, error) { + router.HandleFunc("ginkgo", func(_ context.Context, _ Logger, t *Task) (any, error) { wg.Done() return "done", nil }) @@ -346,7 +346,7 @@ var _ = Describe("Processor", func() { wg.Add(1) router := NewTaskRouter() - router.HandleFunc("ginkgo", func(ctx context.Context, log Logger, task *Task) (interface{}, error) { + router.HandleFunc("ginkgo", func(ctx context.Context, log Logger, task *Task) (any, error) { // these will panic as its in a different routine, but they are supposed to pass so thats fine t, err := client.LoadTaskByID(task.ID) Expect(err).ToNot(HaveOccurred()) @@ -468,7 +468,7 @@ var _ = Describe("Processor", func() { var runs []string router := NewTaskRouter() - router.HandleFunc("ginkgo", func(ctx context.Context, log Logger, task *Task) (interface{}, error) { + router.HandleFunc("ginkgo", func(ctx context.Context, log Logger, task *Task) (any, error) { mu.Lock() defer mu.Unlock() defer wg.Done() diff --git a/request_reply_handler.go b/request_reply_handler.go index 626aa12..d05f563 100644 --- a/request_reply_handler.go +++ b/request_reply_handler.go @@ -52,7 +52,7 @@ func RequestReplySubjectForTaskType(taskType string) string { return fmt.Sprintf(RequestReplyTaskHandlerPattern, taskType) } -func (r *requestReplyHandler) processTask(ctx context.Context, logger Logger, task *Task) (interface{}, error) { +func (r *requestReplyHandler) processTask(ctx context.Context, logger Logger, task *Task) (any, error) { if r.nc == nil { return nil, fmt.Errorf("no connnection set") } diff --git a/request_reply_handler_test.go b/request_reply_handler_test.go index 15d54a4..fde997e 100644 --- a/request_reply_handler_test.go +++ b/request_reply_handler_test.go @@ -111,7 +111,7 @@ var _ = Describe("RequestReplyHandler", func() { go func() { nc.Subscribe(fmt.Sprintf(RequestReplyTaskHandlerPattern, "email:new"), func(msg *nats.Msg) { - fail := func(format string, a ...interface{}) { + fail := func(format string, a ...any) { resp := nats.NewMsg(msg.Subject) resp.Header.Add(RequestReplyError, fmt.Sprintf(format, a...)) msg.RespondMsg(resp) diff --git a/scheduled_task.go b/scheduled_task.go index 294dc8d..39d9166 100644 --- a/scheduled_task.go +++ b/scheduled_task.go @@ -77,7 +77,7 @@ func newScheduledTaskFromTask(name string, schedule string, queue string, task * return sched, cs, nil } -func newScheduledTask(name string, schedule string, queue string, taskType string, payload interface{}, opts ...TaskOpt) (*ScheduledTask, cron.Schedule, error) { +func newScheduledTask(name string, schedule string, queue string, taskType string, payload any, opts ...TaskOpt) (*ScheduledTask, cron.Schedule, error) { task, err := NewTask(taskType, payload, opts...) if err != nil { return nil, nil, err diff --git a/task.go b/task.go index 8f8b83e..47585c6 100644 --- a/task.go +++ b/task.go @@ -90,7 +90,7 @@ type Task struct { // LastErr is the most recent handling error if any LastErr string `json:"last_err,omitempty"` - storageOptions interface{} + storageOptions any mu sync.Mutex } @@ -104,13 +104,13 @@ type TasksInfo struct { // TaskResult is the result of task execution, this will only be set for successfully processed jobs type TaskResult struct { - Payload interface{} `json:"payload"` - CompletedAt time.Time `json:"completed"` + Payload any `json:"payload"` + CompletedAt time.Time `json:"completed"` } // NewTask creates a new task of taskType that can later be used to route tasks to handlers. // The task will carry a JSON encoded representation of payload. -func NewTask(taskType string, payload interface{}, opts ...TaskOpt) (*Task, error) { +func NewTask(taskType string, payload any, opts ...TaskOpt) (*Task, error) { if !IsValidName(taskType) { return nil, fmt.Errorf("%w: must match %s", ErrTaskTypeInvalid, validNameMatcher) } diff --git a/tools.go b/tools.go new file mode 100644 index 0000000..47773d5 --- /dev/null +++ b/tools.go @@ -0,0 +1,19 @@ +// Copyright (c) 2022, R.I. Pienaar and the Choria Project contributors +// +// SPDX-License-Identifier: Apache-2.0 + +//go:build tools + +package main + +import ( + _ "github.com/onsi/ginkgo/v2/ginkgo/generators" + _ "github.com/onsi/ginkgo/v2/ginkgo/internal" + _ "github.com/onsi/ginkgo/v2/ginkgo/labels" +) + +// this file is here to make things like go generate and ginkgo install +// happy, it has dependencies imported that it does not use and the build +// constraint ensures it's excluded during normal builds. +// +// see https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module