@inlang/sdk 0.34.2 → 0.34.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/dist/adapter/solidAdapter.test.js +1 -1
- package/dist/createMessagesQuery.d.ts +14 -2
- package/dist/createMessagesQuery.d.ts.map +1 -1
- package/dist/createMessagesQuery.js +338 -6
- package/dist/createMessagesQuery.test.js +133 -88
- package/dist/createNodeishFsWithWatcher.d.ts +1 -0
- package/dist/createNodeishFsWithWatcher.d.ts.map +1 -1
- package/dist/createNodeishFsWithWatcher.js +2 -2
- package/dist/createNodeishFsWithWatcher.test.js +8 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +12 -2
- package/dist/loadProject.d.ts.map +1 -1
- package/dist/loadProject.js +21 -487
- package/dist/persistence/filelock/acquireFileLock.d.ts +3 -0
- package/dist/persistence/filelock/acquireFileLock.d.ts.map +1 -0
- package/dist/persistence/filelock/acquireFileLock.js +109 -0
- package/dist/persistence/filelock/releaseLock.d.ts +3 -0
- package/dist/persistence/filelock/releaseLock.d.ts.map +1 -0
- package/dist/persistence/filelock/releaseLock.js +23 -0
- package/package.json +8 -8
- package/src/adapter/solidAdapter.test.ts +1 -1
- package/src/createMessagesQuery.test.ts +147 -109
- package/src/createMessagesQuery.ts +477 -8
- package/src/createNodeishFsWithWatcher.test.ts +13 -0
- package/src/createNodeishFsWithWatcher.ts +2 -2
- package/src/errors.ts +16 -4
- package/src/loadProject.ts +20 -666
- package/src/persistence/filelock/acquireFileLock.ts +124 -0
- package/src/persistence/filelock/releaseLock.ts +28 -0
package/dist/loadProject.js
CHANGED
|
@@ -1,25 +1,21 @@
|
|
|
1
1
|
import { resolveModules } from "./resolve-modules/index.js";
|
|
2
2
|
import { TypeCompiler, ValueErrorType } from "@sinclair/typebox/compiler";
|
|
3
|
-
import { ProjectSettingsFileJSONSyntaxError, ProjectSettingsFileNotFoundError, ProjectSettingsInvalidError,
|
|
3
|
+
import { ProjectSettingsFileJSONSyntaxError, ProjectSettingsFileNotFoundError, ProjectSettingsInvalidError, } from "./errors.js";
|
|
4
4
|
import { createRoot, createSignal, createEffect } from "./reactivity/solid.js";
|
|
5
5
|
import { createMessagesQuery } from "./createMessagesQuery.js";
|
|
6
6
|
import { createMessageLintReportsQuery } from "./createMessageLintReportsQuery.js";
|
|
7
|
-
import { ProjectSettings
|
|
7
|
+
import { ProjectSettings } from "./versionedInterfaces.js";
|
|
8
8
|
import { tryCatch } from "@inlang/result";
|
|
9
9
|
import { migrateIfOutdated } from "@inlang/project-settings/migration";
|
|
10
10
|
import { createNodeishFsWithAbsolutePaths } from "./createNodeishFsWithAbsolutePaths.js";
|
|
11
11
|
import { normalizePath } from "@lix-js/fs";
|
|
12
12
|
import { assertValidProjectPath } from "./validateProjectPath.js";
|
|
13
13
|
import { maybeMigrateToDirectory } from "./migrations/migrateToDirectory.js";
|
|
14
|
-
import { stringifyMessage as stringifyMessage } from "./storage/helper.js";
|
|
15
|
-
import { humanIdHash } from "./storage/human-id/human-readable-id.js";
|
|
16
|
-
import { createNodeishFsWithWatcher } from "./createNodeishFsWithWatcher.js";
|
|
17
14
|
import { maybeCreateFirstProjectId } from "./migrations/maybeCreateFirstProjectId.js";
|
|
18
15
|
import { capture } from "./telemetry/capture.js";
|
|
19
16
|
import { identifyProject } from "./telemetry/groupIdentify.js";
|
|
20
17
|
import _debug from "debug";
|
|
21
18
|
const debug = _debug("sdk:loadProject");
|
|
22
|
-
const debugLock = _debug("sdk:lockfile");
|
|
23
19
|
const settingsCompiler = TypeCompiler.Compile(ProjectSettings);
|
|
24
20
|
/**
|
|
25
21
|
* @param projectPath - Absolute path to the inlang settings file.
|
|
@@ -31,15 +27,6 @@ const settingsCompiler = TypeCompiler.Compile(ProjectSettings);
|
|
|
31
27
|
*/
|
|
32
28
|
export async function loadProject(args) {
|
|
33
29
|
const projectPath = normalizePath(args.projectPath);
|
|
34
|
-
const messageStates = {
|
|
35
|
-
messageDirtyFlags: {},
|
|
36
|
-
messageLoadHash: {},
|
|
37
|
-
isSaving: false,
|
|
38
|
-
currentSaveMessagesViaPlugin: undefined,
|
|
39
|
-
sheduledSaveMessages: undefined,
|
|
40
|
-
isLoading: false,
|
|
41
|
-
sheduledLoadMessagesViaPlugin: undefined,
|
|
42
|
-
};
|
|
43
30
|
// -- validation --------------------------------------------------------
|
|
44
31
|
// the only place where throwing is acceptable because the project
|
|
45
32
|
// won't even be loaded. do not throw anywhere else. otherwise, apps
|
|
@@ -111,52 +98,27 @@ export async function loadProject(args) {
|
|
|
111
98
|
// -- messages ----------------------------------------------------------
|
|
112
99
|
let settingsValue;
|
|
113
100
|
createEffect(() => (settingsValue = settings())); // workaround to not run effects twice (e.g. settings change + modules change) (I'm sure there exists a solid way of doing this, but I haven't found it yet)
|
|
114
|
-
// please don't use this as source of truth, use the query instead
|
|
115
|
-
// needed for granular linting
|
|
116
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- setMessages is not called directly we use the CRUD operations on the messageQuery to set the messages now
|
|
117
|
-
const [messages, setMessages] = createSignal([]);
|
|
118
101
|
const [loadMessagesViaPluginError, setLoadMessagesViaPluginError] = createSignal();
|
|
119
102
|
const [saveMessagesViaPluginError, setSaveMessagesViaPluginError] = createSignal();
|
|
120
|
-
const messagesQuery = createMessagesQuery(
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
// - the plugin loads messages -> reads the file messages.json -> start watching on messages.json -> updateMessages
|
|
140
|
-
updateMessages: () => {
|
|
141
|
-
// preserving console.logs as comments pending #
|
|
142
|
-
debug("load messages because of a change in the message.json files");
|
|
143
|
-
loadMessagesViaPlugin(fsWithWatcher, messageLockDirPath, messageStates, messagesQuery, settings(), // NOTE we bang here - we don't expect the settings to become null during the livetime of a project
|
|
144
|
-
resolvedPluginApi)
|
|
145
|
-
.catch((e) => setLoadMessagesViaPluginError(new PluginLoadMessagesError({ cause: e })))
|
|
146
|
-
.then(() => {
|
|
147
|
-
if (loadMessagesViaPluginError() !== undefined) {
|
|
148
|
-
setLoadMessagesViaPluginError(undefined);
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
},
|
|
152
|
-
});
|
|
153
|
-
loadMessagesViaPlugin(fsWithWatcher, messageLockDirPath, messageStates, messagesQuery, _settings, resolvedPluginApi)
|
|
154
|
-
.then(() => {
|
|
155
|
-
markInitAsComplete();
|
|
156
|
-
})
|
|
157
|
-
.catch((err) => {
|
|
158
|
-
markInitAsFailed(new PluginLoadMessagesError({ cause: err }));
|
|
159
|
-
});
|
|
103
|
+
const messagesQuery = createMessagesQuery({
|
|
104
|
+
projectPath,
|
|
105
|
+
nodeishFs,
|
|
106
|
+
settings,
|
|
107
|
+
resolvedModules,
|
|
108
|
+
onInitialMessageLoadResult: (e) => {
|
|
109
|
+
if (e) {
|
|
110
|
+
markInitAsFailed(e);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
markInitAsComplete();
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
onLoadMessageResult: (e) => {
|
|
117
|
+
setLoadMessagesViaPluginError(e);
|
|
118
|
+
},
|
|
119
|
+
onSaveMessageResult: (e) => {
|
|
120
|
+
setSaveMessagesViaPluginError(e);
|
|
121
|
+
},
|
|
160
122
|
});
|
|
161
123
|
// -- installed items ----------------------------------------------------
|
|
162
124
|
const installedMessageLintRules = () => {
|
|
@@ -187,73 +149,6 @@ export async function loadProject(args) {
|
|
|
187
149
|
};
|
|
188
150
|
// -- app ---------------------------------------------------------------
|
|
189
151
|
const initializeError = await initialized.catch((error) => error);
|
|
190
|
-
const abortController = new AbortController();
|
|
191
|
-
nodeishFs.watch("/", { signal: abortController.signal }) !== undefined;
|
|
192
|
-
// map of message id => dispose function from createRoot for each message
|
|
193
|
-
const trackedMessages = new Map();
|
|
194
|
-
let initialSetup = true;
|
|
195
|
-
// -- subscribe to all messages and write to files on signal -------------
|
|
196
|
-
createEffect(() => {
|
|
197
|
-
// debug("Outer createEffect")
|
|
198
|
-
const _resolvedModules = resolvedModules();
|
|
199
|
-
if (!_resolvedModules)
|
|
200
|
-
return;
|
|
201
|
-
const resolvedPluginApi = _resolvedModules.resolvedPluginApi;
|
|
202
|
-
const currentMessageIds = new Set(messagesQuery.includedMessageIds());
|
|
203
|
-
const deletedTrackedMessages = [...trackedMessages].filter((tracked) => !currentMessageIds.has(tracked[0]));
|
|
204
|
-
for (const messageId of currentMessageIds) {
|
|
205
|
-
if (!trackedMessages.has(messageId)) {
|
|
206
|
-
// we create a new root to be able to cleanup an effect for a message that got deleted
|
|
207
|
-
createRoot((dispose) => {
|
|
208
|
-
createEffect(() => {
|
|
209
|
-
// debug("Inner createEffect", messageId)
|
|
210
|
-
const message = messagesQuery.get({ where: { id: messageId } });
|
|
211
|
-
if (!message) {
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
if (!trackedMessages?.has(messageId)) {
|
|
215
|
-
// initial effect execution - add dispose function
|
|
216
|
-
trackedMessages?.set(messageId, dispose);
|
|
217
|
-
}
|
|
218
|
-
// don't trigger saves or set dirty flags during initial setup
|
|
219
|
-
if (!initialSetup) {
|
|
220
|
-
debug("message changed", messageId);
|
|
221
|
-
messageStates.messageDirtyFlags[message.id] = true;
|
|
222
|
-
saveMessagesViaPlugin(nodeishFs, messageLockDirPath, messageStates, messagesQuery, settings(), resolvedPluginApi)
|
|
223
|
-
.catch((e) => setSaveMessagesViaPluginError(new PluginSaveMessagesError({ cause: e })))
|
|
224
|
-
.then(() => {
|
|
225
|
-
if (saveMessagesViaPluginError() !== undefined) {
|
|
226
|
-
setSaveMessagesViaPluginError(undefined);
|
|
227
|
-
}
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
for (const deletedMessage of deletedTrackedMessages) {
|
|
235
|
-
const deletedMessageId = deletedMessage[0];
|
|
236
|
-
// call dispose to cleanup the effect
|
|
237
|
-
const messageEffectDisposeFunction = trackedMessages.get(deletedMessageId);
|
|
238
|
-
if (messageEffectDisposeFunction) {
|
|
239
|
-
messageEffectDisposeFunction();
|
|
240
|
-
trackedMessages.delete(deletedMessageId);
|
|
241
|
-
}
|
|
242
|
-
// mark the deleted message as dirty to force a save
|
|
243
|
-
messageStates.messageDirtyFlags[deletedMessageId] = true;
|
|
244
|
-
}
|
|
245
|
-
if (deletedTrackedMessages.length > 0) {
|
|
246
|
-
// we keep track of the latest save within the loadProject call to await it at the end - this is not used in subsequetial upserts
|
|
247
|
-
saveMessagesViaPlugin(nodeishFs, messageLockDirPath, messageStates, messagesQuery, settings(), resolvedPluginApi)
|
|
248
|
-
.catch((e) => setSaveMessagesViaPluginError(new PluginSaveMessagesError({ cause: e })))
|
|
249
|
-
.then(() => {
|
|
250
|
-
if (saveMessagesViaPluginError() !== undefined) {
|
|
251
|
-
setSaveMessagesViaPluginError(undefined);
|
|
252
|
-
}
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
initialSetup = false;
|
|
256
|
-
});
|
|
257
152
|
const lintReportsQuery = createMessageLintReportsQuery(messagesQuery, settings, installedMessageLintRules, resolvedModules);
|
|
258
153
|
/**
|
|
259
154
|
* Utility to escape reactive tracking and avoid multiple calls to
|
|
@@ -373,7 +268,6 @@ const createAwaitable = () => {
|
|
|
373
268
|
});
|
|
374
269
|
return [promise, resolve, reject];
|
|
375
270
|
};
|
|
376
|
-
const makeTrulyAsync = (fn) => (async () => fn)();
|
|
377
271
|
// Skip initial call, eg. to skip setup of a createEffect
|
|
378
272
|
function skipFirst(func) {
|
|
379
273
|
let initial = false;
|
|
@@ -394,363 +288,3 @@ export function createSubscribable(signal) {
|
|
|
394
288
|
},
|
|
395
289
|
});
|
|
396
290
|
}
|
|
397
|
-
// --- serialization of loading / saving messages.
|
|
398
|
-
// 1. A plugin saveMessage call can not be called simultaniously to avoid side effects - its an async function not controlled by us
|
|
399
|
-
// 2. loading and saving must not run in "parallel".
|
|
400
|
-
// - json plugin exports into separate file per language.
|
|
401
|
-
// - saving a message in two different languages would lead to a write in de.json first
|
|
402
|
-
// - This will leads to a load of the messages and since en.json has not been saved yet the english variant in the message would get overritten with the old state again
|
|
403
|
-
/**
|
|
404
|
-
* Messsage that loads messages from a plugin - this method synchronizes with the saveMessage funciton.
|
|
405
|
-
* If a save is in progress loading will wait until saving is done. If another load kicks in during this load it will queue the
|
|
406
|
-
* load and execute it at the end of this load. subsequential loads will not be queued but the same promise will be reused
|
|
407
|
-
*
|
|
408
|
-
* - NOTE: this means that the parameters used to load like settingsValue and loadPlugin might not take into account. this has to be refactored
|
|
409
|
-
* with the loadProject restructuring
|
|
410
|
-
* @param fs
|
|
411
|
-
* @param messagesQuery
|
|
412
|
-
* @param settingsValue
|
|
413
|
-
* @param loadPlugin
|
|
414
|
-
* @returns void - updates the files and messages in of the project in place
|
|
415
|
-
*/
|
|
416
|
-
async function loadMessagesViaPlugin(fs, lockDirPath, messageState, messagesQuery, settingsValue, resolvedPluginApi) {
|
|
417
|
-
const experimentalAliases = !!settingsValue.experimental?.aliases;
|
|
418
|
-
// loading is an asynchronous process - check if another load is in progress - queue this call if so
|
|
419
|
-
if (messageState.isLoading) {
|
|
420
|
-
if (!messageState.sheduledLoadMessagesViaPlugin) {
|
|
421
|
-
messageState.sheduledLoadMessagesViaPlugin = createAwaitable();
|
|
422
|
-
}
|
|
423
|
-
// another load will take place right after the current one - its goingt to be idempotent form the current requested one - don't reschedule
|
|
424
|
-
return messageState.sheduledLoadMessagesViaPlugin[0];
|
|
425
|
-
}
|
|
426
|
-
// set loading flag
|
|
427
|
-
messageState.isLoading = true;
|
|
428
|
-
let lockTime = undefined;
|
|
429
|
-
try {
|
|
430
|
-
lockTime = await acquireFileLock(fs, lockDirPath, "loadMessage");
|
|
431
|
-
const loadedMessages = await makeTrulyAsync(resolvedPluginApi.loadMessages({
|
|
432
|
-
settings: settingsValue,
|
|
433
|
-
nodeishFs: fs,
|
|
434
|
-
}));
|
|
435
|
-
for (const loadedMessage of loadedMessages) {
|
|
436
|
-
const loadedMessageClone = structuredClone(loadedMessage);
|
|
437
|
-
const currentMessages = messagesQuery
|
|
438
|
-
.getAll()
|
|
439
|
-
// TODO #1585 here we match using the id to support legacy load message plugins - after we introduced import / export methods we will use importedMessage.alias
|
|
440
|
-
.filter((message) => (experimentalAliases ? message.alias["default"] : message.id) === loadedMessage.id);
|
|
441
|
-
if (currentMessages.length > 1) {
|
|
442
|
-
// NOTE: if we happen to find two messages witht the sam alias we throw for now
|
|
443
|
-
// - this could be the case if one edits the aliase manualy
|
|
444
|
-
throw new Error("more than one message with the same id or alias found ");
|
|
445
|
-
}
|
|
446
|
-
else if (currentMessages.length === 1) {
|
|
447
|
-
// update message in place - leave message id and alias untouched
|
|
448
|
-
loadedMessageClone.alias = {};
|
|
449
|
-
// TODO #1585 we have to map the id of the importedMessage to the alias and fill the id property with the id of the existing message - change when import mesage provides importedMessage.alias
|
|
450
|
-
if (experimentalAliases) {
|
|
451
|
-
loadedMessageClone.alias["default"] = loadedMessageClone.id;
|
|
452
|
-
loadedMessageClone.id = currentMessages[0].id;
|
|
453
|
-
}
|
|
454
|
-
// NOTE stringifyMessage encodes messages independent from key order!
|
|
455
|
-
const importedEnecoded = stringifyMessage(loadedMessageClone);
|
|
456
|
-
// NOTE could use hash instead of the whole object JSON to save memory...
|
|
457
|
-
if (messageState.messageLoadHash[loadedMessageClone.id] === importedEnecoded) {
|
|
458
|
-
// debug("skipping upsert!")
|
|
459
|
-
continue;
|
|
460
|
-
}
|
|
461
|
-
// This logic is preventing cycles - could also be handled if update api had a parameter for who triggered update
|
|
462
|
-
// e.g. when FS was updated, we don't need to write back to FS
|
|
463
|
-
// update is synchronous, so update effect will be triggered immediately
|
|
464
|
-
// NOTE: this might trigger a save before we have the chance to delete - but since save is async and waits for the lock acquired by this method - its save to set the flags afterwards
|
|
465
|
-
messagesQuery.update({ where: { id: loadedMessageClone.id }, data: loadedMessageClone });
|
|
466
|
-
// we load a fresh version - lets delete dirty flag that got created by the update
|
|
467
|
-
delete messageState.messageDirtyFlags[loadedMessageClone.id];
|
|
468
|
-
// NOTE could use hash instead of the whole object JSON to save memory...
|
|
469
|
-
messageState.messageLoadHash[loadedMessageClone.id] = importedEnecoded;
|
|
470
|
-
}
|
|
471
|
-
else {
|
|
472
|
-
// message with the given alias does not exist so far
|
|
473
|
-
loadedMessageClone.alias = {};
|
|
474
|
-
// TODO #1585 we have to map the id of the importedMessage to the alias - change when import mesage provides importedMessage.alias
|
|
475
|
-
if (experimentalAliases) {
|
|
476
|
-
loadedMessageClone.alias["default"] = loadedMessageClone.id;
|
|
477
|
-
let currentOffset = 0;
|
|
478
|
-
let messsageId;
|
|
479
|
-
do {
|
|
480
|
-
messsageId = humanIdHash(loadedMessageClone.id, currentOffset);
|
|
481
|
-
if (messagesQuery.get({ where: { id: messsageId } })) {
|
|
482
|
-
currentOffset += 1;
|
|
483
|
-
messsageId = undefined;
|
|
484
|
-
}
|
|
485
|
-
} while (messsageId === undefined);
|
|
486
|
-
// create a humanId based on a hash of the alias
|
|
487
|
-
loadedMessageClone.id = messsageId;
|
|
488
|
-
}
|
|
489
|
-
const importedEnecoded = stringifyMessage(loadedMessageClone);
|
|
490
|
-
// add the message - this will trigger an async file creation in the backgound!
|
|
491
|
-
messagesQuery.create({ data: loadedMessageClone });
|
|
492
|
-
// we load a fresh version - lets delete dirty flag that got created by the create method
|
|
493
|
-
delete messageState.messageDirtyFlags[loadedMessageClone.id];
|
|
494
|
-
messageState.messageLoadHash[loadedMessageClone.id] = importedEnecoded;
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
await releaseLock(fs, lockDirPath, "loadMessage", lockTime);
|
|
498
|
-
lockTime = undefined;
|
|
499
|
-
debug("loadMessagesViaPlugin: " + loadedMessages.length + " Messages processed ");
|
|
500
|
-
messageState.isLoading = false;
|
|
501
|
-
}
|
|
502
|
-
finally {
|
|
503
|
-
if (lockTime !== undefined) {
|
|
504
|
-
await releaseLock(fs, lockDirPath, "loadMessage", lockTime);
|
|
505
|
-
}
|
|
506
|
-
messageState.isLoading = false;
|
|
507
|
-
}
|
|
508
|
-
const executingScheduledMessages = messageState.sheduledLoadMessagesViaPlugin;
|
|
509
|
-
if (executingScheduledMessages) {
|
|
510
|
-
// a load has been requested during the load - executed it
|
|
511
|
-
// reset sheduling to except scheduling again
|
|
512
|
-
messageState.sheduledLoadMessagesViaPlugin = undefined;
|
|
513
|
-
// recall load unawaited to allow stack to pop
|
|
514
|
-
loadMessagesViaPlugin(fs, lockDirPath, messageState, messagesQuery, settingsValue, resolvedPluginApi)
|
|
515
|
-
.then(() => {
|
|
516
|
-
// resolve the scheduled load message promise
|
|
517
|
-
executingScheduledMessages[1]();
|
|
518
|
-
})
|
|
519
|
-
.catch((e) => {
|
|
520
|
-
// reject the scheduled load message promise
|
|
521
|
-
executingScheduledMessages[2](e);
|
|
522
|
-
});
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
async function saveMessagesViaPlugin(fs, lockDirPath, messageState, messagesQuery, settingsValue, resolvedPluginApi) {
|
|
526
|
-
// queue next save if we have a save ongoing
|
|
527
|
-
if (messageState.isSaving) {
|
|
528
|
-
if (!messageState.sheduledSaveMessages) {
|
|
529
|
-
messageState.sheduledSaveMessages = createAwaitable();
|
|
530
|
-
}
|
|
531
|
-
return messageState.sheduledSaveMessages[0];
|
|
532
|
-
}
|
|
533
|
-
// set isSavingFlag
|
|
534
|
-
messageState.isSaving = true;
|
|
535
|
-
messageState.currentSaveMessagesViaPlugin = (async function () {
|
|
536
|
-
const saveMessageHashes = {};
|
|
537
|
-
// check if we have any dirty message - witho
|
|
538
|
-
if (Object.keys(messageState.messageDirtyFlags).length == 0) {
|
|
539
|
-
// nothing to save :-)
|
|
540
|
-
debug("save was skipped - no messages marked as dirty... build!");
|
|
541
|
-
messageState.isSaving = false;
|
|
542
|
-
return;
|
|
543
|
-
}
|
|
544
|
-
let messageDirtyFlagsBeforeSave;
|
|
545
|
-
let lockTime;
|
|
546
|
-
try {
|
|
547
|
-
lockTime = await acquireFileLock(fs, lockDirPath, "saveMessage");
|
|
548
|
-
// since it may takes some time to acquire the lock we check if the save is required still (loadMessage could have happend in between)
|
|
549
|
-
if (Object.keys(messageState.messageDirtyFlags).length == 0) {
|
|
550
|
-
debug("save was skipped - no messages marked as dirty... releasing lock again");
|
|
551
|
-
messageState.isSaving = false;
|
|
552
|
-
// release lock in finally block
|
|
553
|
-
return;
|
|
554
|
-
}
|
|
555
|
-
const currentMessages = messagesQuery.getAll();
|
|
556
|
-
const messagesToExport = [];
|
|
557
|
-
for (const message of currentMessages) {
|
|
558
|
-
if (messageState.messageDirtyFlags[message.id]) {
|
|
559
|
-
const importedEnecoded = stringifyMessage(message);
|
|
560
|
-
// NOTE: could use hash instead of the whole object JSON to save memory...
|
|
561
|
-
saveMessageHashes[message.id] = importedEnecoded;
|
|
562
|
-
}
|
|
563
|
-
const fixedExportMessage = { ...message };
|
|
564
|
-
// TODO #1585 here we match using the id to support legacy load message plugins - after we introduced import / export methods we will use importedMessage.alias
|
|
565
|
-
if (settingsValue.experimental?.aliases) {
|
|
566
|
-
fixedExportMessage.id = fixedExportMessage.alias["default"] ?? fixedExportMessage.id;
|
|
567
|
-
}
|
|
568
|
-
messagesToExport.push(fixedExportMessage);
|
|
569
|
-
}
|
|
570
|
-
// wa are about to save the messages to the plugin - reset all flags now
|
|
571
|
-
messageDirtyFlagsBeforeSave = { ...messageState.messageDirtyFlags };
|
|
572
|
-
messageState.messageDirtyFlags = {};
|
|
573
|
-
// NOTE: this assumes that the plugin will handle message ordering
|
|
574
|
-
await resolvedPluginApi.saveMessages({
|
|
575
|
-
settings: settingsValue,
|
|
576
|
-
messages: messagesToExport,
|
|
577
|
-
nodeishFs: fs,
|
|
578
|
-
});
|
|
579
|
-
for (const [messageId, messageHash] of Object.entries(saveMessageHashes)) {
|
|
580
|
-
messageState.messageLoadHash[messageId] = messageHash;
|
|
581
|
-
}
|
|
582
|
-
if (lockTime !== undefined) {
|
|
583
|
-
await releaseLock(fs, lockDirPath, "saveMessage", lockTime);
|
|
584
|
-
lockTime = undefined;
|
|
585
|
-
}
|
|
586
|
-
// if there is a queued load, allow it to take the lock before we run additional saves.
|
|
587
|
-
if (messageState.sheduledLoadMessagesViaPlugin) {
|
|
588
|
-
debug("saveMessagesViaPlugin calling queued loadMessagesViaPlugin to share lock");
|
|
589
|
-
await loadMessagesViaPlugin(fs, lockDirPath, messageState, messagesQuery, settingsValue, resolvedPluginApi);
|
|
590
|
-
}
|
|
591
|
-
messageState.isSaving = false;
|
|
592
|
-
}
|
|
593
|
-
catch (err) {
|
|
594
|
-
// something went wrong - add dirty flags again
|
|
595
|
-
if (messageDirtyFlagsBeforeSave !== undefined) {
|
|
596
|
-
for (const dirtyMessageId of Object.keys(messageDirtyFlagsBeforeSave)) {
|
|
597
|
-
messageState.messageDirtyFlags[dirtyMessageId] = true;
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
if (lockTime !== undefined) {
|
|
601
|
-
await releaseLock(fs, lockDirPath, "saveMessage", lockTime);
|
|
602
|
-
lockTime = undefined;
|
|
603
|
-
}
|
|
604
|
-
messageState.isSaving = false;
|
|
605
|
-
// ok an error
|
|
606
|
-
throw new PluginSaveMessagesError({
|
|
607
|
-
cause: err,
|
|
608
|
-
});
|
|
609
|
-
}
|
|
610
|
-
finally {
|
|
611
|
-
if (lockTime !== undefined) {
|
|
612
|
-
await releaseLock(fs, lockDirPath, "saveMessage", lockTime);
|
|
613
|
-
lockTime = undefined;
|
|
614
|
-
}
|
|
615
|
-
messageState.isSaving = false;
|
|
616
|
-
}
|
|
617
|
-
})();
|
|
618
|
-
await messageState.currentSaveMessagesViaPlugin;
|
|
619
|
-
if (messageState.sheduledSaveMessages) {
|
|
620
|
-
const executingSheduledSaveMessages = messageState.sheduledSaveMessages;
|
|
621
|
-
messageState.sheduledSaveMessages = undefined;
|
|
622
|
-
saveMessagesViaPlugin(fs, lockDirPath, messageState, messagesQuery, settingsValue, resolvedPluginApi)
|
|
623
|
-
.then(() => {
|
|
624
|
-
executingSheduledSaveMessages[1]();
|
|
625
|
-
})
|
|
626
|
-
.catch((e) => {
|
|
627
|
-
executingSheduledSaveMessages[2](e);
|
|
628
|
-
});
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
const maxRetries = 10;
|
|
632
|
-
const nProbes = 50;
|
|
633
|
-
const probeInterval = 100;
|
|
634
|
-
async function acquireFileLock(fs, lockDirPath, lockOrigin, tryCount = 0) {
|
|
635
|
-
if (tryCount > maxRetries) {
|
|
636
|
-
throw new Error(lockOrigin + " exceeded maximum Retries (5) to acquire lockfile " + tryCount);
|
|
637
|
-
}
|
|
638
|
-
try {
|
|
639
|
-
debugLock(lockOrigin + " tries to acquire a lockfile Retry Nr.: " + tryCount);
|
|
640
|
-
await fs.mkdir(lockDirPath);
|
|
641
|
-
const stats = await fs.stat(lockDirPath);
|
|
642
|
-
debugLock(lockOrigin + " acquired a lockfile Retry Nr.: " + tryCount);
|
|
643
|
-
return stats.mtimeMs;
|
|
644
|
-
}
|
|
645
|
-
catch (error) {
|
|
646
|
-
if (error.code !== "EEXIST") {
|
|
647
|
-
// we only expect the error that the file exists already (locked by other process)
|
|
648
|
-
throw error;
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
let currentLockTime;
|
|
652
|
-
try {
|
|
653
|
-
const stats = await fs.stat(lockDirPath);
|
|
654
|
-
currentLockTime = stats.mtimeMs;
|
|
655
|
-
}
|
|
656
|
-
catch (fstatError) {
|
|
657
|
-
if (fstatError.code === "ENOENT") {
|
|
658
|
-
// lock file seems to be gone :) - lets try again
|
|
659
|
-
debugLock(lockOrigin + " tryCount++ lock file seems to be gone :) - lets try again " + tryCount);
|
|
660
|
-
return acquireFileLock(fs, lockDirPath, lockOrigin, tryCount + 1);
|
|
661
|
-
}
|
|
662
|
-
throw fstatError;
|
|
663
|
-
}
|
|
664
|
-
debugLock(lockOrigin +
|
|
665
|
-
" tries to acquire a lockfile - lock currently in use... starting probe phase " +
|
|
666
|
-
tryCount);
|
|
667
|
-
return new Promise((resolve, reject) => {
|
|
668
|
-
let probeCounts = 0;
|
|
669
|
-
const scheduleProbationTimeout = () => {
|
|
670
|
-
setTimeout(async () => {
|
|
671
|
-
probeCounts += 1;
|
|
672
|
-
let lockFileStats = undefined;
|
|
673
|
-
try {
|
|
674
|
-
debugLock(lockOrigin + " tries to acquire a lockfile - check if the lock is free now " + tryCount);
|
|
675
|
-
// alright lets give it another try
|
|
676
|
-
lockFileStats = await fs.stat(lockDirPath);
|
|
677
|
-
}
|
|
678
|
-
catch (fstatError) {
|
|
679
|
-
if (fstatError.code === "ENOENT") {
|
|
680
|
-
debugLock(lockOrigin +
|
|
681
|
-
" tryCount++ in Promise - tries to acquire a lockfile - lock file seems to be free now - try to acquire " +
|
|
682
|
-
tryCount);
|
|
683
|
-
const lock = acquireFileLock(fs, lockDirPath, lockOrigin, tryCount + 1);
|
|
684
|
-
return resolve(lock);
|
|
685
|
-
}
|
|
686
|
-
return reject(fstatError);
|
|
687
|
-
}
|
|
688
|
-
// still the same locker! -
|
|
689
|
-
if (lockFileStats.mtimeMs === currentLockTime) {
|
|
690
|
-
if (probeCounts >= nProbes) {
|
|
691
|
-
// ok maximum lock time ran up (we waitetd nProbes * probeInterval) - we consider the lock to be stale
|
|
692
|
-
debugLock(lockOrigin +
|
|
693
|
-
" tries to acquire a lockfile - lock not free - but stale lets drop it" +
|
|
694
|
-
tryCount);
|
|
695
|
-
try {
|
|
696
|
-
await fs.rmdir(lockDirPath);
|
|
697
|
-
}
|
|
698
|
-
catch (rmLockError) {
|
|
699
|
-
if (rmLockError.code === "ENOENT") {
|
|
700
|
-
// lock already gone?
|
|
701
|
-
// Option 1: The "stale process" decided to get rid of it
|
|
702
|
-
// Option 2: Another process acquiring the lock and detected a stale one as well
|
|
703
|
-
}
|
|
704
|
-
return reject(rmLockError);
|
|
705
|
-
}
|
|
706
|
-
try {
|
|
707
|
-
debugLock(lockOrigin +
|
|
708
|
-
" tryCount++ same locker - try to acquire again after removing stale lock " +
|
|
709
|
-
tryCount);
|
|
710
|
-
const lock = await acquireFileLock(fs, lockDirPath, lockOrigin, tryCount + 1);
|
|
711
|
-
return resolve(lock);
|
|
712
|
-
}
|
|
713
|
-
catch (lockAquireException) {
|
|
714
|
-
return reject(lockAquireException);
|
|
715
|
-
}
|
|
716
|
-
}
|
|
717
|
-
else {
|
|
718
|
-
// lets schedule a new probation
|
|
719
|
-
return scheduleProbationTimeout();
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
else {
|
|
723
|
-
try {
|
|
724
|
-
debugLock(lockOrigin + " tryCount++ different locker - try to acquire again " + tryCount);
|
|
725
|
-
const lock = await acquireFileLock(fs, lockDirPath, lockOrigin, tryCount + 1);
|
|
726
|
-
return resolve(lock);
|
|
727
|
-
}
|
|
728
|
-
catch (error) {
|
|
729
|
-
return reject(error);
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
|
-
}, probeInterval);
|
|
733
|
-
};
|
|
734
|
-
scheduleProbationTimeout();
|
|
735
|
-
});
|
|
736
|
-
}
|
|
737
|
-
async function releaseLock(fs, lockDirPath, lockOrigin, lockTime) {
|
|
738
|
-
debugLock(lockOrigin + " releasing the lock ");
|
|
739
|
-
try {
|
|
740
|
-
const stats = await fs.stat(lockDirPath);
|
|
741
|
-
if (stats.mtimeMs === lockTime) {
|
|
742
|
-
// this can be corrupt as welll since the last getStat and the current a modification could have occured :-/
|
|
743
|
-
await fs.rmdir(lockDirPath);
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
catch (statError) {
|
|
747
|
-
debugLock(lockOrigin + " couldn't release the lock");
|
|
748
|
-
if (statError.code === "ENOENT") {
|
|
749
|
-
// ok seeks like the log was released by someone else
|
|
750
|
-
debugLock(lockOrigin + " WARNING - the lock was released by a different process");
|
|
751
|
-
return;
|
|
752
|
-
}
|
|
753
|
-
debugLock(statError);
|
|
754
|
-
throw statError;
|
|
755
|
-
}
|
|
756
|
-
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"acquireFileLock.d.ts","sourceRoot":"","sources":["../../../src/persistence/filelock/acquireFileLock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAQnD,wBAAsB,eAAe,CACpC,EAAE,EAAE,iBAAiB,EACrB,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE,MAAM,EAClB,QAAQ,GAAE,MAAU,GAClB,OAAO,CAAC,MAAM,CAAC,CA8GjB"}
|