Skip to content

Commit

Permalink
simplify examples, logger setup offloaded to qtest
Browse files Browse the repository at this point in the history
  • Loading branch information
fogfish committed Nov 17, 2023
1 parent 57b1133 commit 43967a6
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 45 deletions.
12 changes: 2 additions & 10 deletions examples/bytes/dequeue/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,16 @@ package main

import (
"log/slog"
"os"

"github.com/fogfish/swarm"
"github.com/fogfish/swarm/broker/sqs"
"github.com/fogfish/swarm/internal/qtest"
"github.com/fogfish/swarm/queue"
"github.com/fogfish/swarm/queue/bytes"
)

func main() {
slog.SetDefault(
slog.New(
slog.NewTextHandler(os.Stdout,
&slog.HandlerOptions{
Level: slog.LevelDebug,
},
),
),
)
qtest.NewLogger()

q := queue.Must(sqs.New("swarm-test", swarm.WithLogStdErr()))

Expand Down
14 changes: 2 additions & 12 deletions examples/bytes/enqueue/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,15 @@
package main

import (
"log/slog"
"os"

"github.com/fogfish/swarm"
"github.com/fogfish/swarm/broker/sqs"
"github.com/fogfish/swarm/internal/qtest"
"github.com/fogfish/swarm/queue"
"github.com/fogfish/swarm/queue/bytes"
)

func main() {
slog.SetDefault(
slog.New(
slog.NewTextHandler(os.Stdout,
&slog.HandlerOptions{
Level: slog.LevelDebug,
},
),
),
)
qtest.NewLogger()

q := queue.Must(sqs.New("swarm-test", swarm.WithLogStdErr()))

Expand Down
3 changes: 3 additions & 0 deletions examples/eventbridge/dequeue/eventbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/fogfish/swarm"
"github.com/fogfish/swarm/broker/eventbridge"
"github.com/fogfish/swarm/internal/qtest"
"github.com/fogfish/swarm/queue"
)

Expand All @@ -32,6 +33,8 @@ type Like struct {
}

func main() {
qtest.NewLogger()

q := queue.Must(eventbridge.New("swarm-example-eventbridge", swarm.WithLogStdErr()))

go actor[User]("user").handle(queue.Dequeue[User](q))
Expand Down
3 changes: 3 additions & 0 deletions examples/eventbridge/enqueue/eventbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package main
import (
"github.com/fogfish/swarm"
"github.com/fogfish/swarm/broker/eventbridge"
"github.com/fogfish/swarm/internal/qtest"
"github.com/fogfish/swarm/queue"
)

Expand All @@ -30,6 +31,8 @@ type Like struct {
}

func main() {
qtest.NewLogger()

q := queue.Must(eventbridge.New("swarm-example-eventbridge-latest",
swarm.WithSource("swarm-example-eventbridge"),
swarm.WithLogStdErr(),
Expand Down
3 changes: 3 additions & 0 deletions examples/events/dequeue/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/fogfish/swarm"
"github.com/fogfish/swarm/broker/sqs"
"github.com/fogfish/swarm/internal/qtest"
"github.com/fogfish/swarm/queue"
"github.com/fogfish/swarm/queue/events"
)
Expand Down Expand Up @@ -51,6 +52,8 @@ func (EventNote) HKT1(swarm.EventType) {}
func (EventNote) HKT2(*Note) {}

func main() {
qtest.NewLogger()

q := queue.Must(sqs.New("swarm-test", swarm.WithLogStdErr()))

go create(events.Dequeue[*User, EventCreateUser](q))
Expand Down
3 changes: 3 additions & 0 deletions examples/events/enqueue/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package main
import (
"github.com/fogfish/swarm"
"github.com/fogfish/swarm/broker/sqs"
"github.com/fogfish/swarm/internal/qtest"
"github.com/fogfish/swarm/queue"
"github.com/fogfish/swarm/queue/events"
)
Expand Down Expand Up @@ -47,6 +48,8 @@ func (EventNote) HKT1(swarm.EventType) {}
func (EventNote) HKT2(*Note) {}

func main() {
qtest.NewLogger()

q := queue.Must(sqs.New("swarm-test", swarm.WithLogStdErr()))

userCreated := swarm.LogDeadLetters(events.Enqueue[*User, EventCreateUser](q))
Expand Down
3 changes: 3 additions & 0 deletions examples/events3/dequeue/events3.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import (

"github.com/fogfish/swarm"
"github.com/fogfish/swarm/broker/events3"
"github.com/fogfish/swarm/internal/qtest"
"github.com/fogfish/swarm/queue"
)

func main() {
qtest.NewLogger()

q := queue.Must(events3.New("swarm-test", swarm.WithLogStdErr()))

go common(events3.Dequeue(q))
Expand Down
3 changes: 3 additions & 0 deletions examples/eventsqs/dequeue/eventsqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/fogfish/swarm"
"github.com/fogfish/swarm/broker/eventsqs"
"github.com/fogfish/swarm/internal/qtest"
"github.com/fogfish/swarm/queue"
)

Expand All @@ -32,6 +33,8 @@ type Like struct {
}

func main() {
qtest.NewLogger()

q := queue.Must(eventsqs.New("swarm-example-sqs-latest", swarm.WithLogStdErr()))

go actor[User]("user").handle(queue.Dequeue[User](q))
Expand Down
3 changes: 3 additions & 0 deletions examples/eventsqs/enqueue/eventsqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package main
import (
"github.com/fogfish/swarm"
"github.com/fogfish/swarm/broker/eventsqs"
"github.com/fogfish/swarm/internal/qtest"
"github.com/fogfish/swarm/queue"
)

Expand All @@ -30,6 +31,8 @@ type Like struct {
}

func main() {
qtest.NewLogger()

q := queue.Must(eventsqs.New("swarm-example-sqs-latest", swarm.WithLogStdErr()))

user, _ := queue.Enqueue[*User](q)
Expand Down
12 changes: 2 additions & 10 deletions examples/sqs/dequeue/sqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ package main

import (
"log/slog"
"os"

"github.com/fogfish/swarm"
"github.com/fogfish/swarm/broker/sqs"
"github.com/fogfish/swarm/internal/qtest"
"github.com/fogfish/swarm/queue"
)

Expand All @@ -33,15 +33,7 @@ type Like struct {
}

func main() {
slog.SetDefault(
slog.New(
slog.NewTextHandler(os.Stdout,
&slog.HandlerOptions{
Level: slog.LevelDebug,
},
),
),
)
qtest.NewLogger()

q := queue.Must(sqs.New("swarm-test", swarm.WithLogStdErr()))

Expand Down
14 changes: 2 additions & 12 deletions examples/sqs/enqueue/sqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
package main

import (
"log/slog"
"os"

"github.com/fogfish/swarm"
"github.com/fogfish/swarm/broker/sqs"
"github.com/fogfish/swarm/internal/qtest"
"github.com/fogfish/swarm/queue"
)

Expand All @@ -33,15 +31,7 @@ type Like struct {
}

func main() {
slog.SetDefault(
slog.New(
slog.NewTextHandler(os.Stdout,
&slog.HandlerOptions{
Level: slog.LevelDebug,
},
),
),
)
qtest.NewLogger()

q := queue.Must(sqs.New("swarm-test", swarm.WithLogStdErr()))

Expand Down
19 changes: 19 additions & 0 deletions internal/qtest/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package qtest

import (
"log/slog"
"os"
)

// Config logger for debug/testing purposes
func NewLogger() {
slog.SetDefault(
slog.New(
slog.NewTextHandler(os.Stdout,
&slog.HandlerOptions{
Level: slog.LevelDebug,
},
),
),
)
}
2 changes: 1 addition & 1 deletion queue/bytes/dequeue.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Dequeue(q swarm.Broker, cat string) (<-chan *swarm.Msg[[]byte], chan<- *swa
return
}

slog.Debug("Broker ack'ed object", "kind", "bytes", "category", cat, "object", object)
slog.Debug("Broker ack'ed object", "kind", "bytes", "category", cat, "object", object.Object)
})

pipe.Emit(ch.Msg, q.Config().PollFrequency, func() (*swarm.Msg[[]byte], error) {
Expand Down

0 comments on commit 43967a6

Please sign in to comment.