-
-
Notifications
You must be signed in to change notification settings - Fork 921
Expand file tree
/
Copy pathblockservice.go
More file actions
88 lines (76 loc) · 2.84 KB
/
blockservice.go
File metadata and controls
88 lines (76 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2025, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package blockservice
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/wavetermdev/waveterm/pkg/blockcontroller"
"github.com/wavetermdev/waveterm/pkg/filestore"
"github.com/wavetermdev/waveterm/pkg/tsgen/tsgenmeta"
"github.com/wavetermdev/waveterm/pkg/waveobj"
"github.com/wavetermdev/waveterm/pkg/wcore"
"github.com/wavetermdev/waveterm/pkg/wshrpc"
"github.com/wavetermdev/waveterm/pkg/wstore"
)
type BlockService struct{}
const DefaultTimeout = 2 * time.Second
var BlockServiceInstance = &BlockService{}
func (bs *BlockService) SendCommand_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
Desc: "send command to block",
ArgNames: []string{"blockid", "cmd"},
}
}
func (bs *BlockService) GetControllerStatus(ctx context.Context, blockId string) (*blockcontroller.BlockControllerRuntimeStatus, error) {
return blockcontroller.GetBlockControllerRuntimeStatus(blockId), nil
}
func (*BlockService) SaveTerminalState_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
Desc: "save the terminal state to a blockfile",
ArgNames: []string{"ctx", "blockId", "state", "stateType", "ptyOffset", "termSize"},
}
}
func (bs *BlockService) SaveTerminalState(ctx context.Context, blockId string, state string, stateType string, ptyOffset int64, termSize waveobj.TermSize) error {
_, err := wstore.DBMustGet[*waveobj.Block](ctx, blockId)
if err != nil {
return err
}
if stateType != "full" && stateType != "preview" {
return fmt.Errorf("invalid state type: %q", stateType)
}
// ignore MakeFile error (already exists is ok)
filestore.WFS.MakeFile(ctx, blockId, "cache:term:"+stateType, nil, wshrpc.FileOpts{})
err = filestore.WFS.WriteFile(ctx, blockId, "cache:term:"+stateType, []byte(state))
if err != nil {
return fmt.Errorf("cannot save terminal state: %w", err)
}
fileMeta := wshrpc.FileMeta{
"ptyoffset": ptyOffset,
"termsize": termSize,
}
err = filestore.WFS.WriteMeta(ctx, blockId, "cache:term:"+stateType, fileMeta, true)
if err != nil {
return fmt.Errorf("cannot save terminal state meta: %w", err)
}
return nil
}
func (*BlockService) CleanupOrphanedBlocks_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
Desc: "queue a layout action to cleanup orphaned blocks in the tab",
ArgNames: []string{"ctx", "tabId"},
}
}
func (bs *BlockService) CleanupOrphanedBlocks(ctx context.Context, tabId string) (waveobj.UpdatesRtnType, error) {
ctx = waveobj.ContextWithUpdates(ctx)
layoutAction := waveobj.LayoutActionData{
ActionType: wcore.LayoutActionDataType_CleanupOrphaned,
ActionId: uuid.NewString(),
}
err := wcore.QueueLayoutActionForTab(ctx, tabId, layoutAction)
if err != nil {
return nil, fmt.Errorf("error queuing cleanup layout action: %w", err)
}
return waveobj.ContextGetUpdatesRtn(ctx), nil
}