Skip to content

Commit

Permalink
panic less
Browse files Browse the repository at this point in the history
  • Loading branch information
roessland committed Jan 28, 2024
1 parent cc79c2f commit 035e3fc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
7 changes: 3 additions & 4 deletions pkg/withoutings/app/command/process_raw_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ type ProcessRawNotificationHandler interface {

func (h processRawNotificationHandler) Handle(ctx context.Context, cmd ProcessRawNotification) error {
log := logging.MustGetLoggerFromContext(ctx)
log.Info("Processing raw notification: ", cmd.RawNotification.UUID())

log.Debug("raw notification: ", cmd.RawNotification)
log = log.WithField("raw_notification_uuid", "cmd.RawNotification.UUID()")
log.WithField("event", "info.command.ProcessRawNotification.started").Info()

if cmd.RawNotification.Status() != subscription.RawNotificationStatusPending {
log.WithField("event", "error.command.ProcessRawNotification.invalidStatus").
Expand Down Expand Up @@ -58,7 +57,7 @@ func (h processRawNotificationHandler) Handle(ctx context.Context, cmd ProcessRa
}

if parsedParams.Appli != 1 {
panic("not implemented - can only handle weigh-ins")
return fmt.Errorf("not yet able to handle appli: %d", parsedParams.Appli)
}

// Fetch data from Withings API
Expand Down
15 changes: 10 additions & 5 deletions pkg/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package worker
import (
"context"
"encoding/json"
"fmt"
"github.com/roessland/withoutings/pkg/logging"
"github.com/roessland/withoutings/pkg/withoutings/adapter/topic"
"github.com/roessland/withoutings/pkg/withoutings/app"
"github.com/roessland/withoutings/pkg/withoutings/app/command"
Expand All @@ -28,6 +28,7 @@ func (wrk *Worker) close() {
}

func (wrk *Worker) Work(ctx context.Context) {
log := logging.MustGetLoggerFromContext(ctx)

messages, err := wrk.App.Subscriber.Subscribe(ctx, topic.WithingsRawNotificationReceived)
if err != nil {
Expand All @@ -37,25 +38,29 @@ func (wrk *Worker) Work(ctx context.Context) {
// TODO refactor to watermill router, avoid acking immediately

for msg := range messages {
fmt.Printf("Received message: %s - %s\n", msg.UUID, msg.Payload)
log = log.WithField("message_uuid", msg.UUID).WithField("message_payload", msg.Payload).WithField("message_metadata", msg.Metadata)
log.WithField("event", "info.worker.message.received")
var rawNotificationReceived subscription.RawNotificationReceived
err := json.Unmarshal(msg.Payload, &rawNotificationReceived)
if err != nil {
panic(err)
log.WithError(err).WithField("event", "error.worker.unmarshal.failed").Error()
continue
}

rawNotification, err := wrk.SubscriptionRepo.GetRawNotificationByUUID(ctx,
rawNotificationReceived.RawNotificationUUID,
)
if err != nil {
panic(err)
log.WithError(err).WithField("event", "error.worker.GetRawNotificationByUUID.failed").Error()
continue
}

err = wrk.Commands.ProcessRawNotification.Handle(ctx, command.ProcessRawNotification{
RawNotification: rawNotification,
})
if err != nil {
panic(err)
log.WithError(err).WithField("event", "error.worker.ProcessRawNotification.failed").Error()
continue
}
}
//asyncSrv := asynq.NewServer(
Expand Down

0 comments on commit 035e3fc

Please sign in to comment.