mirror of
https://github.com/wavetermdev/waveterm.git
synced 2024-12-21 16:38:23 +01:00
Fix initialdata bug (#1365)
There was a bug in main-server where we were returning Window from EnsureInitialData and then overwriting the layout on the active tab. The return values from this function are unclear. Some of the initial data was being set in the wcore.EnsureInitialData and some was being set in main, so I've moved all the logic into EnsureInitialData and now it just returns error.
This commit is contained in:
parent
82f53dc1fb
commit
7e49e14977
@ -29,7 +29,6 @@ import (
|
|||||||
"github.com/wavetermdev/waveterm/pkg/wconfig"
|
"github.com/wavetermdev/waveterm/pkg/wconfig"
|
||||||
"github.com/wavetermdev/waveterm/pkg/wcore"
|
"github.com/wavetermdev/waveterm/pkg/wcore"
|
||||||
"github.com/wavetermdev/waveterm/pkg/web"
|
"github.com/wavetermdev/waveterm/pkg/web"
|
||||||
"github.com/wavetermdev/waveterm/pkg/wlayout"
|
|
||||||
"github.com/wavetermdev/waveterm/pkg/wps"
|
"github.com/wavetermdev/waveterm/pkg/wps"
|
||||||
"github.com/wavetermdev/waveterm/pkg/wshrpc"
|
"github.com/wavetermdev/waveterm/pkg/wshrpc"
|
||||||
"github.com/wavetermdev/waveterm/pkg/wshrpc/wshremote"
|
"github.com/wavetermdev/waveterm/pkg/wshrpc/wshremote"
|
||||||
@ -276,7 +275,7 @@ func main() {
|
|||||||
log.Printf("error initializing wsh and shell-integration files: %v\n", err)
|
log.Printf("error initializing wsh and shell-integration files: %v\n", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
window, firstRun, err := wcore.EnsureInitialData()
|
err = wcore.EnsureInitialData()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error ensuring initial data: %v\n", err)
|
log.Printf("error ensuring initial data: %v\n", err)
|
||||||
return
|
return
|
||||||
@ -286,28 +285,7 @@ func main() {
|
|||||||
log.Printf("error clearing temp files: %v\n", err)
|
log.Printf("error clearing temp files: %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if firstRun {
|
|
||||||
migrateErr := wstore.TryMigrateOldHistory()
|
|
||||||
if migrateErr != nil {
|
|
||||||
log.Printf("error migrating old history: %v\n", migrateErr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if window != nil {
|
|
||||||
ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
|
|
||||||
defer cancelFn()
|
|
||||||
if !firstRun {
|
|
||||||
ws, err := wcore.GetWorkspace(ctx, window.WorkspaceId)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("error getting workspace: %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = wlayout.BootstrapNewWorkspaceLayout(ctx, ws)
|
|
||||||
if err != nil {
|
|
||||||
log.Panicf("error applying new window layout: %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
createMainWshClient()
|
createMainWshClient()
|
||||||
installShutdownSignalHandlers()
|
installShutdownSignalHandlers()
|
||||||
startupActivityUpdate()
|
startupActivityUpdate()
|
||||||
|
91
pkg/wcore/block.go
Normal file
91
pkg/wcore/block.go
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
package wcore
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/wavetermdev/waveterm/pkg/blockcontroller"
|
||||||
|
"github.com/wavetermdev/waveterm/pkg/panichandler"
|
||||||
|
"github.com/wavetermdev/waveterm/pkg/telemetry"
|
||||||
|
"github.com/wavetermdev/waveterm/pkg/waveobj"
|
||||||
|
"github.com/wavetermdev/waveterm/pkg/wps"
|
||||||
|
"github.com/wavetermdev/waveterm/pkg/wshrpc"
|
||||||
|
"github.com/wavetermdev/waveterm/pkg/wstore"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateSubBlock(ctx context.Context, blockId string, blockDef *waveobj.BlockDef) (*waveobj.Block, error) {
|
||||||
|
if blockDef == nil {
|
||||||
|
return nil, fmt.Errorf("blockDef is nil")
|
||||||
|
}
|
||||||
|
if blockDef.Meta == nil || blockDef.Meta.GetString(waveobj.MetaKey_View, "") == "" {
|
||||||
|
return nil, fmt.Errorf("no view provided for new block")
|
||||||
|
}
|
||||||
|
blockData, err := wstore.CreateSubBlock(ctx, blockId, blockDef)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error creating sub block: %w", err)
|
||||||
|
}
|
||||||
|
return blockData, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateBlock(ctx context.Context, tabId string, blockDef *waveobj.BlockDef, rtOpts *waveobj.RuntimeOpts) (*waveobj.Block, error) {
|
||||||
|
if blockDef == nil {
|
||||||
|
return nil, fmt.Errorf("blockDef is nil")
|
||||||
|
}
|
||||||
|
if blockDef.Meta == nil || blockDef.Meta.GetString(waveobj.MetaKey_View, "") == "" {
|
||||||
|
return nil, fmt.Errorf("no view provided for new block")
|
||||||
|
}
|
||||||
|
blockData, err := wstore.CreateBlock(ctx, tabId, blockDef, rtOpts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error creating block: %w", err)
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
defer panichandler.PanicHandler("CreateBlock:telemetry")
|
||||||
|
blockView := blockDef.Meta.GetString(waveobj.MetaKey_View, "")
|
||||||
|
if blockView == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
|
||||||
|
defer cancelFn()
|
||||||
|
telemetry.UpdateActivity(tctx, wshrpc.ActivityUpdate{
|
||||||
|
Renderers: map[string]int{blockView: 1},
|
||||||
|
})
|
||||||
|
}()
|
||||||
|
return blockData, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteBlock(ctx context.Context, blockId string) error {
|
||||||
|
block, err := wstore.DBMustGet[*waveobj.Block](ctx, blockId)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error getting block: %w", err)
|
||||||
|
}
|
||||||
|
if block == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if len(block.SubBlockIds) > 0 {
|
||||||
|
for _, subBlockId := range block.SubBlockIds {
|
||||||
|
err := DeleteBlock(ctx, subBlockId)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error deleting subblock %s: %w", subBlockId, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = wstore.DeleteBlock(ctx, blockId)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error deleting block: %w", err)
|
||||||
|
}
|
||||||
|
go blockcontroller.StopBlockController(blockId)
|
||||||
|
sendBlockCloseEvent(blockId)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendBlockCloseEvent(blockId string) {
|
||||||
|
waveEvent := wps.WaveEvent{
|
||||||
|
Event: wps.Event_BlockClose,
|
||||||
|
Scopes: []string{
|
||||||
|
waveobj.MakeORef(waveobj.OType_Block, blockId).String(),
|
||||||
|
},
|
||||||
|
Data: blockId,
|
||||||
|
}
|
||||||
|
wps.Broker.Publish(waveEvent)
|
||||||
|
}
|
@ -11,12 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/wavetermdev/waveterm/pkg/blockcontroller"
|
|
||||||
"github.com/wavetermdev/waveterm/pkg/panichandler"
|
|
||||||
"github.com/wavetermdev/waveterm/pkg/telemetry"
|
|
||||||
"github.com/wavetermdev/waveterm/pkg/waveobj"
|
"github.com/wavetermdev/waveterm/pkg/waveobj"
|
||||||
"github.com/wavetermdev/waveterm/pkg/wps"
|
|
||||||
"github.com/wavetermdev/waveterm/pkg/wshrpc"
|
|
||||||
"github.com/wavetermdev/waveterm/pkg/wstore"
|
"github.com/wavetermdev/waveterm/pkg/wstore"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -28,90 +23,54 @@ import (
|
|||||||
const DefaultTimeout = 2 * time.Second
|
const DefaultTimeout = 2 * time.Second
|
||||||
const DefaultActivateBlockTimeout = 60 * time.Second
|
const DefaultActivateBlockTimeout = 60 * time.Second
|
||||||
|
|
||||||
func DeleteBlock(ctx context.Context, blockId string) error {
|
// Ensures that the initial data is present in the store, creates an initial window if needed
|
||||||
block, err := wstore.DBMustGet[*waveobj.Block](ctx, blockId)
|
func EnsureInitialData() error {
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error getting block: %w", err)
|
|
||||||
}
|
|
||||||
if block == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if len(block.SubBlockIds) > 0 {
|
|
||||||
for _, subBlockId := range block.SubBlockIds {
|
|
||||||
err := DeleteBlock(ctx, subBlockId)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error deleting subblock %s: %w", subBlockId, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
err = wstore.DeleteBlock(ctx, blockId)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error deleting block: %w", err)
|
|
||||||
}
|
|
||||||
go blockcontroller.StopBlockController(blockId)
|
|
||||||
sendBlockCloseEvent(blockId)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func sendBlockCloseEvent(blockId string) {
|
|
||||||
waveEvent := wps.WaveEvent{
|
|
||||||
Event: wps.Event_BlockClose,
|
|
||||||
Scopes: []string{
|
|
||||||
waveobj.MakeORef(waveobj.OType_Block, blockId).String(),
|
|
||||||
},
|
|
||||||
Data: blockId,
|
|
||||||
}
|
|
||||||
wps.Broker.Publish(waveEvent)
|
|
||||||
}
|
|
||||||
|
|
||||||
// returns (new-window, first-time, error)
|
|
||||||
func EnsureInitialData() (*waveobj.Window, bool, error) {
|
|
||||||
// does not need to run in a transaction since it is called on startup
|
// does not need to run in a transaction since it is called on startup
|
||||||
ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
|
ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
|
||||||
defer cancelFn()
|
defer cancelFn()
|
||||||
firstRun := false
|
|
||||||
client, err := wstore.DBGetSingleton[*waveobj.Client](ctx)
|
client, err := wstore.DBGetSingleton[*waveobj.Client](ctx)
|
||||||
if err == wstore.ErrNotFound {
|
if err == wstore.ErrNotFound {
|
||||||
client, err = CreateClient(ctx)
|
client, err = CreateClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, fmt.Errorf("error creating client: %w", err)
|
return fmt.Errorf("error creating client: %w", err)
|
||||||
|
}
|
||||||
|
migrateErr := wstore.TryMigrateOldHistory()
|
||||||
|
if migrateErr != nil {
|
||||||
|
log.Printf("error migrating old history: %v\n", migrateErr)
|
||||||
}
|
}
|
||||||
firstRun = true
|
|
||||||
}
|
}
|
||||||
if client.TempOID == "" {
|
if client.TempOID == "" {
|
||||||
log.Println("client.TempOID is empty")
|
log.Println("client.TempOID is empty")
|
||||||
client.TempOID = uuid.NewString()
|
client.TempOID = uuid.NewString()
|
||||||
err = wstore.DBUpdate(ctx, client)
|
err = wstore.DBUpdate(ctx, client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, fmt.Errorf("error updating client: %w", err)
|
return fmt.Errorf("error updating client: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Printf("clientid: %s\n", client.OID)
|
log.Printf("clientid: %s\n", client.OID)
|
||||||
if len(client.WindowIds) == 1 {
|
if len(client.WindowIds) == 1 {
|
||||||
log.Println("client has one window")
|
log.Println("client has one window")
|
||||||
window := CheckAndFixWindow(ctx, client.WindowIds[0])
|
CheckAndFixWindow(ctx, client.WindowIds[0])
|
||||||
if window != nil {
|
return nil
|
||||||
return window, firstRun, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if len(client.WindowIds) > 0 {
|
if len(client.WindowIds) > 0 {
|
||||||
log.Println("client has windows")
|
log.Println("client has windows")
|
||||||
return nil, false, nil
|
return nil
|
||||||
}
|
}
|
||||||
log.Println("client has no windows, creating default workspace")
|
log.Println("client has no windows, creating default workspace")
|
||||||
defaultWs, err := CreateWorkspace(ctx, "Default workspace", "circle", "green")
|
defaultWs, err := CreateWorkspace(ctx, "Default workspace", "circle", "green")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, fmt.Errorf("error creating default workspace: %w", err)
|
return fmt.Errorf("error creating default workspace: %w", err)
|
||||||
}
|
}
|
||||||
_, err = CreateTab(ctx, defaultWs.OID, "", true)
|
_, err = CreateTab(ctx, defaultWs.OID, "", true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, fmt.Errorf("error creating tab: %w", err)
|
return fmt.Errorf("error creating tab: %w", err)
|
||||||
}
|
}
|
||||||
window, err := CreateWindow(ctx, nil, defaultWs.OID)
|
_, err = CreateWindow(ctx, nil, defaultWs.OID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, fmt.Errorf("error creating window: %w", err)
|
return fmt.Errorf("error creating window: %w", err)
|
||||||
}
|
}
|
||||||
return window, firstRun, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateClient(ctx context.Context) (*waveobj.Client, error) {
|
func CreateClient(ctx context.Context) (*waveobj.Client, error) {
|
||||||
@ -126,46 +85,6 @@ func CreateClient(ctx context.Context) (*waveobj.Client, error) {
|
|||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateSubBlock(ctx context.Context, blockId string, blockDef *waveobj.BlockDef) (*waveobj.Block, error) {
|
|
||||||
if blockDef == nil {
|
|
||||||
return nil, fmt.Errorf("blockDef is nil")
|
|
||||||
}
|
|
||||||
if blockDef.Meta == nil || blockDef.Meta.GetString(waveobj.MetaKey_View, "") == "" {
|
|
||||||
return nil, fmt.Errorf("no view provided for new block")
|
|
||||||
}
|
|
||||||
blockData, err := wstore.CreateSubBlock(ctx, blockId, blockDef)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error creating sub block: %w", err)
|
|
||||||
}
|
|
||||||
return blockData, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func CreateBlock(ctx context.Context, tabId string, blockDef *waveobj.BlockDef, rtOpts *waveobj.RuntimeOpts) (*waveobj.Block, error) {
|
|
||||||
if blockDef == nil {
|
|
||||||
return nil, fmt.Errorf("blockDef is nil")
|
|
||||||
}
|
|
||||||
if blockDef.Meta == nil || blockDef.Meta.GetString(waveobj.MetaKey_View, "") == "" {
|
|
||||||
return nil, fmt.Errorf("no view provided for new block")
|
|
||||||
}
|
|
||||||
blockData, err := wstore.CreateBlock(ctx, tabId, blockDef, rtOpts)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error creating block: %w", err)
|
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
defer panichandler.PanicHandler("CreateBlock:telemetry")
|
|
||||||
blockView := blockDef.Meta.GetString(waveobj.MetaKey_View, "")
|
|
||||||
if blockView == "" {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
tctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
|
|
||||||
defer cancelFn()
|
|
||||||
telemetry.UpdateActivity(tctx, wshrpc.ActivityUpdate{
|
|
||||||
Renderers: map[string]int{blockView: 1},
|
|
||||||
})
|
|
||||||
}()
|
|
||||||
return blockData, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetClientData(ctx context.Context) (*waveobj.Client, error) {
|
func GetClientData(ctx context.Context) (*waveobj.Client, error) {
|
||||||
clientData, err := wstore.DBGetSingleton[*waveobj.Client](ctx)
|
clientData, err := wstore.DBGetSingleton[*waveobj.Client](ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user