Skip to content
This repository was archived by the owner on Jun 12, 2024. It is now read-only.

Commit f621d3a

Browse files
authored
fix blocking error during startup when in demo mode (#838)
1 parent af9aa23 commit f621d3a

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

Taskfile.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
version: "3"
22

33
env:
4+
HBOX_LOG_LEVEL: debug
45
HBOX_STORAGE_SQLITE_URL: .data/homebox.db?_pragma=busy_timeout=1000&_pragma=journal_mode=WAL&_fk=1
56
HBOX_OPTIONS_ALLOW_REGISTRATION: true
67
UNSAFE_DISABLE_PASSWORD_PROJECTION: "yes_i_am_sure"

backend/app/api/demo.go

+22-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"strings"
6+
"time"
67

78
"github.com/hay-kot/homebox/backend/internal/core/services"
89
"github.com/rs/zerolog/log"
@@ -18,28 +19,44 @@ func (a *app) SetupDemo() {
1819
,Kitchen,IOT;Home Assistant; Z-Wave,1,Smart Rocker Light Dimmer,"UltraPro Z-Wave Smart Rocker Light Dimmer with QuickFit and SimpleWire, 3-Way Ready, Compatible with Alexa, Google Assistant, ZWave Hub Required, Repeater/Range Extender, White Paddle Only, 39351",,,39351,Honeywell,,Amazon,65.98,09/30/0202,,,,,,,
1920
`
2021

22+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
23+
defer cancel()
24+
2125
registration := services.UserRegistration{
2226
2327
Name: "Demo",
2428
Password: "demo",
2529
}
2630

2731
// First check if we've already setup a demo user and skip if so
28-
_, err := a.services.User.Login(context.Background(), registration.Email, registration.Password, false)
32+
log.Debug().Msg("Checking if demo user already exists")
33+
_, err := a.services.User.Login(ctx, registration.Email, registration.Password, false)
2934
if err == nil {
35+
log.Info().Msg("Demo user already exists, skipping setup")
3036
return
3137
}
3238

33-
_, err = a.services.User.RegisterUser(context.Background(), registration)
39+
log.Debug().Msg("Demo user does not exist, setting up demo")
40+
_, err = a.services.User.RegisterUser(ctx, registration)
3441
if err != nil {
3542
log.Err(err).Msg("Failed to register demo user")
3643
log.Fatal().Msg("Failed to setup demo")
3744
}
3845

39-
token, _ := a.services.User.Login(context.Background(), registration.Email, registration.Password, false)
40-
self, _ := a.services.User.GetSelf(context.Background(), token.Raw)
46+
token, err := a.services.User.Login(ctx, registration.Email, registration.Password, false)
47+
if err != nil {
48+
log.Err(err).Msg("Failed to login demo user")
49+
log.Fatal().Msg("Failed to setup demo")
50+
return
51+
}
52+
self, err := a.services.User.GetSelf(ctx, token.Raw)
53+
if err != nil {
54+
log.Err(err).Msg("Failed to get self")
55+
log.Fatal().Msg("Failed to setup demo")
56+
return
57+
}
4158

42-
_, err = a.services.Items.CsvImport(context.Background(), self.GroupID, strings.NewReader(csvText))
59+
_, err = a.services.Items.CsvImport(ctx, self.GroupID, strings.NewReader(csvText))
4360
if err != nil {
4461
log.Err(err).Msg("Failed to import CSV")
4562
log.Fatal().Msg("Failed to setup demo")

backend/app/api/main.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,15 @@ func run(cfg *config.Config) error {
207207

208208
runner.AddFunc("eventbus", app.bus.Run)
209209

210+
runner.AddFunc("seed_database", func(ctx context.Context) error {
211+
// TODO: Remove through external API that does setup
212+
if cfg.Demo {
213+
log.Info().Msg("Running in demo mode, creating demo data")
214+
app.SetupDemo()
215+
}
216+
return nil
217+
})
218+
210219
runner.AddPlugin(NewTask("purge-tokens", time.Duration(24)*time.Hour, func(ctx context.Context) {
211220
_, err := app.repos.AuthTokens.PurgeExpiredTokens(ctx)
212221
if err != nil {
@@ -239,12 +248,6 @@ func run(cfg *config.Config) error {
239248
}
240249
}))
241250

242-
// TODO: Remove through external API that does setup
243-
if cfg.Demo {
244-
log.Info().Msg("Running in demo mode, creating demo data")
245-
app.SetupDemo()
246-
}
247-
248251
if cfg.Debug.Enabled {
249252
runner.AddFunc("debug", func(ctx context.Context) error {
250253
debugserver := http.Server{

backend/internal/core/services/reporting/eventbus/eventbus.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type EventBus struct {
3535

3636
func New() *EventBus {
3737
return &EventBus{
38-
ch: make(chan eventData, 10),
38+
ch: make(chan eventData, 100),
3939
subscribers: map[Event][]func(any){
4040
EventLabelMutation: {},
4141
EventLocationMutation: {},

backend/internal/core/services/service_user.go

+4
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,19 @@ func (svc *UserService) RegisterUser(ctx context.Context, data UserRegistration)
9292
if err != nil {
9393
return repo.UserOut{}, err
9494
}
95+
log.Debug().Msg("user created")
9596

9697
// Create the default labels and locations for the group.
9798
if creatingGroup {
99+
log.Debug().Msg("creating default labels")
98100
for _, label := range defaultLabels() {
99101
_, err := svc.repos.Labels.Create(ctx, usr.GroupID, label)
100102
if err != nil {
101103
return repo.UserOut{}, err
102104
}
103105
}
104106

107+
log.Debug().Msg("creating default locations")
105108
for _, location := range defaultLocations() {
106109
_, err := svc.repos.Locations.Create(ctx, usr.GroupID, location)
107110
if err != nil {
@@ -112,6 +115,7 @@ func (svc *UserService) RegisterUser(ctx context.Context, data UserRegistration)
112115

113116
// Decrement the invitation token if it was used.
114117
if token.ID != uuid.Nil {
118+
log.Debug().Msg("decrementing invitation token")
115119
err = svc.repos.Groups.InvitationUpdate(ctx, token.ID, token.Uses-1)
116120
if err != nil {
117121
log.Err(err).Msg("Failed to update invitation token")

0 commit comments

Comments
 (0)