From 5bc8933aee5568b0866039112cd04a5cfc94289a Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Mon, 5 Aug 2024 09:19:05 +0000 Subject: [PATCH] wip --- service/services.go | 6 ++++-- service/ysk.go | 28 +++++++++++++++++++++++++++- service/ysk_test.go | 6 ++++-- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/service/services.go b/service/services.go index ccfe8d2..5b81183 100644 --- a/service/services.go +++ b/service/services.go @@ -36,13 +36,15 @@ func NewServices(repository *repository.Repository) Services { eventTypeService := NewEventTypeService(repository) actionTypeService := NewActionTypeService(repository) + eventServiceWS := NewEventServiceWS(eventTypeService) + return Services{ EventTypeService: eventTypeService, - EventServiceWS: NewEventServiceWS(eventTypeService), SocketIOService: NewSocketIOService(), + EventServiceWS: eventServiceWS, ActionTypeService: actionTypeService, ActionServiceWS: NewActionServiceWS(actionTypeService), - YSKService: NewYSKService(repository), + YSKService: NewYSKService(repository, eventServiceWS), } } diff --git a/service/ysk.go b/service/ysk.go index 0f01feb..07fd794 100644 --- a/service/ysk.go +++ b/service/ysk.go @@ -2,6 +2,8 @@ package service import ( "context" + "fmt" + "log" "github.com/IceWhaleTech/CasaOS-MessageBus/pkg/ysk" "github.com/IceWhaleTech/CasaOS-MessageBus/repository" @@ -9,11 +11,13 @@ import ( type YSKService struct { repository *repository.Repository + ws *EventServiceWS } -func NewYSKService(repository *repository.Repository) *YSKService { +func NewYSKService(repository *repository.Repository, ws *EventServiceWS) *YSKService { return &YSKService{ repository: repository, + ws: ws, } } @@ -37,3 +41,25 @@ func (s *YSKService) UpsertYSKCard(ctx context.Context, yskCard ysk.YSKCard) err func (s *YSKService) DeleteYSKCard(ctx context.Context, id string) error { return nil } + +func (s *YSKService) Start() { + channel, err := s.ws.Subscribe(ysk.SERVICENAME, []string{ + "ysk:card:create", "ysk:card:delete", + }) + if err != nil { + return + } + + go func() { + for { + select { + case event, ok := <-channel: + if !ok { + log.Println("channel closed") + } + fmt.Println(event) + } + } + }() + +} diff --git a/service/ysk_test.go b/service/ysk_test.go index 55b1eb6..3bc1961 100644 --- a/service/ysk_test.go +++ b/service/ysk_test.go @@ -15,7 +15,7 @@ func setup(t *testing.T) (*service.YSKService, func()) { repository, err := repository.NewDatabaseRepositoryInMemory() assert.NilError(t, err) - yskService := service.NewYSKService(&repository) + yskService := service.NewYSKService(&repository, nil) return yskService, func() { repository.Close() } @@ -62,7 +62,9 @@ func TestInsertAllTypeCardList(t *testing.T) { assert.Equal(t, len(cardList), 0) cardInsertQueue := []ysk.YSKCard{ - utils.ApplicationInstallProgress, utils.DiskInsertNotice, utils.ApplicationUpdateNotice, + utils.ApplicationInstallProgress, utils.DiskInsertNotice, + // the notice is short. it didn't be stored + utils.ApplicationUpdateNotice, utils.ApplicationInstallProgress.WithProgress("Installing LinuxServer/Jellyfin", 50), utils.ApplicationInstallProgress.WithProgress("Installing LinuxServer/Jellyfin", 55), utils.ApplicationInstallProgress.WithProgress("Installing LinuxServer/Jellyfin", 80), utils.ApplicationInstallProgress.WithProgress("Installing LinuxServer/Jellyfin", 99), utils.ApplicationUpdateNotice,