@@ -11,6 +11,9 @@ package actors
1111
1212import (
1313 "context"
14+ "fmt"
15+ "os"
16+ "strconv"
1417 "time"
1518
1619 "github.com/hypermodeinc/modus/runtime/db"
@@ -20,31 +23,62 @@ import (
2023 "github.com/hypermodeinc/modus/runtime/wasmhost"
2124
2225 goakt "github.com/tochemey/goakt/v3/actor"
26+ goakt_static "github.com/tochemey/goakt/v3/discovery/static"
27+ goakt_remote "github.com/tochemey/goakt/v3/remote"
28+ "github.com/travisjeffery/go-dynaport"
2329)
2430
2531var _actorSystem goakt.ActorSystem
2632
2733func Initialize (ctx context.Context ) {
2834
29- actorLogger := newActorLogger (logger .Get (ctx ))
30-
31- actorSystem , err := goakt .NewActorSystem ("modus" ,
32- goakt .WithLogger (actorLogger ),
35+ opts := []goakt.Option {
36+ goakt .WithLogger (newActorLogger (logger .Get (ctx ))),
3337 goakt .WithCoordinatedShutdown (beforeShutdown ),
34- goakt .WithPassivationDisabled (), // TODO: enable passivation. Requires a persistence store in production for agent state.
35- goakt .WithActorInitTimeout (10 * time .Second ), // TODO: adjust this value, or make it configurable
36- goakt .WithActorInitMaxRetries (1 )) // TODO: adjust this value, or make it configurable
38+ goakt .WithPubSub (),
39+ goakt .WithPassivation (time .Second * 10 ), // TODO: adjust this value, or make it configurable
40+ goakt .WithActorInitTimeout (10 * time .Second ), // TODO: adjust this value, or make it configurable
41+ goakt .WithActorInitMaxRetries (1 ), // TODO: adjust this value, or make it configurable
42+ }
3743
38- if err != nil {
39- logger .Fatal (ctx ).Err (err ).Msg ("Failed to create actor system." )
44+ // NOTE: we're not relying on cluster mode yet. The below code block is for future use and testing purposes only.
45+ if clusterMode , _ := strconv .ParseBool (os .Getenv ("MODUS_USE_CLUSTER_MODE" )); clusterMode {
46+ // TODO: static discovery should really only be used for local development and testing.
47+ // In production, we should use a more robust discovery mechanism, such as Kubernetes or NATS.
48+ // See https://tochemey.gitbook.io/goakt/features/service-discovery
49+
50+ // We just get three random ports for now.
51+ // In prod, these will need to be configured so they are consistent across all nodes.
52+ ports := dynaport .Get (3 )
53+ var gossip_port = ports [0 ]
54+ var peers_port = ports [1 ]
55+ var remoting_port = ports [2 ]
56+
57+ disco := goakt_static .NewDiscovery (& goakt_static.Config {
58+ Hosts : []string {
59+ fmt .Sprintf ("localhost:%d" , gossip_port ),
60+ },
61+ })
62+
63+ opts = append (opts ,
64+ goakt .WithRemote (goakt_remote .NewConfig ("localhost" , remoting_port )),
65+ goakt .WithCluster (goakt .NewClusterConfig ().
66+ WithDiscovery (disco ).
67+ WithDiscoveryPort (gossip_port ).
68+ WithPeersPort (peers_port ).
69+ WithKinds (& wasmAgentActor {}, & subscriptionActor {}),
70+ ),
71+ )
4072 }
4173
42- if err := actorSystem .Start (ctx ); err != nil {
74+ if actorSystem , err := goakt .NewActorSystem ("modus" , opts ... ); err != nil {
75+ logger .Fatal (ctx ).Err (err ).Msg ("Failed to create actor system." )
76+ } else if err := actorSystem .Start (ctx ); err != nil {
4377 logger .Fatal (ctx ).Err (err ).Msg ("Failed to start actor system." )
78+ } else {
79+ _actorSystem = actorSystem
4480 }
4581
46- _actorSystem = actorSystem
47-
4882 logger .Info (ctx ).Msg ("Actor system started." )
4983
5084 pluginmanager .RegisterPluginLoadedCallback (loadAgentActors )
@@ -64,7 +98,8 @@ func loadAgentActors(ctx context.Context, plugin *plugins.Plugin) error {
6498 }
6599
66100 // 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
101+ // TODO: when we scale out to allow more nodes in the cluster, we'll need to decide
102+ // which node is responsible for spawning each actor.
68103 agents , err := db .QueryActiveAgents (ctx )
69104 if err != nil {
70105 logger .Err (ctx , err ).Msg ("Failed to query agents from database." )
@@ -73,7 +108,7 @@ func loadAgentActors(ctx context.Context, plugin *plugins.Plugin) error {
73108 host := wasmhost .GetWasmHost (ctx )
74109 for _ , agent := range agents {
75110 if ! runningAgents [agent .Id ] {
76- spawnActorForAgent (host , plugin , agent .Id , agent .Name , true , & agent .Data )
111+ spawnActorForAgentAsync (host , plugin , agent .Id , agent .Name , true , & agent .Data )
77112 }
78113 }
79114
0 commit comments