Skip to content
Snippets Groups Projects
Commit 357d2cd0 authored by Bernardo Cardoso's avatar Bernardo Cardoso
Browse files

Allow APP to fully stop xxDK stop workers + unload Cmix state

parent c04e6940
No related branches found
No related tags found
1 merge request!143Some changes for Phoenixx
......@@ -22,6 +22,7 @@ import (
"gitlab.com/elixxir/xxdk-wasm/logging"
"gitlab.com/elixxir/xxdk-wasm/storage"
"gitlab.com/elixxir/xxdk-wasm/wasm"
"gitlab.com/elixxir/xxdk-wasm/worker"
)
func main() {
......@@ -77,6 +78,13 @@ var wasmCmd = &cobra.Command{
},
}
func stopWorkers(_ js.Value, _ []js.Value) any {
// Stop all existing managers
jww.INFO.Printf("Stopping xxDK WebAssembly workers...")
worker.Tracker.Stop()
return nil
}
// setGlobals enables all global functions to be accessible to Javascript.
func setGlobals() {
jww.INFO.Printf("Starting xxDK WebAssembly bindings.")
......@@ -166,6 +174,7 @@ func setGlobals() {
js.Global().Set("LoadCmix", js.FuncOf(wasm.LoadCmix))
js.Global().Set("LoadSynchronizedCmix",
js.FuncOf(wasm.LoadSynchronizedCmix))
js.Global().Set("UnloadCmix", js.FuncOf(wasm.UnloadCmix))
// wasm/delivery.go
js.Global().Set("SetDashboardURL", js.FuncOf(wasm.SetDashboardURL))
......@@ -272,6 +281,9 @@ func setGlobals() {
// wasm/rpc.go
js.Global().Set("RPCSend", js.FuncOf(wasm.RPCSend))
// Stop all existing workers (except logfile worker)
js.Global().Set("StopWorkers", js.FuncOf(stopWorkers))
}
var (
......
......@@ -233,6 +233,18 @@ func LoadSynchronizedCmix(_ js.Value, args []js.Value) any {
return utils.CreatePromise(promiseFn)
}
// UnloadCmix will unload an existing cMix instance
//
// Parameters:
// - args[0] - ID of [Cmix] object in tracker (int). This can be retrieved
// using [Cmix.GetID].
//
// Returns error or nil
func UnloadCmix(_ js.Value, args []js.Value) any {
cmixID := args[0].Int()
return bindings.DeleteCmixInstance(cmixID)
}
// GetID returns the ID for this [bindings.Cmix] in the cmixTracker.
//
// Returns:
......
......@@ -10,6 +10,7 @@
package worker
import (
"sync"
"syscall/js"
"time"
......@@ -38,6 +39,45 @@ type Manager struct {
w Worker
}
// Keep track of all managers created so that they can be stopped
// by the main WASM thread
type ManagersTracker struct {
tracked map[int]*Manager
count int
mux sync.Mutex
}
// Only add managers locally
func (mt *ManagersTracker) add(m *Manager) {
mt.mux.Lock()
defer mt.mux.Unlock()
id := mt.count
mt.count++
mt.tracked[id] = m
}
// Stop all managers except logger
func (mt *ManagersTracker) Stop() {
mt.mux.Lock()
defer mt.mux.Unlock()
for id, m := range mt.tracked {
name := m.Name()
if name == "xxdkLogFileWorker-main" {
// Don't stop the logfile manager
continue
}
m.Stop()
delete(mt.tracked, id)
}
}
var Tracker = &ManagersTracker{
tracked: make(map[int]*Manager),
count: 0,
}
// NewManager generates a new Manager. This functions will only return once
// communication with the worker has been established.
func NewManager(aURL, name string, messageLogging bool) (*Manager, error) {
......@@ -58,6 +98,8 @@ func NewManager(aURL, name string, messageLogging bool) (*Manager, error) {
w: w,
}
Tracker.add(m)
// Register a callback that will receive initial message from worker
// indicating that it is ready
ready := make(chan struct{})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment