@@ -5,21 +5,25 @@ package blockcontroller
55
66import (
77 "context"
8+ "encoding/json"
89 "fmt"
910 "io"
1011 "log"
12+ "net/http"
1113 "os"
1214 "os/exec"
1315 "path/filepath"
1416 "runtime"
1517 "strings"
1618 "sync"
1719 "syscall"
20+ "time"
1821
1922 "github.com/wavetermdev/waveterm/pkg/utilds"
2023 "github.com/wavetermdev/waveterm/pkg/wavebase"
2124 "github.com/wavetermdev/waveterm/pkg/waveobj"
2225 "github.com/wavetermdev/waveterm/pkg/wps"
26+ "github.com/wavetermdev/waveterm/pkg/wstore"
2327 "github.com/wavetermdev/waveterm/tsunami/build"
2428)
2529
@@ -81,6 +85,46 @@ func getCachesDir() string {
8185 return cacheDir
8286}
8387
88+ func (c * TsunamiController ) fetchAndSetSchemas (port int ) {
89+ url := fmt .Sprintf ("http://localhost:%d/api/schemas" , port )
90+ client := & http.Client {
91+ Timeout : 10 * time .Second ,
92+ }
93+
94+ resp , err := client .Get (url )
95+ if err != nil {
96+ log .Printf ("TsunamiController: failed to fetch schemas from %s: %v" , url , err )
97+ return
98+ }
99+ defer resp .Body .Close ()
100+
101+ if resp .StatusCode != http .StatusOK {
102+ log .Printf ("TsunamiController: received non-200 status %d from %s" , resp .StatusCode , url )
103+ return
104+ }
105+
106+ var schemas any
107+ if err := json .NewDecoder (resp .Body ).Decode (& schemas ); err != nil {
108+ log .Printf ("TsunamiController: failed to decode schemas response: %v" , err )
109+ return
110+ }
111+
112+ blockRef := waveobj .MakeORef (waveobj .OType_Block , c .blockId )
113+ wstore .SetRTInfo (blockRef , map [string ]any {
114+ "tsunami:schemas" : schemas ,
115+ })
116+
117+ log .Printf ("TsunamiController: successfully fetched and cached schemas for block %s" , c .blockId )
118+ }
119+
120+ func (c * TsunamiController ) clearSchemas () {
121+ blockRef := waveobj .MakeORef (waveobj .OType_Block , c .blockId )
122+ wstore .SetRTInfo (blockRef , map [string ]any {
123+ "tsunami:schemas" : nil ,
124+ })
125+ log .Printf ("TsunamiController: cleared schemas for block %s" , c .blockId )
126+ }
127+
84128func getTsunamiAppCachePath (scope string , appName string , osArch string ) (string , error ) {
85129 cachesDir := getCachesDir ()
86130 tsunamiCacheDir := filepath .Join (cachesDir , "tsunami-build-cache" )
@@ -229,6 +273,11 @@ func (c *TsunamiController) Start(ctx context.Context, blockMeta waveobj.MetaMap
229273 })
230274 go c .sendStatusUpdate ()
231275
276+ // Asynchronously fetch schemas after port is detected
277+ go func () {
278+ c .fetchAndSetSchemas (tsunamiProc .Port )
279+ }()
280+
232281 // Monitor process completion
233282 go func () {
234283 <- tsunamiProc .WaitCh
@@ -240,6 +289,7 @@ func (c *TsunamiController) Start(ctx context.Context, blockMeta waveobj.MetaMap
240289 c .port = 0
241290 c .exitCode = exitCodeFromWaitErr (tsunamiProc .WaitRtn )
242291 })
292+ c .clearSchemas ()
243293 go c .sendStatusUpdate ()
244294 }
245295 c .runLock .Unlock ()
@@ -273,6 +323,7 @@ func (c *TsunamiController) Stop(graceful bool, newStatus string) error {
273323 c .status = newStatus
274324 c .port = 0
275325 })
326+ c .clearSchemas ()
276327 go c .sendStatusUpdate ()
277328 return nil
278329}
0 commit comments