Skip to content

Commit

Permalink
Enhance logging and clean up code
Browse files Browse the repository at this point in the history
Added proper message handling to logging functions in slogAdapter. Updated queue server to use new SlogAdapter and removed an unused struct in scheduler. Enhanced readability by breaking long lines for signal notifications.
  • Loading branch information
dmitrymomot committed Aug 1, 2024
1 parent f0309ae commit 80c754d
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 16 deletions.
19 changes: 14 additions & 5 deletions example/queue/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"context"
"fmt"
"log"
"log/slog"
"os"
"os/signal"
"syscall"
"time"

"github.com/dmitrymomot/asyncer"
"golang.org/x/sync/errgroup"

"github.com/dmitrymomot/asyncer"
)

const (
Expand Down Expand Up @@ -47,7 +49,8 @@ func main() {

// Run a new queue server with redis as the broker.
eg.Go(asyncer.RunQueueServer(
ctx, redisAddr, nil,
ctx, redisAddr,
asyncer.NewSlogAdapter(slog.Default().With(slog.String("component", "queue-server"))),
// Register a handler for the task.
asyncer.HandlerFunc(TestTaskName, testTaskHandler),
asyncer.HandlerFunc(TestTaskName2, testTaskHandler2),
Expand All @@ -60,7 +63,12 @@ func main() {
asyncer.WithTaskDeadline(10*time.Minute),
asyncer.WithMaxRetry(0),
)
defer enqueuer.Close()
defer func(enqueuer *asyncer.Enqueuer) {
err := enqueuer.Close()
if err != nil {
slog.Error("Failed to close the enqueuer", "error", err)
}
}(enqueuer)

// Enqueue a task with payload.
// The task will be processed after immediately.
Expand Down Expand Up @@ -90,8 +98,9 @@ func main() {
// Listen for signals to cancel the context.
// This will stop the routine and close the queue server.
eg.Go(func() error {
c := make(chan os.Signal, 1) // Create channel to signify a signal being sent
signal.Notify(c, os.Interrupt, syscall.SIGTERM) // When an interrupt or termination signal is sent, notify the channel
c := make(chan os.Signal, 1) // Create channel to signify a signal being sent
signal.Notify(c, os.Interrupt,
syscall.SIGTERM) // When an interrupt or termination signal is sent, notify the channel

select {
case <-c:
Expand Down
9 changes: 3 additions & 6 deletions example/scheduler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@ import (
"fmt"
"time"

"github.com/dmitrymomot/asyncer"
"golang.org/x/sync/errgroup"

"github.com/dmitrymomot/asyncer"
)

const (
redisAddr = "redis://localhost:6379/0"
TestTaskName = "scheduled_task"
)

type TestTaskPayload struct {
Name string
}

// test task handler function
func testTaskHandler(ctx context.Context) error {
func testTaskHandler(_ context.Context) error {
fmt.Println("scheduled test task handler called at", time.Now().Format(time.RFC3339))
return nil
}
Expand Down
101 changes: 96 additions & 5 deletions slog_adapter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package asyncer

import (
"fmt"
"log/slog"
"os"

Expand All @@ -17,27 +18,117 @@ func NewSlogAdapter(log *slog.Logger) asynq.Logger {

// Debug logs a message at Debug level.
func (s *slogAdapter) Debug(args ...interface{}) {
s.log.Debug("", args...)
if len(args) == 0 {
return
}

var msg string

// If there is only one argument, it is the message.
if len(args) == 1 {
msg = fmt.Sprint(args[0])
args = make([]interface{}, 0)
}

// If there are more than one argument, the first one is the message.
if l := len(args); l > 1 && l%2 != 0 {
msg = fmt.Sprint(args[0])
args = args[1:]
}

s.log.Debug(msg, args...)
}

// Info logs a message at Info level.
func (s *slogAdapter) Info(args ...interface{}) {
s.log.Info("", args...)
if len(args) == 0 {
return
}

var msg string

// If there is only one argument, it is the message.
if len(args) == 1 {
msg = fmt.Sprint(args[0])
args = make([]interface{}, 0)
}

// If there are more than one argument, the first one is the message.
if l := len(args); l > 1 && l%2 != 0 {
msg = fmt.Sprint(args[0])
args = args[1:]
}

s.log.Info(msg, args...)
}

// Warn logs a message at Warning level.
func (s *slogAdapter) Warn(args ...interface{}) {
s.log.Warn("", args...)
if len(args) == 0 {
return
}

var msg string

// If there is only one argument, it is the message.
if len(args) == 1 {
msg = fmt.Sprint(args[0])
args = make([]interface{}, 0)
}

// If there are more than one argument, the first one is the message.
if l := len(args); l > 1 && l%2 != 0 {
msg = fmt.Sprint(args[0])
args = args[1:]
}

s.log.Warn(msg, args...)
}

// Error logs a message at Error level.
func (s *slogAdapter) Error(args ...interface{}) {
s.log.Error("", args...)
if len(args) == 0 {
return
}

var msg string

// If there is only one argument, it is the message.
if len(args) == 1 {
msg = fmt.Sprint(args[0])
args = make([]interface{}, 0)
}

// If there are more than one argument, the first one is the message.
if l := len(args); l > 1 && l%2 != 0 {
msg = fmt.Sprint(args[0])
args = args[1:]
}

s.log.Error(msg, args...)
}

// Fatal logs a message at Fatal level
// and process will exit with status set to 1.
func (s *slogAdapter) Fatal(args ...interface{}) {
s.log.Error("", args...)
if len(args) == 0 {
return
}

var msg string

// If there is only one argument, it is the message.
if len(args) == 1 {
msg = fmt.Sprint(args[0])
args = make([]interface{}, 0)
}

// If there are more than one argument, the first one is the message.
if l := len(args); l > 1 && l%2 != 0 {
msg = fmt.Sprint(args[0])
args = args[1:]
}

s.log.Error(msg, args...)
os.Exit(1)
}

0 comments on commit 80c754d

Please sign in to comment.