Skip to content

Commit

Permalink
Show wasm metadata in logs (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint authored Mar 15, 2024
1 parent 5f3ef7f commit 6fe3557
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 74 deletions.
1 change: 1 addition & 0 deletions dgraph/dgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package dgraph
import (
"context"
"fmt"

"hmruntime/config"
"hmruntime/utils"
)
Expand Down
3 changes: 2 additions & 1 deletion functions/ashelpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package functions

import (
"bytes"
"hmruntime/testutils"
"testing"

"hmruntime/testutils"
)

// "Hello World" in Japanese
Expand Down
3 changes: 2 additions & 1 deletion functions/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
package functions

import (
"hmruntime/testutils"
"testing"
"time"

"hmruntime/testutils"
)

func Test_DateNow(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion functions/fncall.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"context"
"encoding/json"
"fmt"
"time"

"hmruntime/logger"
"hmruntime/schema"
"time"

"github.com/dgraph-io/gqlparser/ast"
wasm "github.com/tetratelabs/wazero/api"
Expand Down
14 changes: 8 additions & 6 deletions functions/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ package functions

import (
"context"
"reflect"
"strings"

"hmruntime/host"
"hmruntime/logger"
"hmruntime/schema"
"reflect"
"strings"
)

// map that holds the function info for each resolver
Expand Down Expand Up @@ -46,9 +47,10 @@ func registerFunctions(ctx context.Context, gqlSchema string) error {

// Build a map of resolvers to function info, including the plugin name.
// If there are function name conflicts between plugins, the last plugin loaded wins.
for pluginName, cm := range host.CompiledModules {
for pluginName, plugin := range host.Plugins {
for _, scma := range funcSchemas {
for _, fn := range cm.ExportedFunctions() {
module := *plugin.Module
for _, fn := range module.ExportedFunctions() {
fnName := fn.ExportNames()[0]
if strings.EqualFold(fnName, scma.FunctionName()) {
info := schema.FunctionInfo{PluginName: pluginName, Schema: scma}
Expand Down Expand Up @@ -78,8 +80,8 @@ func registerFunctions(ctx context.Context, gqlSchema string) error {
break
}
}
_, foundModule := host.CompiledModules[info.PluginName]
if !foundSchema || !foundModule {
_, foundPlugin := host.Plugins[info.PluginName]
if !foundSchema || !foundPlugin {
delete(FunctionsMap, resolver)
logger.Info(ctx).
Str("resolver", resolver).
Expand Down
5 changes: 3 additions & 2 deletions functions/schemaWatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ package functions
import (
"context"
"errors"
"net/url"
"time"

"hmruntime/config"
"hmruntime/dgraph"
"hmruntime/host"
"hmruntime/logger"
"net/url"
"time"
)

// Holds the current GraphQL schema
Expand Down
4 changes: 2 additions & 2 deletions host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import "github.com/tetratelabs/wazero"
// runtime instance for the WASM modules
var WasmRuntime wazero.Runtime

// map that holds the compiled modules for each plugin
var CompiledModules = make(map[string]wazero.CompiledModule)
// map that holds all of the loaded plugins, indexed by their name
var Plugins = make(map[string]Plugin)

// Channel used to signal that registration is needed
var RegistrationRequest chan bool = make(chan bool)
34 changes: 17 additions & 17 deletions plugins/loader.go → host/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
* Copyright 2023 Hypermode, Inc.
*/

package plugins
package host

import (
"context"
"fmt"
"hmruntime/aws"
"hmruntime/config"
"hmruntime/host"
"hmruntime/logger"
"os"
"path"
"regexp"
"strings"
"time"

"hmruntime/aws"
"hmruntime/config"
"hmruntime/logger"

"github.com/radovskyb/watcher"
)

Expand Down Expand Up @@ -45,9 +45,9 @@ func ReloadPlugins(ctx context.Context) error {
}

// Unload any plugins that are no longer present
for name := range host.CompiledModules {
for name := range Plugins {
if !loaded[name] {
err := unloadPluginModule(ctx, name)
err := unloadPlugin(ctx, name)
if err != nil {
return fmt.Errorf("failed to unload plugin '%s': %w", name, err)
}
Expand Down Expand Up @@ -124,7 +124,7 @@ func loadPlugins(ctx context.Context) (map[string]bool, error) {
}

for plugin := range plugins {
err := loadPluginModule(ctx, plugin)
err := loadPlugin(ctx, plugin)
if err != nil {
logger.Err(ctx, err).
Str("plugin", plugin).
Expand All @@ -143,7 +143,7 @@ func loadPlugins(ctx context.Context) (map[string]bool, error) {
// If the plugins path is a single plugin's base directory, load the single plugin.
if _, err := os.Stat(config.PluginsPath + "/build/debug.wasm"); err == nil {
pluginName := path.Base(config.PluginsPath)
err := loadPluginModule(ctx, pluginName)
err := loadPlugin(ctx, pluginName)
if err != nil {
logger.Err(ctx, err).
Str("plugin", pluginName).
Expand Down Expand Up @@ -177,7 +177,7 @@ func loadPlugins(ctx context.Context) (map[string]bool, error) {
}

// Load the plugin
err := loadPluginModule(ctx, pluginName)
err := loadPlugin(ctx, pluginName)
if err != nil {
logger.Err(ctx, err).
Str("plugin", pluginName).
Expand All @@ -201,7 +201,7 @@ func WatchForJsonChanges(ctx context.Context) error {
func WatchForPluginChanges(ctx context.Context) error {

if config.NoReload {
logger.Warn(ctx).Msg("Automatic plugin reloading is disabled. Restart the server to load new or modified plugins.")
logger.Warn(ctx).Msg("Automatic plugin reloading is disabled. Restart the server to load new or modified host.")
return nil
}

Expand Down Expand Up @@ -275,14 +275,14 @@ func watchDirectoryForPluginChanges(ctx context.Context) error {

switch evt.Op {
case watcher.Create, watcher.Write:
err = loadPluginModule(ctx, pluginName)
err = loadPlugin(ctx, pluginName)
if err != nil {
logger.Err(ctx, err).
Str("plugin", pluginName).
Msg("Failed to load plugin.")
}
case watcher.Remove:
err = unloadPluginModule(ctx, pluginName)
err = unloadPlugin(ctx, pluginName)
if err != nil {
logger.Err(ctx, err).
Str("plugin", pluginName).
Expand All @@ -291,7 +291,7 @@ func watchDirectoryForPluginChanges(ctx context.Context) error {
}

// Signal that we need to register functions
host.RegistrationRequest <- true
RegistrationRequest <- true

case err := <-w.Error:
logger.Err(ctx, err).Msg("Failure while watching plugin directory.")
Expand Down Expand Up @@ -440,7 +440,7 @@ func watchStorageForPluginChanges(ctx context.Context) error {
// Load/reload any new or modified plugins
for name, etag := range plugins {
if awsPlugins[name] != etag {
err := loadPluginModule(ctx, name)
err := loadPlugin(ctx, name)
if err != nil {
logger.Err(ctx, err).
Str("plugin", name).
Expand All @@ -454,7 +454,7 @@ func watchStorageForPluginChanges(ctx context.Context) error {
// Unload any plugins that are no longer present
for name := range awsPlugins {
if _, found := plugins[name]; !found {
err := unloadPluginModule(ctx, name)
err := unloadPlugin(ctx, name)
if err != nil {
logger.Err(ctx, err).
Str("plugin", name).
Expand All @@ -467,7 +467,7 @@ func watchStorageForPluginChanges(ctx context.Context) error {

// If anything changed, signal that we need to register functions
if changed {
host.RegistrationRequest <- true
RegistrationRequest <- true
}

select {
Expand Down
Loading

0 comments on commit 6fe3557

Please sign in to comment.