Skip to content

Commit f137407

Browse files
feat: lazy loading all the services (#470)
1 parent 7c36784 commit f137407

File tree

8 files changed

+194
-197
lines changed

8 files changed

+194
-197
lines changed

Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,4 @@ tasks:
325325
mv "$OUTPUT_DIR"/arduino/app_bricks/static "$DST_DIR"
326326
rm -r "$OUTPUT_DIR" "$ZIP_NAME"
327327
# Bumps the default pinned latest base python image tag
328-
sed -i "s#var RunnerVersion = \".*#var RunnerVersion = \"${semver_tag}\"#" cmd/arduino-app-cli/main.go
328+
sed -i "s#RunnerVersion = \".*#RunnerVersion = \"${semver_tag}\"#" cmd/arduino-app-cli/internal/servicelocator/servicelocator.go

cmd/arduino-app-cli/app.go

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,27 @@ import (
1111
"strings"
1212

1313
"github.com/arduino/go-paths-helper"
14-
dockerClient "github.com/docker/docker/client"
1514
"github.com/spf13/cobra"
1615
"mkuznets.com/go/tabwriter"
1716

17+
"github.com/bcmi-labs/orchestrator/cmd/arduino-app-cli/internal/servicelocator"
1818
"github.com/bcmi-labs/orchestrator/internal/orchestrator"
1919
"github.com/bcmi-labs/orchestrator/internal/orchestrator/app"
20-
"github.com/bcmi-labs/orchestrator/internal/orchestrator/bricksindex"
21-
"github.com/bcmi-labs/orchestrator/internal/orchestrator/modelsindex"
2220
)
2321

24-
func newAppCmd(
25-
docker *dockerClient.Client,
26-
provisioner *orchestrator.Provision,
27-
modelsIndex *modelsindex.ModelsIndex,
28-
bricksIndex *bricksindex.BricksIndex,
29-
) *cobra.Command {
22+
func newAppCmd() *cobra.Command {
3023
appCmd := &cobra.Command{
3124
Use: "app",
3225
Short: "Manage Arduino Apps",
3326
Long: "A CLI tool to manage Arduino Apps, including starting, stopping, logging, and provisioning.",
3427
}
3528

3629
appCmd.AddCommand(newCreateCmd())
37-
appCmd.AddCommand(newStartCmd(docker, provisioner, modelsIndex, bricksIndex))
30+
appCmd.AddCommand(newStartCmd())
3831
appCmd.AddCommand(newStopCmd())
39-
appCmd.AddCommand(newRestartCmd(docker, provisioner, modelsIndex, bricksIndex))
32+
appCmd.AddCommand(newRestartCmd())
4033
appCmd.AddCommand(newLogsCmd())
41-
appCmd.AddCommand(newListCmd(docker))
34+
appCmd.AddCommand(newListCmd())
4235
appCmd.AddCommand(newPsCmd())
4336
appCmd.AddCommand(newMonitorCmd())
4437

@@ -75,12 +68,7 @@ func newCreateCmd() *cobra.Command {
7568
return cmd
7669
}
7770

78-
func newStartCmd(
79-
docker *dockerClient.Client,
80-
provisioner *orchestrator.Provision,
81-
modelsIndex *modelsindex.ModelsIndex,
82-
bricksIndex *bricksindex.BricksIndex,
83-
) *cobra.Command {
71+
func newStartCmd() *cobra.Command {
8472
return &cobra.Command{
8573
Use: "start app_path",
8674
Short: "Start an Arduino app",
@@ -93,7 +81,7 @@ func newStartCmd(
9381
if err != nil {
9482
return err
9583
}
96-
return startHandler(cmd.Context(), docker, provisioner, app, modelsIndex, bricksIndex)
84+
return startHandler(cmd.Context(), app)
9785
},
9886
}
9987
}
@@ -116,12 +104,7 @@ func newStopCmd() *cobra.Command {
116104
}
117105
}
118106

119-
func newRestartCmd(
120-
docker *dockerClient.Client,
121-
provisioner *orchestrator.Provision,
122-
modelsIndex *modelsindex.ModelsIndex,
123-
bricksIndex *bricksindex.BricksIndex,
124-
) *cobra.Command {
107+
func newRestartCmd() *cobra.Command {
125108
cmd := &cobra.Command{
126109
Use: "restart app_path",
127110
Short: "Restart or Start an Arduino app",
@@ -137,7 +120,7 @@ func newRestartCmd(
137120
if err := stopHandler(cmd.Context(), app); err != nil {
138121
slog.Warn("failed to stop app", "error", err)
139122
}
140-
return startHandler(cmd.Context(), docker, provisioner, app, modelsIndex, bricksIndex)
123+
return startHandler(cmd.Context(), app)
141124
},
142125
}
143126
return cmd
@@ -175,14 +158,14 @@ func newMonitorCmd() *cobra.Command {
175158

176159
}
177160

178-
func newListCmd(docker *dockerClient.Client) *cobra.Command {
161+
func newListCmd() *cobra.Command {
179162
var jsonFormat bool
180163

181164
cmd := &cobra.Command{
182165
Use: "list",
183166
Short: "List all running Python apps",
184167
RunE: func(cmd *cobra.Command, args []string) error {
185-
return listHandler(cmd.Context(), docker, jsonFormat)
168+
return listHandler(cmd.Context(), jsonFormat)
186169
},
187170
}
188171

@@ -263,15 +246,16 @@ func newPropertiesCmd() *cobra.Command {
263246
return cmd
264247
}
265248

266-
func startHandler(
267-
ctx context.Context,
268-
docker *dockerClient.Client,
269-
provisioner *orchestrator.Provision,
270-
app app.ArduinoApp,
271-
modelsIndex *modelsindex.ModelsIndex,
272-
bricksIndex *bricksindex.BricksIndex,
273-
) error {
274-
for message := range orchestrator.StartApp(ctx, docker, provisioner, modelsIndex, bricksIndex, app) {
249+
func startHandler(ctx context.Context, app app.ArduinoApp) error {
250+
stream := orchestrator.StartApp(
251+
ctx,
252+
servicelocator.GetDockerClient(),
253+
servicelocator.GetProvisioner(),
254+
servicelocator.GetModelsIndex(),
255+
servicelocator.GetBricksIndex(),
256+
app,
257+
)
258+
for message := range stream {
275259
switch message.GetType() {
276260
case orchestrator.ProgressType:
277261
slog.Info("progress", slog.Float64("progress", float64(message.GetProgress().Progress)))
@@ -317,12 +301,15 @@ func logsHandler(ctx context.Context, app app.ArduinoApp, tail *uint64) error {
317301
return nil
318302
}
319303

320-
func listHandler(ctx context.Context, docker *dockerClient.Client, jsonFormat bool) error {
321-
res, err := orchestrator.ListApps(ctx, docker, orchestrator.ListAppRequest{
322-
ShowExamples: true,
323-
ShowApps: true,
324-
IncludeNonStandardLocationApps: true,
325-
})
304+
func listHandler(ctx context.Context, jsonFormat bool) error {
305+
res, err := orchestrator.ListApps(ctx,
306+
servicelocator.GetDockerClient(),
307+
orchestrator.ListAppRequest{
308+
ShowExamples: true,
309+
ShowApps: true,
310+
IncludeNonStandardLocationApps: true,
311+
},
312+
)
326313
if err != nil {
327314
return nil
328315
}

cmd/arduino-app-cli/bricks.go

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,51 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6-
"io/fs"
76

7+
"github.com/bcmi-labs/orchestrator/cmd/arduino-app-cli/internal/servicelocator"
88
"github.com/bcmi-labs/orchestrator/internal/orchestrator"
9-
"github.com/bcmi-labs/orchestrator/internal/orchestrator/bricksindex"
10-
"github.com/bcmi-labs/orchestrator/internal/orchestrator/modelsindex"
119

1210
"github.com/spf13/cobra"
1311
)
1412

15-
func newBrickCmd(
16-
brickDocsFS fs.FS,
17-
modelsIndex *modelsindex.ModelsIndex,
18-
bricksIndex *bricksindex.BricksIndex,
19-
) *cobra.Command {
13+
func newBrickCmd() *cobra.Command {
2014
appCmd := &cobra.Command{
2115
Use: "brick",
2216
Short: "Manage Arduino Bricks",
2317
}
2418

25-
appCmd.AddCommand(newBricksListCmd(modelsIndex, bricksIndex))
26-
appCmd.AddCommand(newBricksDetailsCmd(brickDocsFS, bricksIndex))
19+
appCmd.AddCommand(newBricksListCmd())
20+
appCmd.AddCommand(newBricksDetailsCmd())
2721

2822
return appCmd
2923
}
3024

31-
func newBricksListCmd(
32-
modelsIndex *modelsindex.ModelsIndex,
33-
bricksIndex *bricksindex.BricksIndex,
34-
) *cobra.Command {
25+
func newBricksListCmd() *cobra.Command {
3526
return &cobra.Command{
3627
Use: "list",
3728
Short: "List all available bricks",
3829
RunE: func(cmd *cobra.Command, args []string) error {
39-
return bricksListHandler(modelsIndex, bricksIndex)
30+
return bricksListHandler()
4031
},
4132
}
4233
}
4334

44-
func newBricksDetailsCmd(
45-
brickDocsFS fs.FS,
46-
bricksIndex *bricksindex.BricksIndex,
47-
) *cobra.Command {
35+
func newBricksDetailsCmd() *cobra.Command {
4836
return &cobra.Command{
4937
Use: "details",
5038
Short: "Details of a specific brick",
5139
Args: cobra.ExactArgs(1),
5240
RunE: func(cmd *cobra.Command, args []string) error {
53-
return bricksDetailsHandler(brickDocsFS, bricksIndex, args[0])
41+
return bricksDetailsHandler(args[0])
5442
},
5543
}
5644
}
5745

58-
func bricksListHandler(
59-
modelsIndex *modelsindex.ModelsIndex,
60-
bricksIndex *bricksindex.BricksIndex,
61-
) error {
62-
res, err := orchestrator.BricksList(modelsIndex, bricksIndex)
46+
func bricksListHandler() error {
47+
res, err := orchestrator.BricksList(
48+
servicelocator.GetModelsIndex(),
49+
servicelocator.GetBricksIndex(),
50+
)
6351
if err != nil {
6452
return nil
6553
}
@@ -72,12 +60,12 @@ func bricksListHandler(
7260
return nil
7361
}
7462

75-
func bricksDetailsHandler(
76-
brickDocsFS fs.FS,
77-
bricksIndex *bricksindex.BricksIndex,
78-
id string,
79-
) error {
80-
res, err := orchestrator.BricksDetails(brickDocsFS, bricksIndex, id)
63+
func bricksDetailsHandler(id string) error {
64+
res, err := orchestrator.BricksDetails(
65+
servicelocator.GetBricksDocsFS(),
66+
servicelocator.GetBricksIndex(),
67+
id,
68+
)
8169
if err != nil {
8270
return nil
8371
}

cmd/arduino-app-cli/daemon.go

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,23 @@ package main
33
import (
44
"context"
55
"errors"
6-
"io/fs"
76
"log/slog"
87
"net/http"
98
"time"
109

10+
"github.com/bcmi-labs/orchestrator/cmd/arduino-app-cli/internal/servicelocator"
1111
"github.com/bcmi-labs/orchestrator/internal/api"
1212
"github.com/bcmi-labs/orchestrator/internal/orchestrator"
13-
"github.com/bcmi-labs/orchestrator/internal/orchestrator/bricksindex"
14-
"github.com/bcmi-labs/orchestrator/internal/orchestrator/modelsindex"
1513
"github.com/bcmi-labs/orchestrator/internal/update"
1614
"github.com/bcmi-labs/orchestrator/internal/update/apt"
1715
"github.com/bcmi-labs/orchestrator/internal/update/arduino"
1816
"github.com/bcmi-labs/orchestrator/pkg/httprecover"
1917

20-
dockerClient "github.com/docker/docker/client"
2118
"github.com/jub0bs/cors"
2219
"github.com/spf13/cobra"
2320
)
2421

25-
func newDaemonCmd(
26-
docker *dockerClient.Client,
27-
provisioner *orchestrator.Provision,
28-
brickDocsFS fs.FS,
29-
modelsIndex *modelsindex.ModelsIndex,
30-
bricksIndex *bricksindex.BricksIndex,
31-
) *cobra.Command {
22+
func newDaemonCmd() *cobra.Command {
3223
daemonCmd := &cobra.Command{
3324
Use: "daemon",
3425
Short: "Run an HTTP server to expose arduino-app-cli functionality thorough REST API",
@@ -40,54 +31,38 @@ func newDaemonCmd(
4031
slog.Info("Starting default app")
4132
err := orchestrator.StartDefaultApp(
4233
cmd.Context(),
43-
docker,
44-
provisioner,
45-
modelsIndex,
46-
bricksIndex,
34+
servicelocator.GetDockerClient(),
35+
servicelocator.GetProvisioner(),
36+
servicelocator.GetModelsIndex(),
37+
servicelocator.GetBricksIndex(),
4738
)
4839
if err != nil {
4940
slog.Error("Failed to start default app", slog.String("error", err.Error()))
5041
}
5142
slog.Info("Default app started")
5243
}()
5344

54-
httpHandler(
55-
cmd.Context(),
56-
docker,
57-
provisioner,
58-
daemonPort,
59-
brickDocsFS,
60-
modelsIndex,
61-
bricksIndex,
62-
)
45+
httpHandler(cmd.Context(), daemonPort)
6346
},
6447
}
6548
daemonCmd.Flags().String("port", "8080", "The TCP port the daemon will listen to")
6649
return daemonCmd
6750
}
6851

69-
func httpHandler(
70-
ctx context.Context,
71-
dockerClient *dockerClient.Client,
72-
provisioner *orchestrator.Provision,
73-
daemonPort string,
74-
brickDocsFS fs.FS,
75-
modelsIndex *modelsindex.ModelsIndex,
76-
bricksIndex *bricksindex.BricksIndex,
77-
) {
52+
func httpHandler(ctx context.Context, daemonPort string) {
7853
slog.Info("Starting HTTP server", slog.String("address", ":"+daemonPort))
7954

8055
apiSrv := api.NewHTTPRouter(
81-
dockerClient,
56+
servicelocator.GetDockerClient(),
8257
Version,
8358
update.NewManager(
8459
apt.New(),
8560
arduino.NewArduinoPlatformUpdater(),
8661
),
87-
provisioner,
88-
brickDocsFS,
89-
modelsIndex,
90-
bricksIndex,
62+
servicelocator.GetProvisioner(),
63+
servicelocator.GetBricksDocsFS(),
64+
servicelocator.GetModelsIndex(),
65+
servicelocator.GetBricksIndex(),
9166
)
9267

9368
corsMiddlware, err := cors.NewMiddleware(

0 commit comments

Comments
 (0)