Skip to content

Commit

Permalink
defer WASM file parsing to start command
Browse files Browse the repository at this point in the history
Signed-off-by: JeffMboya <[email protected]>
  • Loading branch information
JeffMboya committed Dec 12, 2024
1 parent 405768b commit 4dba515
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
23 changes: 3 additions & 20 deletions cmd/proplet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const registryTimeout = 30 * time.Second

var (
wasmFilePath string
wasmBinary []byte
logLevel slog.Level
)

Expand Down Expand Up @@ -64,18 +63,11 @@ func run() error {
}
}

if hasWASMFile {
wasmBinary, err = loadWASMFile(wasmFilePath)
if err != nil {
return fmt.Errorf("failed to load WASM file: %w", err)
}
}

if cfg.RegistryURL == "" && wasmBinary == nil {
return errors.New("missing registry URL and WASM binary file")
if cfg.RegistryURL == "" && !hasWASMFile {
return errors.New("missing registry URL and WASM file")
}

service, err := proplet.NewService(ctx, cfg, wasmBinary, logger)
service, err := proplet.NewService(ctx, cfg, wasmFilePath, logger)
if err != nil {
return fmt.Errorf("service initialization error: %w", err)
}
Expand All @@ -100,15 +92,6 @@ func configureLogger(level string) *slog.Logger {
return slog.New(logHandler)
}

func loadWASMFile(path string) ([]byte, error) {
wasmBytes, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read WASM file: %w", err)
}

return wasmBytes, nil
}

func checkRegistryConnectivity(registryURL string) error {
ctx, cancel := context.WithTimeout(context.Background(), registryTimeout)
defer cancel()
Expand Down
22 changes: 20 additions & 2 deletions proplet/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type PropletService struct {
config Config
mqttService *MQTTService
runtime *WazeroRuntime
wasmFilePath string
wasmBinary []byte
chunks map[string][][]byte
chunkMetadata map[string]*ChunkPayload
Expand Down Expand Up @@ -104,7 +105,7 @@ func (w *WazeroRuntime) StopApp(ctx context.Context, appName string) error {
return nil
}

func NewService(ctx context.Context, cfg Config, wasmBinary []byte, logger *slog.Logger) (*PropletService, error) {
func NewService(ctx context.Context, cfg Config, wasmFilePath string, logger *slog.Logger) (*PropletService, error) {
mqttService, err := NewMQTTService(ctx, cfg, logger)
if err != nil {
return nil, fmt.Errorf("failed to initialize MQTT client: %w", err)
Expand All @@ -114,7 +115,7 @@ func NewService(ctx context.Context, cfg Config, wasmBinary []byte, logger *slog
config: cfg,
mqttService: mqttService,
runtime: NewWazeroRuntime(ctx),
wasmBinary: wasmBinary,
wasmFilePath: wasmFilePath,
chunks: make(map[string][][]byte),
chunkMetadata: make(map[string]*ChunkPayload),
}, nil
Expand Down Expand Up @@ -177,6 +178,14 @@ func (p *PropletService) handleStartCommand(ctx context.Context, _ string, msg m

logger.Info("Received start command", slog.String("app_name", startReq.AppName))

if p.wasmBinary == nil && p.wasmFilePath != "" {
p.wasmBinary, err = loadWASMFile(p.wasmFilePath)
if err != nil {
return fmt.Errorf("failed to load WASM file: %w", err)
}
logger.Info("WASM file loaded successfully", slog.String("path", p.wasmFilePath))
}

if p.wasmBinary != nil {
logger.Info("Using preloaded WASM binary", slog.String("app_name", startReq.AppName))
function, err := p.runtime.StartApp(ctx, startReq.AppName, p.wasmBinary, "add")
Expand Down Expand Up @@ -411,3 +420,12 @@ func (p *PropletService) registryUpdate(ctx context.Context, _ string, msg map[s

return nil
}

func loadWASMFile(path string) ([]byte, error) {
wasmBytes, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read WASM file: %w", err)
}

return wasmBytes, nil
}

0 comments on commit 4dba515

Please sign in to comment.