@@ -13,9 +13,11 @@ import (
1313 "context"
1414 "time"
1515
16+ "github.com/hypermodeinc/modus/runtime/db"
1617 "github.com/hypermodeinc/modus/runtime/logger"
1718 "github.com/hypermodeinc/modus/runtime/pluginmanager"
1819 "github.com/hypermodeinc/modus/runtime/plugins"
20+ "github.com/hypermodeinc/modus/runtime/wasmhost"
1921
2022 goakt "github.com/tochemey/goakt/v3/actor"
2123)
@@ -45,18 +47,36 @@ func Initialize(ctx context.Context) {
4547
4648 logger .Info (ctx ).Msg ("Actor system started." )
4749
48- pluginmanager .RegisterPluginLoadedCallback (reloadAgentActors )
50+ pluginmanager .RegisterPluginLoadedCallback (loadAgentActors )
4951}
5052
51- func reloadAgentActors (ctx context.Context , plugin * plugins.Plugin ) error {
52- for _ , pid := range _actorSystem .Actors () {
53+ func loadAgentActors (ctx context.Context , plugin * plugins.Plugin ) error {
54+ // reload modules for actors that are already running
55+ actors := _actorSystem .Actors ()
56+ runningAgents := make (map [string ]bool , len (actors ))
57+ for _ , pid := range actors {
5358 if actor , ok := pid .Actor ().(* WasmAgentActor ); ok {
59+ runningAgents [actor .agentId ] = true
5460 if err := actor .reloadModule (ctx , plugin ); err != nil {
5561 return err
5662 }
5763 }
5864 }
5965
66+ // spawn actors for agents with state in the database, that are not already running
67+ // TODO: when we scale out with GoAkt cluster mode, we'll need to decide which node is responsible for spawning the actor
68+ agents , err := db .QueryActiveAgents (ctx )
69+ if err != nil {
70+ logger .Err (ctx , err ).Msg ("Failed to query agents from database." )
71+ return err
72+ }
73+ host := wasmhost .GetWasmHost (ctx )
74+ for _ , agent := range agents {
75+ if ! runningAgents [agent .Id ] {
76+ spawnActorForAgent (host , plugin , agent .Id , agent .Name , true , & agent .Data )
77+ }
78+ }
79+
6080 return nil
6181}
6282
0 commit comments