Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.

Commit 091fc1a

Browse files
Merge branch 'main' into mjp/data
2 parents 7ce2a1b + f390f9b commit 091fc1a

File tree

6 files changed

+78
-70
lines changed

6 files changed

+78
-70
lines changed

datasource_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import (
1818
"time"
1919

2020
"hmruntime/config"
21-
"hmruntime/server"
21+
"hmruntime/httpserver"
22+
"hmruntime/services"
2223

2324
"github.com/docker/docker/api/types/container"
2425
"github.com/docker/docker/api/types/image"
@@ -100,14 +101,14 @@ func TestMain(m *testing.M) {
100101
config.Port = httpListenPort
101102

102103
// setup runtime services
103-
initRuntimeServices(context.Background())
104-
defer stopRuntimeServices(context.Background())
104+
services.Start(context.Background())
105+
defer services.Stop(context.Background())
105106

106107
// start HTTP server
107108
ctx, stop := context.WithCancel(context.Background())
108109
done := make(chan struct{})
109110
go func() {
110-
server.Start(ctx, true)
111+
httpserver.Start(ctx, true)
111112
close(done)
112113
}()
113114

server/admin.go renamed to httpserver/admin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright 2024 Hypermode, Inc.
33
*/
44

5-
package server
5+
package httpserver
66

77
import (
88
"encoding/json"

server/server.go renamed to httpserver/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright 2024 Hypermode, Inc.
33
*/
44

5-
package server
5+
package httpserver
66

77
import (
88
"context"

main.go

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,11 @@ import (
1313
"path/filepath"
1414
"runtime"
1515

16-
"hmruntime/aws"
17-
"hmruntime/collections"
1816
"hmruntime/config"
19-
"hmruntime/db"
20-
"hmruntime/graphql"
21-
"hmruntime/hostfunctions"
17+
"hmruntime/httpserver"
2218
"hmruntime/logger"
23-
"hmruntime/manifestdata"
24-
"hmruntime/pluginmanager"
25-
"hmruntime/secrets"
26-
"hmruntime/server"
27-
"hmruntime/storage"
19+
"hmruntime/services"
2820
"hmruntime/utils"
29-
"hmruntime/wasmhost"
3021

3122
"github.com/joho/godotenv"
3223
)
@@ -54,64 +45,19 @@ func main() {
5445
utils.InitSentry(rootSourcePath)
5546
defer utils.FlushSentryEvents()
5647

57-
// Initialize the runtime services
48+
// Create the main background context
5849
ctx := context.Background()
59-
initRuntimeServices(ctx)
60-
defer stopRuntimeServices(ctx)
50+
51+
// Start the runtime services
52+
services.Start(ctx)
53+
defer services.Stop(ctx)
6154

6255
// Set local mode if debugging is enabled
6356
local := utils.HypermodeDebugEnabled()
6457

6558
// Start the HTTP server to listen for requests.
6659
// Note, this function blocks, and handles shutdown gracefully.
67-
server.Start(ctx, local)
68-
}
69-
70-
func initRuntimeServices(ctx context.Context) {
71-
transaction, ctx := utils.NewSentryTransactionForCurrentFunc(ctx)
72-
defer transaction.Finish()
73-
74-
// Initialize the WebAssembly runtime
75-
wasmhost.InitWasmHost(ctx)
76-
77-
// Register the host functions with the runtime
78-
hostfunctions.RegisterHostFunctions(ctx)
79-
80-
// Initialize AWS functionality
81-
aws.Initialize(ctx)
82-
83-
// Initialize the secrets provider
84-
secrets.Initialize(ctx)
85-
86-
// Initialize the storage provider
87-
storage.Initialize(ctx)
88-
89-
// Initialize the metadata database
90-
db.Initialize(ctx)
91-
92-
// Initialize in mem vector factory
93-
collections.InitializeIndexFactory(ctx)
94-
95-
// Load app data and monitor for changes
96-
onChange := func([]error) {
97-
hostfunctions.ShutdownPools()
98-
}
99-
manifestdata.MonitorManifestFile(ctx, onChange)
100-
101-
// Load plugins and monitor for changes
102-
pluginmanager.MonitorPlugins(ctx)
103-
104-
// Initialize the GraphQL engine
105-
graphql.Initialize()
106-
}
107-
108-
func stopRuntimeServices(ctx context.Context) {
109-
collections.CloseIndexFactory(ctx)
110-
wasmhost.RuntimeInstance.Close(ctx)
111-
logger.Close()
112-
113-
db.Stop()
114-
hostfunctions.ShutdownPools()
60+
httpserver.Start(ctx, local)
11561
}
11662

11763
func getRootSourcePath() string {

metrics/metrics_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"testing"
1515

1616
"hmruntime/config"
17-
"hmruntime/server"
17+
"hmruntime/httpserver"
1818

1919
"github.com/prometheus/common/expfmt"
2020
)
@@ -64,7 +64,7 @@ func TestRuntimeMetrics(t *testing.T) {
6464

6565
setupRuntime()
6666

67-
mux := server.GetHandlerMux()
67+
mux := httpserver.GetHandlerMux()
6868
s := httptest.NewServer(mux)
6969
defer s.Close()
7070

services/services.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2024 Hypermode, Inc.
3+
*/
4+
5+
package services
6+
7+
import (
8+
"context"
9+
10+
"hmruntime/aws"
11+
"hmruntime/collections"
12+
"hmruntime/db"
13+
"hmruntime/graphql"
14+
"hmruntime/hostfunctions"
15+
"hmruntime/logger"
16+
"hmruntime/manifestdata"
17+
"hmruntime/pluginmanager"
18+
"hmruntime/secrets"
19+
"hmruntime/storage"
20+
"hmruntime/utils"
21+
"hmruntime/wasmhost"
22+
)
23+
24+
// Starts any services that need to be started when the runtime starts.
25+
func Start(ctx context.Context) {
26+
transaction, ctx := utils.NewSentryTransactionForCurrentFunc(ctx)
27+
defer transaction.Finish()
28+
29+
// None of these should block. If they need to do background work, they should start a goroutine internally.
30+
31+
// NOTE: Initialization order is important in some cases.
32+
// If you need to change the order or add new services, be sure to test thoroughly.
33+
// Generally, new services should be added to the end of the list, unless there is a specific reason to do otherwise.
34+
35+
wasmhost.InitWasmHost(ctx)
36+
hostfunctions.RegisterHostFunctions(ctx)
37+
aws.Initialize(ctx)
38+
secrets.Initialize(ctx)
39+
storage.Initialize(ctx)
40+
db.Initialize(ctx)
41+
collections.InitializeIndexFactory(ctx)
42+
manifestdata.MonitorManifestFile(ctx, func([]error) { hostfunctions.ShutdownPools() })
43+
pluginmanager.MonitorPlugins(ctx)
44+
graphql.Initialize()
45+
}
46+
47+
// Stops any services that need to be stopped when the runtime stops.
48+
func Stop(ctx context.Context) {
49+
50+
// NOTE: Stopping services also has an order dependency.
51+
// If you need to change the order or add new services, be sure to test thoroughly.
52+
53+
// Unlike start, these should each block until they are fully stopped.
54+
55+
collections.CloseIndexFactory(ctx)
56+
wasmhost.RuntimeInstance.Close(ctx)
57+
logger.Close()
58+
59+
db.Stop()
60+
hostfunctions.ShutdownPools()
61+
}

0 commit comments

Comments
 (0)