-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wasm): enable running wasm workloads on proplets
Signed-off-by: Rodney Osodo <[email protected]>
- Loading branch information
1 parent
201f1da
commit cb37230
Showing
22 changed files
with
684 additions
and
695 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ run: | |
timeout: 10m | ||
|
||
issues: | ||
exclude-dirs: | ||
- examples/ | ||
max-issues-per-linter: 100 | ||
max-same-issues: 100 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package proplet | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/absmach/propeller/proplet" | ||
) | ||
|
||
func StartProplet(ctx context.Context, cancel context.CancelFunc, cfg proplet.Config) error { | ||
var level slog.Level | ||
if err := level.UnmarshalText([]byte(cfg.LogLevel)); err != nil { | ||
return fmt.Errorf("failed to parse log level: %s", err.Error()) | ||
} | ||
logHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ | ||
Level: level, | ||
}) | ||
logger := slog.New(logHandler) | ||
slog.SetDefault(logger) | ||
|
||
go func() { | ||
sigChan := make(chan os.Signal, 1) | ||
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) | ||
sig := <-sigChan | ||
logger.Info("Received shutdown signal", slog.String("signal", sig.String())) | ||
cancel() | ||
}() | ||
|
||
if cfg.RegistryURL != "" { | ||
if err := checkRegistryConnectivity(ctx, cfg.RegistryURL, cfg.RegistryTimeout); err != nil { | ||
return errors.Join(errors.New("failed to connect to registry URL"), err) | ||
} | ||
|
||
logger.Info("successfully connected to registry URL", slog.String("url", cfg.RegistryURL)) | ||
} | ||
|
||
mqttClient, err := proplet.NewMQTTClient(ctx, cfg, logger) | ||
if err != nil { | ||
return errors.Join(errors.New("failed to initialize mqtt client"), err) | ||
} | ||
wazero := proplet.NewWazeroRuntime(logger, mqttClient, cfg.ChannelID) | ||
|
||
service, err := proplet.NewService(cfg, mqttClient, logger, wazero) | ||
if err != nil { | ||
return errors.Join(errors.New("failed to initialize service"), err) | ||
} | ||
|
||
if err := service.Run(ctx, logger); err != nil { | ||
return errors.Join(errors.New("failed to run service"), err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func checkRegistryConnectivity(ctx context.Context, registryURL string, registryTimeout time.Duration) error { | ||
ctx, cancel := context.WithTimeout(ctx, registryTimeout) | ||
defer cancel() | ||
|
||
client := http.DefaultClient | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, registryURL, http.NoBody) | ||
if err != nil { | ||
return fmt.Errorf("failed to create HTTP request: %w", err) | ||
} | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("failed to connect to registry URL: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("fegistry returned unexpected status: %d", resp.StatusCode) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package main | ||
|
||
//export add | ||
func add(x, y uint32) uint32 { | ||
return x + y | ||
} | ||
|
||
// main is required for the `wasi` target, even if it isn't used. | ||
// See https://wazero.io/languages/tinygo/#why-do-i-have-to-define-main | ||
func main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.