Skip to content

Commit

Permalink
fix: remove all fatal log and change to error propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
peterxcli committed Jan 31, 2024
1 parent 3087bad commit 74e0d0d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 18 deletions.
16 changes: 14 additions & 2 deletions pkg/controller/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,13 @@ func (ctrl *UserController) SubscribeEvent(c *gin.Context) {
return
}

go ctrl.asynqService.EnqueueEventNotification(userID, *request.ID, request.EventTimeStart)
err = ctrl.asynqService.EnqueueEventNotification(c, userID, *request.ID, request.EventTimeStart)
if err != nil {
c.AbortWithStatusJSON(500, model.Response{
Msg: err.Error(),
})
return
}

c.JSON(200, model.EventResponse{
Data: newEvent,
Expand Down Expand Up @@ -416,7 +422,13 @@ func (ctrl *UserController) UnScribeEvent(c *gin.Context) {
return
}

go ctrl.asynqService.DeleteEventNotification(userID + eventID)
err = ctrl.asynqService.DeleteEventNotification(c, userID+eventID)
if err != nil {
c.AbortWithStatusJSON(500, model.Response{
Msg: err.Error(),
})
return
}

c.JSON(200, model.Response{
Msg: "delete success",
Expand Down
9 changes: 5 additions & 4 deletions pkg/line_utils/flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"log"
)

func CreateFlexMessage(event *model.EventDetails) *linebot.FlexContainer {
func CreateFlexMessage(event *model.EventDetails) (*linebot.FlexContainer, error) {
// Construct the Flex Message payload using event details
contents := map[string]interface{}{
"type": "bubble",
Expand All @@ -20,15 +20,16 @@ func CreateFlexMessage(event *model.EventDetails) *linebot.FlexContainer {

contentsJSON, err := json.Marshal(contents)
if err != nil {
log.Fatal(err)
return nil, err
}

flexContainer, err := linebot.UnmarshalFlexMessageJSON(contentsJSON)
if err != nil {
log.Fatal(err)
log.Println(err)
return nil, err
}

return &flexContainer
return &flexContainer, nil
}

func buildContents(event *model.EventDetails) []map[string]interface{} {
Expand Down
4 changes: 2 additions & 2 deletions pkg/model/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ type EventService interface {
}

type AsynqNotificationService interface {
EnqueueEventNotification(userID, eventID, eventStartTime string)
DeleteEventNotification(TaskID string)
EnqueueEventNotification(ctx context.Context, userID, eventID, eventStartTime string) error
DeleteEventNotification(ctx context.Context, TaskID string) error
// TODO: delete event notification by event id
}
13 changes: 8 additions & 5 deletions pkg/service/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"bikefest/pkg/bootstrap"
"bikefest/pkg/model"
"context"
"encoding/json"
"log"
"time"
Expand Down Expand Up @@ -38,17 +39,18 @@ func newEventNotification(userId, eventId string) (*asynq.Task, error) {

// DeleteEventNotification deletes the task from the queue.
// the taskID is the userID + eventID
func (as *AsynqServiceImpl) DeleteEventNotification(taskID string) {
func (as *AsynqServiceImpl) DeleteEventNotification(ctx context.Context, taskID string) error {
err := as.inspector.DeleteTask("default", taskID)
if err != nil {
log.Fatal(err)
return err
}
return nil
}

func (as *AsynqServiceImpl) EnqueueEventNotification(userID, eventID, eventStartTime string) {
func (as *AsynqServiceImpl) EnqueueEventNotification(ctx context.Context, userID, eventID, eventStartTime string) error {
t, err := newEventNotification(userID, eventID)
if err != nil {
log.Fatal(err)
return err
}

location, _ := time.LoadLocation(as.env.Server.TimeZone)
Expand All @@ -59,9 +61,10 @@ func (as *AsynqServiceImpl) EnqueueEventNotification(userID, eventID, eventStart
info, err := as.client.Enqueue(t, asynq.ProcessAt(processTime), asynq.TaskID(userID+eventID))

if err != nil {
log.Fatal(err)
return err
}
log.Printf(" [*] Successfully enqueued task: %+v\nThe task should be executed at %s", info, processTime.String())
return nil
}

func NewAsynqService(client *asynq.Client, inspector *asynq.Inspector, env *bootstrap.Env) model.AsynqNotificationService {
Expand Down
1 change: 1 addition & 0 deletions pkg/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"context"
"errors"
"time"

tokensvc "bikefest/internal/token"
Expand Down
17 changes: 12 additions & 5 deletions pkg/worker/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,24 @@ func (eth *EventTaskHandler) HandleEventTask(ctx context.Context, t *asynq.Task)
eventDetails := model.EventDetails{}
err = json.Unmarshal([]byte(*event.EventDetail), &eventDetails)
if err != nil {
log.Fatal(err)
log.Println(err)
return err
}

flexContainer := line_utils.CreateFlexMessage(&eventDetails)
flexMessage := linebot.NewFlexMessage(fmt.Sprintf("Event: %s, 即將開始", eventDetails.Name), *flexContainer)
var message linebot.SendingMessage
flexContainer, err := line_utils.CreateFlexMessage(&eventDetails)
if err != nil {
message = linebot.NewTextMessage(fmt.Sprintf("Event: %s, 即將開始", eventDetails.Name))
} else {
message = linebot.NewFlexMessage(fmt.Sprintf("Event: %s, 即將開始", eventDetails.Name), *flexContainer)
}

//message := linebot.NewTextMessage(fmt.Sprintf("Hello, Event %s is going to start within 30 minutes!!!", p.EventID))

_, err = eth.bot.PushMessage(p.UserID, flexMessage).Do()
_, err = eth.bot.PushMessage(p.UserID, message).Do()
if err != nil {
log.Fatal(err)
log.Println(err)
return err
}

return nil
Expand Down

0 comments on commit 74e0d0d

Please sign in to comment.