-
Notifications
You must be signed in to change notification settings - Fork 6
/
BackendSync.ts
473 lines (415 loc) · 18.2 KB
/
BackendSync.ts
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import {
API_URL,
BACKEND_MIN_VERSION,
SETUP_ISSUE_BACKEND_UPDATE_NEEDED,
SETUP_ISSUE_GIT_NOT_INSTALLED,
SETUP_ISSUE_SUIBASE_NOT_INSTALLED,
SETUP_ISSUE_SUIBASE_NOT_ON_PATH,
WEBVIEW_BACKEND,
WORKDIRS_KEYS,
WORKDIR_IDX_TESTNET,
} from "./common/Consts";
import { Mutex } from "async-mutex";
import { BaseWebview } from "./bases/BaseWebview";
import {
RequestWorkdirPackages,
RequestWorkdirStatus,
UpdateVersions,
UpdateWorkdirPackages,
UpdateWorkdirStatus,
} from "./common/ViewMessages";
import { SuibaseJson, SuibaseJsonVersions } from "./common/SuibaseJson";
import { SuibaseExec } from "./SuibaseExec";
import * as vscode from "vscode";
import { LogChannels } from "./LogChannels";
import * as semver from "semver";
// One instance per workdir, instantiated in order as the elements of WORKDIRS_KEYS.
class BackendWorkdirTracking {
versions: SuibaseJsonVersions; // Result from getVersions backend call.
workdirStatus: SuibaseJson; // Result from getWorkdirStatus backend call.
workdirPackages: SuibaseJson; // Result from getWorkdirPackages backend call.
constructor() {
this.versions = new SuibaseJsonVersions();
this.workdirStatus = new SuibaseJson();
this.workdirPackages = new SuibaseJson();
}
}
export class BackendSync {
private static sInstance?: BackendSync;
private mWorkdirTrackings: BackendWorkdirTracking[] = []; // One instance per workdir, align with WORKDIRS_KEYS.
private mForceRefreshOnNextReconnect: boolean; // Force some refresh when there was a lost of backend connection.
private activeWorkdir: string = WORKDIRS_KEYS[WORKDIR_IDX_TESTNET];
private consecutiveNotRespondingCount = 0;
// Singleton
private constructor() {
this.mForceRefreshOnNextReconnect = false;
// Create one instance of BackendWorkdirTracking for each element in WORKDIRS_KEYS.
for (const {} of WORKDIRS_KEYS) {
this.mWorkdirTrackings.push(new BackendWorkdirTracking());
}
}
public static activate() {
if (!BackendSync.sInstance) {
BackendSync.getInstance();
} else {
console.error("BackendSync.activate() called more than once");
}
}
public static deactivate() {
const instance = BackendSync.sInstance;
if (instance) {
instance.mWorkdirTrackings = [];
}
BackendSync.sInstance = undefined;
}
public static getInstance(): BackendSync {
if (!BackendSync.sInstance) {
BackendSync.sInstance = new BackendSync();
// Initialize callback for all Webview messages.
BaseWebview.setBackendSyncMessageCallback(
BackendSync.sInstance.handleViewMessage.bind(BackendSync.sInstance)
);
// Start periodic sync with backend.
setTimeout(() => {
if (BackendSync.sInstance) {
BackendSync.sInstance.syncLoop();
}
}, 1);
}
return BackendSync.sInstance;
}
public handleViewMessage(message: any): void {
try {
if (message.name === "ForceVersionsRefresh" || message.name === "InitView") {
this.forceRefresh();
} else if (message.name === "WorkdirCommand") {
let workdir = "";
if (message.workdirIdx >= 0 && message.workdirIdx < WORKDIRS_KEYS.length) {
workdir = WORKDIRS_KEYS[message.workdirIdx];
}
void this.fetchWorkdirCommand(workdir, message.command);
} else if (message.name === "RequestWorkdirStatus") {
void this.replyWorkdirStatus(message as RequestWorkdirStatus);
} else if (message.name === "RequestWorkdirPackages") {
void this.replyWorkdirPackages(message as RequestWorkdirPackages);
} else if (message.name === "OpenDashboardPanel") {
// Call the VSCode command "suibase.dashboard" to open/reveal the dashboard panel.
vscode.commands.executeCommand("suibase.dashboard").then(undefined, (error) => {
console.error(`Error executing suibase.dashboard command: ${error}`);
});
}
} catch (error) {
console.error(`Error in handleViewMessage: ${JSON.stringify(error)}`);
}
}
// Allow to trig a force refresh of all backend states.
// Can be called safely from anywhere.
public forceRefresh(): void {
this.syncLoop(true);
}
private loopMutex: Mutex = new Mutex();
private syncLoop(forceRefresh = false): void {
//console.log("StateLoop::_sync_loop() force_refresh=" + force_refresh);
// Used for calling the loop() async function when the caller does
// not care for the returned promise or error.
//
// This also eliminate eslint warning when the returned promise is unused.
this.asyncLoop(forceRefresh).catch((err) => console.log(err));
}
private async asyncLoop(forceRefresh: boolean): Promise<void> {
await this.loopMutex.runExclusive(async () => {
try {
await this.update(forceRefresh);
} catch (error) {
console.error(`Catch done in asyncLoop: ${JSON.stringify(error)}`);
}
if (forceRefresh === false) {
// Schedule for the periodical update.
setTimeout(() => {
this.syncLoop(forceRefresh);
}, 600); // milliseconds
}
}); // End of loop_mutex section
}
private async fetchBackend<T = any>(method: string, params: Record<string, any> = {}): Promise<T> {
// Do a POST request equivalent to:
// curl -H "Content-Type: application/json" --data '{ "id":1, "jsonrpc":"2.0", "method":"getVersions", "params": {"workdir":"localnet"}}' http://0.0.0.0:44399
//
// On error, throw an exception.
// On success, return the parsed JSON response.
const headers = {
// eslint-disable-next-line @typescript-eslint/naming-convention
"Content-Type": "application/json",
};
const body = {
id: 1,
jsonrpc: "2.0",
method: method,
params: params,
};
try {
const fetch = await import("node-fetch").then((fetch) => fetch.default);
const response = await fetch(API_URL, {
method: "POST",
headers: headers,
body: JSON.stringify(body),
});
if (!response.ok) {
throw new Error(`${method} ${JSON.stringify(params)} not ok`);
}
const json: T = (await response.json()) as T;
BackendSync.validateHeader(json, method);
return json;
} catch (error) {
const errorMsg = `Error in fetchBackend ${method} ${JSON.stringify(params)}: ${JSON.stringify(error)}`;
console.error(errorMsg);
throw error;
}
}
private async fetchGetVersions(workdir: string) {
// TODO Use BackendWorkdirTracking to detect and ignore out-of-order responses.
return await this.fetchBackend("getVersions", { workdir: workdir });
}
private async fetchGetWorkdirStatus(workdir: string) {
// TODO Use BackendWorkdirTracking to detect and ignore out-of-order responses.
return await this.fetchBackend("getWorkdirStatus", { workdir: workdir });
}
private async fetchGetWorkdirPackages(workdir: string) {
// TODO Use BackendWorkdirTracking to detect and ignore out-of-order responses.
return await this.fetchBackend("getWorkdirPackages", { workdir: workdir });
}
private async fetchWorkdirCommand(workdir: string, command: string) {
return await this.fetchBackend("workdirCommand", { workdir: workdir, command: command });
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private static validateHeader(json: any, methodExpected: string) {
// Throw an error on any problem detected.
if (json.jsonrpc !== "2.0") {
throw new Error("Invalid JSON-RPC version");
}
if (json.result.header.method !== methodExpected) {
throw new Error("Invalid method");
}
}
public async update(forceRefresh: boolean) {
// Get the global states from the backend.
// The VersionsUpdate message is periodically pushed to the views.
//
// The views then identify if they need to synchronize further with the extension
// and trig update requests messages as needed.
try {
await this.updateUsingBackend(forceRefresh);
} catch (error) {
// Do nothing, assume the caller will retry later.
}
}
private async diagnoseBackendError(workdirIdx: number) {
// This is a helper function to diagnose the backend error.
//
// It may attempts fixes, and it is assumed the caller will
// retry to contact the backend periodically until success.
//
// It will send a message to the views to display the error message.
// For now, always broadcast problems to all views.
this.mForceRefreshOnNextReconnect = true;
const msg = new UpdateVersions(WEBVIEW_BACKEND, workdirIdx, undefined);
const sb = SuibaseExec.getInstance();
let isUnknownErr = false;
if (sb === undefined) {
msg.setSetupIssue("Internal error. Shell commands failed.");
} else if ((await sb.isGitInstalled()) === false) {
msg.setSetupIssue(SETUP_ISSUE_GIT_NOT_INSTALLED);
} else if ((await sb.isSuibaseInstalled()) === false) {
msg.setSetupIssue(SETUP_ISSUE_SUIBASE_NOT_INSTALLED);
} else if ((await sb.isSuibaseOnPath()) === false) {
msg.setSetupIssue(SETUP_ISSUE_SUIBASE_NOT_ON_PATH + ",homedir=" + SuibaseExec.getHomedir());
} else if ((await sb.isSuibaseBackendRunning()) === false) {
// Start the backend daemon if safe to do...
if ((await sb.isSuibaseBackendUpgrading()) === true) {
// Do not attempt to start the daemon if a backend script is in
// the process of building/upgrading it.
msg.setSetupIssue("Suibase backend upgrading...");
} else if ((await sb.startDaemon()) === true) {
// Started, but need a moment for the backend to init/respond.
msg.setSetupIssue("Suibase backend connecting...");
} else {
isUnknownErr = true;
}
} else {
isUnknownErr = true;
}
if (isUnknownErr) {
// Report as "connecting" at first, and "not responding" after a while.
this.incrementNotRespondingCount(msg);
} else {
this.clearNotRespondingCount();
}
BaseWebview.broadcastMessage(msg);
}
private clearNotRespondingCount() {
this.consecutiveNotRespondingCount = 0;
}
private incrementNotRespondingCount(msg: UpdateVersions) {
this.consecutiveNotRespondingCount++;
if (this.consecutiveNotRespondingCount > 60) {
msg.setSetupIssue("Suibase backend not responding");
} else {
msg.setSetupIssue("Suibase backend connecting...");
}
}
private reportSetupIssue(issueMsg: string) {
this.mForceRefreshOnNextReconnect = true;
const msg = new UpdateVersions(WEBVIEW_BACKEND, 0, undefined);
msg.setSetupIssue(issueMsg);
BaseWebview.broadcastMessage(msg);
}
private async updateUsingBackend(forceRefresh: boolean) {
// Do getVersions for every possible workdir.
//
// TODO Optimize to do this to retrieve all only when dashboard is visible, otherwise just do the active?
// Iterate the WORKDIRS_KEYS
for (let workdirIdx = 0; workdirIdx < WORKDIRS_KEYS.length; workdirIdx++) {
const workdir = WORKDIRS_KEYS[workdirIdx];
let data = undefined;
try {
data = await this.fetchGetVersions(workdir);
} catch (error) {
await this.diagnoseBackendError(workdirIdx);
return;
}
if (data) {
// The backend did respond with something...
this.clearNotRespondingCount();
try {
//console.log("update versions: ", JSON.stringify(data));
// This is an example of data:
// {"jsonrpc":"2.0","result":{
// "header":{"method":"getVersions", "methodUuid":"...","dataUuid":"...","key":"localnet"},
// "versions":[{"method":"getWorkdirStatus","methodUuid":"...","dataUuid":"...","key":"localnet"}],
// "asuiSelection":"localnet"},
// "id":2}
//
// Each element of "versions" match the header you will find for each API method.
// (e.g. see header example in replyWorkdirStatus)
// Sanity check that the data is valid.
// - result should have a header with method "getVersions".
// - the key must match the workdir.
const header = data?.result?.header;
if (!header || header.method !== "getVersions" || header.key !== workdir) {
await this.diagnoseBackendError(workdirIdx);
return;
}
// Check the version of the backend, tell user to upgrade as needed.
// TODO Call to ~/suibase/update should be automated.
if (!header.semver || semver.lt(header.semver, BACKEND_MIN_VERSION)) {
this.reportSetupIssue(SETUP_ISSUE_BACKEND_UPDATE_NEEDED);
return;
}
// In the UI, the asuiSelection should always be one of "localnet", "mainnet", "testnet", "devnet".
//
// If the backend has trouble to identify the active, just default to the last known valid.
if (!data.result.asuiSelection || !WORKDIRS_KEYS.includes(data.result.asuiSelection)) {
data.result.asuiSelection = this.activeWorkdir;
} else {
// Got a valid active workdir from backend, make it the last known valid.
this.activeWorkdir = data.result.asuiSelection;
}
// Keep LogsChannel in-sync with the active workdir (user choice).
LogChannels.getInstance().setActiveWorkdir(this.activeWorkdir);
// Update the SuibaseJson instance for the workdir.
const workdirTracking = this.mWorkdirTrackings[workdirIdx];
const hasChanged = workdirTracking.versions.update(data.result);
// Broadcast UpdateVersions message to all the views when change detected or requested.
// The views will then decide if they need to synchronize further with the extension.
if (hasChanged || forceRefresh || this.mForceRefreshOnNextReconnect) {
this.mForceRefreshOnNextReconnect = false;
BaseWebview.broadcastMessage(new UpdateVersions(WEBVIEW_BACKEND, workdirIdx, data.result));
}
} catch (error) {
const errorMsg = `Error in updateUsingBackend: ${JSON.stringify(error)}. Data: ${JSON.stringify(
data
)}`;
console.error(errorMsg);
throw new Error(errorMsg);
}
}
}
// TODO Robustness. If there was no valid active workdir coming from the backend, then
// try to resolve it with the ~/suibase/workdirs/active symlink.
}
private async replyWorkdirStatus(message: RequestWorkdirStatus) {
const sender = message.sender; // Will reply to sender only.
const workdirIdx = message.workdirIdx;
const methodUuid = message.methodUuid;
const dataUuid = message.dataUuid;
let data = null;
try {
const workdir = WORKDIRS_KEYS[workdirIdx];
const workdirTracking = this.mWorkdirTrackings[workdirIdx];
const tracking = workdirTracking.workdirStatus;
data = tracking.getJson(); // Start with what is already in-memory (fetch only as needed)
if (!data || methodUuid !== tracking.getMethodUuid() || dataUuid !== tracking.getDataUuid()) {
const resp = await this.fetchGetWorkdirStatus(workdir);
data = resp.result;
workdirTracking.workdirStatus.update(data); // Save a copy of latest.
}
// This is an example of data:
// {"jsonrpc":"2.0","result":{"header":{"method":"getWorkdirStatus","methodUuid":"...","dataUuid":"...","key":"localnet"},
// "status":"OK","clientVersion":"1.23.0-db6e04d","asuiSelection":"localnet",
// "services":[{"label":"Localnet process","status":"OK","statusInfo":null,"helpInfo":null,"pid":null},
// {"label":"Faucet process","status":"OK","statusInfo":null,"helpInfo":null,"pid":null},
// {"label":"Proxy server","status":"OK","statusInfo":null,"helpInfo":null,"pid":null},
// {"label":"Multi-link RPC","status":"OK","statusInfo":null,"helpInfo":null,"pid":null}]},"id":2}
//
// Update the SuibaseJson instance for the workdir.
//console.log("replyWorkdirStatus: ", JSON.stringify(data));
BaseWebview.postMessageTo(sender, new UpdateWorkdirStatus(WEBVIEW_BACKEND, workdirIdx, data));
} catch (error) {
const errorMsg = `Error in replyWorkdirStatus: ${JSON.stringify(error)}. Data: ${JSON.stringify(data)}`;
console.error(errorMsg);
//throw new Error(errorMsg);
}
}
private async replyWorkdirPackages(message: RequestWorkdirPackages) {
const sender = message.sender; // Will reply to sender only.
const workdirIdx = message.workdirIdx;
const methodUuid = message.methodUuid;
const dataUuid = message.dataUuid;
let data = null;
try {
const workdir = WORKDIRS_KEYS[workdirIdx];
const workdirTracking = this.mWorkdirTrackings[workdirIdx];
const tracking = workdirTracking.workdirPackages;
data = tracking.getJson(); // Start with what is already in-memory (fetch only as needed)
if (!data || methodUuid !== tracking.getMethodUuid() || dataUuid !== tracking.getDataUuid()) {
const resp = await this.fetchGetWorkdirPackages(workdir);
data = resp.result;
workdirTracking.workdirPackages.update(data); // Save a copy of latest.
}
// This is an example of data:
// {"jsonrpc":"2.0",
// "result":
// {
// "header":{"method":"getWorkdirPackages","methodUuid":"...","dataUuid":"...","key":"localnet"},
// "moveConfigs":{"HPM...DTA":
// {"path":"/home/user/suibase/rust/demo-app/move",
// "latestPackage":{"packageId":"...","packageName":"demo","packageTimestamp":"1714698283265","initObjects":null},
// "olderPackages":[{"packageId":"...","packageName":"demo","packageTimestamp":"1714697847504","initObjects":null}],
// "trackingState":2
// }
// },
// }
// "id":2}
//
// Update the SuibaseJson instance for the workdir.
//console.log(`replyWorkdirPackages: ${JSON.stringify(data)}`);
BaseWebview.postMessageTo(sender, new UpdateWorkdirPackages(WEBVIEW_BACKEND, workdirIdx, data));
} catch (error) {
const errorMsg = `Error in replyWorkdirPackages: ${JSON.stringify(error)}. Data: ${JSON.stringify(
data
)}`;
console.error(errorMsg);
//throw new Error(errorMsg);
}
}
}