Skip to content

Commit ca8fd54

Browse files
committed
Refactor NewService to take a runtime and a courier instance
1 parent 871f61e commit ca8fd54

File tree

4 files changed

+43
-61
lines changed

4 files changed

+43
-61
lines changed

cmd/chipserver/main.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
ulog "log"
56
"log/slog"
67
"os"
@@ -12,7 +13,9 @@ import (
1213
"github.com/getsentry/sentry-go"
1314
_ "github.com/lib/pq"
1415
"github.com/nyaruka/chip"
16+
"github.com/nyaruka/chip/core/courier"
1517
"github.com/nyaruka/chip/runtime"
18+
"github.com/nyaruka/redisx"
1619
slogmulti "github.com/samber/slog-multi"
1720
slogsentry "github.com/samber/slog-sentry"
1821
)
@@ -51,15 +54,41 @@ func main() {
5154
log := slog.With("comp", "main")
5255
log.Info("starting...", "version", version, "released", date)
5356

54-
svc := chip.NewService(config)
55-
if err := svc.Start(); err != nil {
57+
svc, err := newService(config, log)
58+
if err != nil {
5659
log.Error("unable to start", "error", err)
5760
os.Exit(1)
5861
}
5962

6063
handleSignals(svc) // handle our signals
6164
}
6265

66+
func newService(cfg *runtime.Config, log *slog.Logger) (*chip.Service, error) {
67+
rt := &runtime.Runtime{Config: cfg}
68+
var err error
69+
70+
rt.DB, err = runtime.OpenDBPool(rt.Config.DB, 16)
71+
if err != nil {
72+
return nil, fmt.Errorf("error connecting to database: %w", err)
73+
} else {
74+
log.Info("db ok")
75+
}
76+
77+
rt.RP, err = redisx.NewPool(rt.Config.Redis)
78+
if err != nil {
79+
return nil, fmt.Errorf("error connecting to redis: %w", err)
80+
} else {
81+
log.Info("redis ok")
82+
}
83+
84+
svc := chip.NewService(rt, courier.NewCourier(rt.Config))
85+
if err := svc.Start(); err != nil {
86+
return nil, err
87+
}
88+
89+
return svc, nil
90+
}
91+
6392
// handleSignals takes care of trapping quit, interrupt or terminate signals and doing the right thing
6493
func handleSignals(svc *chip.Service) {
6594
sigs := make(chan os.Signal, 1)

service.go

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package chip
22

33
import (
44
"context"
5-
"fmt"
65
"log/slog"
76
"sync"
87
"time"
@@ -13,11 +12,6 @@ import (
1312
"github.com/nyaruka/chip/runtime"
1413
"github.com/nyaruka/chip/web"
1514
"github.com/nyaruka/chip/web/events"
16-
"github.com/nyaruka/redisx"
17-
)
18-
19-
const (
20-
outboxTimeLimit = 2 * time.Minute
2115
)
2216

2317
type Service struct {
@@ -31,14 +25,12 @@ type Service struct {
3125
senderWait sync.WaitGroup
3226
}
3327

34-
func NewService(cfg *runtime.Config) *Service {
35-
rt := &runtime.Runtime{Config: cfg}
36-
28+
func NewService(rt *runtime.Runtime, courier courier.Courier) *Service {
3729
s := &Service{
3830
rt: rt,
3931
store: models.NewStore(rt),
40-
outbox: &queue.Outbox{KeyBase: "chat", InstanceID: cfg.InstanceID},
41-
courier: courier.NewCourier(rt.Config),
32+
outbox: &queue.Outbox{KeyBase: "chat", InstanceID: rt.Config.InstanceID},
33+
courier: courier,
4234
senderStop: make(chan bool),
4335
}
4436

@@ -49,21 +41,6 @@ func NewService(cfg *runtime.Config) *Service {
4941

5042
func (s *Service) Start() error {
5143
log := slog.With("comp", "service")
52-
var err error
53-
54-
s.rt.DB, err = runtime.OpenDBPool(s.rt.Config.DB, 16)
55-
if err != nil {
56-
return fmt.Errorf("error connecting to database: %w", err)
57-
} else {
58-
log.Info("db ok")
59-
}
60-
61-
s.rt.RP, err = redisx.NewPool(s.rt.Config.Redis)
62-
if err != nil {
63-
return fmt.Errorf("error connecting to redis: %w", err)
64-
} else {
65-
log.Info("redis ok")
66-
}
6744

6845
s.server.Start()
6946
s.store.Start()
@@ -114,6 +91,8 @@ func (s *Service) OnChatStarted(ch *models.Channel, chatID models.ChatID) {
11491
log.Error("error setting chat ready", "error", err)
11592
return
11693
}
94+
95+
log.Info("chat started", "chat_id", chatID)
11796
}
11897

11998
func (s *Service) OnChatClosed(ch *models.Channel, chatID models.ChatID) {
@@ -125,6 +104,8 @@ func (s *Service) OnChatClosed(ch *models.Channel, chatID models.ChatID) {
125104
log.Error("error unsetting chat ready", "error", err)
126105
return
127106
}
107+
108+
log.Info("chat closed", "chat_id", chatID)
128109
}
129110

130111
func (s *Service) sender() {

service_test.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

web/server_test.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,16 @@ import (
66
"time"
77

88
"github.com/gorilla/websocket"
9-
"github.com/nyaruka/chip/core/courier"
9+
"github.com/nyaruka/chip"
1010
"github.com/nyaruka/chip/core/models"
1111
"github.com/nyaruka/chip/testsuite"
12-
"github.com/nyaruka/chip/web"
1312
"github.com/nyaruka/gocommon/dates"
1413
"github.com/nyaruka/gocommon/httpx"
1514
"github.com/nyaruka/gocommon/random"
1615
"github.com/stretchr/testify/assert"
1716
"github.com/stretchr/testify/require"
1817
)
1918

20-
type MockService struct {
21-
store models.Store
22-
courier courier.Courier
23-
}
24-
25-
func (s *MockService) Store() models.Store { return s.store }
26-
func (s *MockService) Courier() courier.Courier { return s.courier }
27-
func (s *MockService) OnChatStarted(*models.Channel, models.ChatID) {}
28-
func (s *MockService) OnChatClosed(*models.Channel, models.ChatID) {}
29-
func (s *MockService) OnSendRequest(*models.Channel, *models.MsgOut) {}
30-
3119
func TestServer(t *testing.T) {
3220
ctx, rt := testsuite.Runtime()
3321

@@ -40,11 +28,11 @@ func TestServer(t *testing.T) {
4028
dates.SetNowSource(dates.NewSequentialNowSource(time.Date(2024, 5, 2, 16, 5, 4, 0, time.UTC)))
4129

4230
mockCourier := testsuite.NewMockCourier(rt)
43-
mockSvc := &MockService{store: models.NewStore(rt), courier: mockCourier}
4431

45-
server := web.NewServer(rt, mockSvc)
46-
server.Start()
47-
defer server.Stop()
32+
svc := chip.NewService(rt, mockCourier)
33+
assert.NoError(t, svc.Start())
34+
35+
defer svc.Stop()
4836

4937
req, _ := http.NewRequest("GET", "http://localhost:8071/", nil)
5038
trace, err := httpx.DoTrace(http.DefaultClient, req, nil, nil, -1)

0 commit comments

Comments
 (0)