Skip to content

Commit

Permalink
refactor: switch to new slog (#41)
Browse files Browse the repository at this point in the history
* refactor: switch to new slog
  • Loading branch information
kalbhor authored Sep 20, 2023
1 parent c36ea63 commit ff4f99d
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
test:
strategy:
matrix:
go: ["1.17", "1.18"]
go: ["1.21"]

runs-on: ubuntu-20.04

Expand Down
9 changes: 3 additions & 6 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package tasqueue
import (
"context"
"log"
"log/slog"
"sync"
"testing"

"github.com/go-redis/redis"
rb "github.com/kalbhor/tasqueue/v2/brokers/redis"
rr "github.com/kalbhor/tasqueue/v2/results/redis"
"github.com/zerodha/logf"
)

// The benchmarks use redis as results & broker.
Expand All @@ -32,10 +32,7 @@ func newJob(b *testing.B) Job {

// serverWithRedis returns a tasqueue server with redis as broker and results.
func serverWithRedis(b *testing.B) *Server {
lo := logf.New(logf.Opts{
Level: logf.FatalLevel,
EnableColor: true,
})
lo := slog.Default()
srv, err := NewServer(ServerOpts{
Broker: rb.New(rb.Options{
Addrs: []string{redisAddr},
Expand All @@ -47,7 +44,7 @@ func serverWithRedis(b *testing.B) *Server {
Password: redisPass,
DB: redisDB,
}, lo),
Logger: lo,
Logger: lo.Handler(),
})
if err != nil {
b.Fatal(err)
Expand Down
6 changes: 3 additions & 3 deletions brokers/nats-js/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package nats
import (
"context"
"fmt"
"log/slog"
"time"

"github.com/nats-io/nats.go"
"github.com/zerodha/logf"
)

// Broker is a nats-jetstream based broker implementation.
type Broker struct {
opt Options
log logf.Logger
log *slog.Logger
conn nats.JetStreamContext
}

Expand All @@ -27,7 +27,7 @@ type Options struct {
}

// New() returns a new instance of nats-jetstream broker.
func New(cfg Options, lo logf.Logger) (*Broker, error) {
func New(cfg Options, lo *slog.Logger) (*Broker, error) {
opt := []nats.Option{}

if cfg.EnabledAuth {
Expand Down
6 changes: 3 additions & 3 deletions brokers/redis/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"strconv"
"time"

"github.com/go-redis/redis/v8"
"github.com/zerodha/logf"
)

const (
Expand All @@ -29,12 +29,12 @@ type Options struct {
}

type Broker struct {
log logf.Logger
log *slog.Logger
conn redis.UniversalClient
pollPeriod time.Duration
}

func New(o Options, lo logf.Logger) *Broker {
func New(o Options, lo *slog.Logger) *Broker {
pollPeriod := o.PollPeriod
if o.PollPeriod == 0 {
pollPeriod = DefaultPollPeriod
Expand Down
6 changes: 3 additions & 3 deletions chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package tasqueue

import (
"context"
"encoding/json"
"fmt"

"github.com/google/uuid"
"github.com/vmihailenco/msgpack/v5"
)

// ChainMeta contains fields related to a chain job.
Expand Down Expand Up @@ -135,7 +135,7 @@ checkJobs:
const chainPrefix = "chain:msg:"

func (s *Server) setChainMessage(ctx context.Context, c ChainMessage) error {
b, err := json.Marshal(c)
b, err := msgpack.Marshal(c)
if err != nil {
return err
}
Expand All @@ -149,7 +149,7 @@ func (s *Server) getChainMessage(ctx context.Context, id string) (ChainMessage,
}

var c ChainMessage
if err := json.Unmarshal(b, &c); err != nil {
if err := msgpack.Unmarshal(b, &c); err != nil {
return ChainMessage{}, err
}

Expand Down
4 changes: 2 additions & 2 deletions examples/in-memory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"log"
"log/slog"
"os"
"os/signal"
"time"
Expand All @@ -14,7 +15,6 @@ import (
rb "github.com/kalbhor/tasqueue/v2/brokers/in-memory"
"github.com/kalbhor/tasqueue/v2/examples/tasks"
rr "github.com/kalbhor/tasqueue/v2/results/in-memory"
"github.com/zerodha/logf"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
Expand Down Expand Up @@ -78,7 +78,7 @@ func main() {
srv, err := tasqueue.NewServer(tasqueue.ServerOpts{
Broker: rb.New(),
Results: rr.New(),
Logger: logf.New(logf.Opts{}),
Logger: slog.Default().Handler(),
TraceProvider: tp,
})
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions examples/nats-js/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import (
"encoding/json"
"fmt"
"log"
"log/slog"
"os"
"os/signal"

"github.com/kalbhor/tasqueue/v2"
nats_broker "github.com/kalbhor/tasqueue/v2/brokers/nats-js"
"github.com/kalbhor/tasqueue/v2/examples/tasks"
nats_result "github.com/kalbhor/tasqueue/v2/results/nats-js"
"github.com/zerodha/logf"
)

func main() {
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
lo := logf.New(logf.Opts{})
lo := slog.Default()
brkr, err := nats_broker.New(nats_broker.Options{
URL: "localhost:4222",
EnabledAuth: false,
Expand All @@ -40,7 +40,7 @@ func main() {
srv, err := tasqueue.NewServer(tasqueue.ServerOpts{
Broker: brkr,
Results: res,
Logger: logf.New(logf.Opts{}),
Logger: lo.Handler(),
})
if err != nil {
log.Fatal(err)
Expand Down
8 changes: 3 additions & 5 deletions examples/redis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"log"
"log/slog"
"os"
"os/signal"
"time"
Expand All @@ -13,12 +14,11 @@ import (
rb "github.com/kalbhor/tasqueue/v2/brokers/redis"
"github.com/kalbhor/tasqueue/v2/examples/tasks"
rr "github.com/kalbhor/tasqueue/v2/results/redis"
"github.com/zerodha/logf"
)

func main() {
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
lo := logf.New(logf.Opts{})
lo := slog.Default()
srv, err := tasqueue.NewServer(tasqueue.ServerOpts{
Broker: rb.New(rb.Options{
Addrs: []string{"127.0.0.1:6379"},
Expand All @@ -31,7 +31,7 @@ func main() {
DB: 0,
MetaExpiry: time.Second * 5,
}, lo),
Logger: lo,
Logger: lo.Handler(),
})
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -70,6 +70,4 @@ func main() {
}
}

// Create a task payload.
fmt.Println("exit..")
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/kalbhor/tasqueue/v2

go 1.18
go 1.21

require (
github.com/go-redis/redis v6.15.9+incompatible
Expand All @@ -9,7 +9,6 @@ require (
github.com/nats-io/nats.go v1.28.0
github.com/robfig/cron/v3 v3.0.1
github.com/vmihailenco/msgpack/v5 v5.3.5
github.com/zerodha/logf v0.5.5
go.opentelemetry.io/otel v1.9.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.9.0
go.opentelemetry.io/otel/sdk v1.9.0
Expand All @@ -26,6 +25,7 @@ require (
github.com/nats-io/nats-server/v2 v2.9.21 // indirect
github.com/nats-io/nkeys v0.4.4 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/sys v0.10.0 // indirect
Expand Down
19 changes: 17 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cb
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
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=
Expand All @@ -19,12 +21,15 @@ github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
github.com/minio/highwayhash v1.0.2/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY=
github.com/nats-io/jwt/v2 v2.4.1 h1:Y35W1dgbbz2SQUYDPCaclXcuqleVmpbRa7646Jf2EX4=
github.com/nats-io/jwt/v2 v2.4.1/go.mod h1:24BeQtRwxRV8ruvC4CojXlx/WQ/VjuwlYiH+vu/+ibI=
github.com/nats-io/nats-server/v2 v2.9.21 h1:2TBTh0UDE74eNXQmV4HofsmRSCiVN0TH2Wgrp6BD6fk=
github.com/nats-io/nats-server/v2 v2.9.21/go.mod h1:ozqMZc2vTHcNcblOiXMWIXkf8+0lDGAi5wQcG+O1mHU=
github.com/nats-io/nats.go v1.28.0 h1:Th4G6zdsz2d0OqXdfzKLClo6bOfoI/b1kInhRtFIy5c=
Expand All @@ -34,21 +39,25 @@ github.com/nats-io/nkeys v0.4.4/go.mod h1:XUkxdLPTufzlihbamfzQ7mw/VGx6ObUs+0bN5s
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
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/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/zerodha/logf v0.5.5 h1:AhxHlixHNYwhFjvlgTv6uO4VBKYKxx2I6SbHoHtWLBk=
github.com/zerodha/logf v0.5.5/go.mod h1:HWpfKsie+WFFpnUnUxelT6Z0FC6xu9+qt+oXNMPg6y8=
go.opentelemetry.io/otel v1.9.0 h1:8WZNQFIB2a71LnANS9JeyidJKKGOOremcUtb/OtHISw=
go.opentelemetry.io/otel v1.9.0/go.mod h1:np4EoPGzoPs3O67xUVNoPPcmSvsfOxNlNA4F4AC+0Eo=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.9.0 h1:0uV0qzHk48i1SF8qRI8odMYiwPOLh9gBhiJFpj8H6JY=
Expand All @@ -60,17 +69,23 @@ go.opentelemetry.io/otel/trace v1.9.0/go.mod h1:2737Q0MuG8q1uILYm2YYVkAyLtOofiTN
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
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/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=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
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=
6 changes: 3 additions & 3 deletions groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package tasqueue

import (
"context"
"encoding/json"
"fmt"

"github.com/google/uuid"
"github.com/vmihailenco/msgpack/v5"
)

type Group struct {
Expand Down Expand Up @@ -139,7 +139,7 @@ func getGroupStatus(jobStatus map[string]string) string {
const groupPrefix = "group:msg:"

func (s *Server) setGroupMessage(ctx context.Context, g GroupMessage) error {
b, err := json.Marshal(g)
b, err := msgpack.Marshal(g)
if err != nil {
return err
}
Expand All @@ -153,7 +153,7 @@ func (s *Server) getGroupMessage(ctx context.Context, id string) (GroupMessage,
}

var g GroupMessage
if err := json.Unmarshal(b, &g); err != nil {
if err := msgpack.Unmarshal(b, &g); err != nil {
return GroupMessage{}, err
}

Expand Down
5 changes: 2 additions & 3 deletions jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tasqueue

import (
"context"
"encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -234,7 +233,7 @@ func (s *Server) setJobMessage(ctx context.Context, t JobMessage) error {
defer span.End()
}

b, err := json.Marshal(t)
b, err := msgpack.Marshal(t)
if err != nil {
s.spanError(span, err)
return fmt.Errorf("could not set job message in store : %w", err)
Expand Down Expand Up @@ -263,7 +262,7 @@ func (s *Server) GetJob(ctx context.Context, id string) (JobMessage, error) {
}

var t JobMessage
if err := json.Unmarshal(b, &t); err != nil {
if err := msgpack.Unmarshal(b, &t); err != nil {
s.spanError(span, err)
return JobMessage{}, err
}
Expand Down
Loading

0 comments on commit ff4f99d

Please sign in to comment.