@inlang/sdk 0.34.3 → 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.
@@ -11,58 +11,30 @@ import {
11
11
  ProjectSettingsFileJSONSyntaxError,
12
12
  ProjectSettingsFileNotFoundError,
13
13
  ProjectSettingsInvalidError,
14
- PluginSaveMessagesError,
15
- PluginLoadMessagesError,
16
14
  } from "./errors.js"
17
15
  import { createRoot, createSignal, createEffect } from "./reactivity/solid.js"
18
16
  import { createMessagesQuery } from "./createMessagesQuery.js"
19
17
  import { createMessageLintReportsQuery } from "./createMessageLintReportsQuery.js"
20
- import { ProjectSettings, Message, type NodeishFilesystemSubset } from "./versionedInterfaces.js"
18
+ import { ProjectSettings, type NodeishFilesystemSubset } from "./versionedInterfaces.js"
21
19
  import { tryCatch, type Result } from "@inlang/result"
22
20
  import { migrateIfOutdated } from "@inlang/project-settings/migration"
23
21
  import { createNodeishFsWithAbsolutePaths } from "./createNodeishFsWithAbsolutePaths.js"
24
- import { normalizePath, type NodeishFilesystem } from "@lix-js/fs"
22
+ import { normalizePath } from "@lix-js/fs"
25
23
  import { assertValidProjectPath } from "./validateProjectPath.js"
26
24
  import { maybeMigrateToDirectory } from "./migrations/migrateToDirectory.js"
27
25
 
28
- import { stringifyMessage as stringifyMessage } from "./storage/helper.js"
29
-
30
- import { humanIdHash } from "./storage/human-id/human-readable-id.js"
31
-
32
26
  import type { Repository } from "@lix-js/client"
33
- import { createNodeishFsWithWatcher } from "./createNodeishFsWithWatcher.js"
34
27
 
35
28
  import { maybeCreateFirstProjectId } from "./migrations/maybeCreateFirstProjectId.js"
36
29
 
37
30
  import { capture } from "./telemetry/capture.js"
38
31
  import { identifyProject } from "./telemetry/groupIdentify.js"
39
- import type { NodeishStats } from "@lix-js/fs"
40
-
41
- import type { ResolvedPluginApi } from "./resolve-modules/plugins/types.js"
42
32
 
43
33
  import _debug from "debug"
44
34
  const debug = _debug("sdk:loadProject")
45
- const debugLock = _debug("sdk:lockfile")
46
35
 
47
- const settingsCompiler = TypeCompiler.Compile(ProjectSettings)
48
36
 
49
- type MessageState = {
50
- messageDirtyFlags: {
51
- [messageId: string]: boolean
52
- }
53
- messageLoadHash: {
54
- [messageId: string]: string
55
- }
56
- isSaving: boolean
57
- currentSaveMessagesViaPlugin: Promise<void> | undefined
58
- sheduledSaveMessages:
59
- | [awaitable: Promise<void>, resolve: () => void, reject: (e: unknown) => void]
60
- | undefined
61
- isLoading: boolean
62
- sheduledLoadMessagesViaPlugin:
63
- | [awaitable: Promise<void>, resolve: () => void, reject: (e: unknown) => void]
64
- | undefined
65
- }
37
+ const settingsCompiler = TypeCompiler.Compile(ProjectSettings)
66
38
 
67
39
  /**
68
40
  * @param projectPath - Absolute path to the inlang settings file.
@@ -80,16 +52,6 @@ export async function loadProject(args: {
80
52
  }): Promise<InlangProject> {
81
53
  const projectPath = normalizePath(args.projectPath)
82
54
 
83
- const messageStates = {
84
- messageDirtyFlags: {},
85
- messageLoadHash: {},
86
- isSaving: false,
87
- currentSaveMessagesViaPlugin: undefined,
88
- sheduledSaveMessages: undefined,
89
- isLoading: false,
90
- sheduledLoadMessagesViaPlugin: undefined,
91
- } as MessageState
92
-
93
55
  // -- validation --------------------------------------------------------
94
56
  // the only place where throwing is acceptable because the project
95
57
  // won't even be loaded. do not throw anywhere else. otherwise, apps
@@ -186,11 +148,6 @@ export async function loadProject(args: {
186
148
  let settingsValue: ProjectSettings
187
149
  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)
188
150
 
189
- // please don't use this as source of truth, use the query instead
190
- // needed for granular linting
191
- // 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
192
- const [messages, setMessages] = createSignal<Message[]>([])
193
-
194
151
  const [loadMessagesViaPluginError, setLoadMessagesViaPluginError] = createSignal<
195
152
  Error | undefined
196
153
  >()
@@ -199,64 +156,24 @@ export async function loadProject(args: {
199
156
  Error | undefined
200
157
  >()
201
158
 
202
- const messagesQuery = createMessagesQuery(() => messages())
203
-
204
- const messageLockDirPath = projectPath + "/messagelock"
205
-
206
- createEffect(() => {
207
- // wait for first effect excution until modules are resolved
208
- const _resolvedModules = resolvedModules()
209
- if (!_resolvedModules) return
210
-
211
- const resolvedPluginApi = _resolvedModules.resolvedPluginApi
212
-
213
- if (!resolvedPluginApi.loadMessages) {
214
- markInitAsFailed(undefined)
215
- return
216
- }
217
-
218
- const _settings = settings()
219
- if (!_settings) return
220
-
221
- // TODO #1844 this watcher needs to get pruned when we have a change in the configs which will trigger this again
222
- const fsWithWatcher = createNodeishFsWithWatcher({
223
- nodeishFs: nodeishFs,
224
- // this message is called whenever a file changes that was read earlier by this filesystem
225
- // - the plugin loads messages -> reads the file messages.json -> start watching on messages.json -> updateMessages
226
- updateMessages: () => {
227
- // preserving console.logs as comments pending #
228
- debug("load messages because of a change in the message.json files")
229
- loadMessagesViaPlugin(
230
- fsWithWatcher,
231
- messageLockDirPath,
232
- messageStates,
233
- messagesQuery,
234
- settings()!, // NOTE we bang here - we don't expect the settings to become null during the livetime of a project
235
- resolvedPluginApi
236
- )
237
- .catch((e) => setLoadMessagesViaPluginError(new PluginLoadMessagesError({ cause: e })))
238
- .then(() => {
239
- if (loadMessagesViaPluginError() !== undefined) {
240
- setLoadMessagesViaPluginError(undefined)
241
- }
242
- })
243
- },
244
- })
245
-
246
- loadMessagesViaPlugin(
247
- fsWithWatcher,
248
- messageLockDirPath,
249
- messageStates,
250
- messagesQuery,
251
- _settings,
252
- resolvedPluginApi
253
- )
254
- .then(() => {
159
+ const messagesQuery = createMessagesQuery({
160
+ projectPath,
161
+ nodeishFs,
162
+ settings,
163
+ resolvedModules,
164
+ onInitialMessageLoadResult: (e) => {
165
+ if (e) {
166
+ markInitAsFailed(e)
167
+ } else {
255
168
  markInitAsComplete()
256
- })
257
- .catch((err) => {
258
- markInitAsFailed(new PluginLoadMessagesError({ cause: err }))
259
- })
169
+ }
170
+ },
171
+ onLoadMessageResult: (e) => {
172
+ setLoadMessagesViaPluginError(e)
173
+ },
174
+ onSaveMessageResult: (e) => {
175
+ setSaveMessagesViaPluginError(e)
176
+ },
260
177
  })
261
178
 
262
179
  // -- installed items ----------------------------------------------------
@@ -296,101 +213,6 @@ export async function loadProject(args: {
296
213
 
297
214
  const initializeError: Error | undefined = await initialized.catch((error) => error)
298
215
 
299
- const abortController = new AbortController()
300
- nodeishFs.watch("/", { signal: abortController.signal }) !== undefined
301
-
302
- // map of message id => dispose function from createRoot for each message
303
- const trackedMessages: Map<string, () => void> = new Map()
304
- let initialSetup = true
305
- // -- subscribe to all messages and write to files on signal -------------
306
- createEffect(() => {
307
- // debug("Outer createEffect")
308
-
309
- const _resolvedModules = resolvedModules()
310
- if (!_resolvedModules) return
311
- const resolvedPluginApi = _resolvedModules.resolvedPluginApi
312
-
313
- const currentMessageIds = new Set(messagesQuery.includedMessageIds())
314
- const deletedTrackedMessages = [...trackedMessages].filter(
315
- (tracked) => !currentMessageIds.has(tracked[0])
316
- )
317
-
318
- for (const messageId of currentMessageIds) {
319
- if (!trackedMessages!.has(messageId!)) {
320
- // we create a new root to be able to cleanup an effect for a message that got deleted
321
- createRoot((dispose) => {
322
- createEffect(() => {
323
- // debug("Inner createEffect", messageId)
324
-
325
- const message = messagesQuery.get({ where: { id: messageId } })!
326
- if (!message) {
327
- return
328
- }
329
- if (!trackedMessages?.has(messageId)) {
330
- // initial effect execution - add dispose function
331
- trackedMessages?.set(messageId, dispose)
332
- }
333
-
334
- // don't trigger saves or set dirty flags during initial setup
335
- if (!initialSetup) {
336
- debug("message changed", messageId)
337
- messageStates.messageDirtyFlags[message.id] = true
338
- saveMessagesViaPlugin(
339
- nodeishFs,
340
- messageLockDirPath,
341
- messageStates,
342
- messagesQuery,
343
- settings()!,
344
- resolvedPluginApi
345
- )
346
- .catch((e) =>
347
- setSaveMessagesViaPluginError(new PluginSaveMessagesError({ cause: e }))
348
- )
349
- .then(() => {
350
- if (saveMessagesViaPluginError() !== undefined) {
351
- setSaveMessagesViaPluginError(undefined)
352
- }
353
- })
354
- }
355
- })
356
- })
357
- }
358
- }
359
-
360
- for (const deletedMessage of deletedTrackedMessages) {
361
- const deletedMessageId = deletedMessage[0]
362
-
363
- // call dispose to cleanup the effect
364
- const messageEffectDisposeFunction = trackedMessages.get(deletedMessageId)
365
- if (messageEffectDisposeFunction) {
366
- messageEffectDisposeFunction()
367
- trackedMessages.delete(deletedMessageId)
368
- }
369
- // mark the deleted message as dirty to force a save
370
- messageStates.messageDirtyFlags[deletedMessageId] = true
371
- }
372
-
373
- if (deletedTrackedMessages.length > 0) {
374
- // we keep track of the latest save within the loadProject call to await it at the end - this is not used in subsequetial upserts
375
- saveMessagesViaPlugin(
376
- nodeishFs,
377
- messageLockDirPath,
378
- messageStates,
379
- messagesQuery,
380
- settings()!,
381
- resolvedPluginApi
382
- )
383
- .catch((e) => setSaveMessagesViaPluginError(new PluginSaveMessagesError({ cause: e })))
384
- .then(() => {
385
- if (saveMessagesViaPluginError() !== undefined) {
386
- setSaveMessagesViaPluginError(undefined)
387
- }
388
- })
389
- }
390
-
391
- initialSetup = false
392
- })
393
-
394
216
  const lintReportsQuery = createMessageLintReportsQuery(
395
217
  messagesQuery,
396
218
  settings as () => ProjectSettings,
@@ -551,13 +373,6 @@ const createAwaitable = () => {
551
373
  ]
552
374
  }
553
375
 
554
- // ------------------------------------------------------------------------------------------------
555
-
556
- // TODO: create global util type
557
- type MaybePromise<T> = T | Promise<T>
558
-
559
- const makeTrulyAsync = <T>(fn: MaybePromise<T>): Promise<T> => (async () => fn)()
560
-
561
376
  // Skip initial call, eg. to skip setup of a createEffect
562
377
  function skipFirst(func: (args: any) => any) {
563
378
  let initial = false
@@ -579,464 +394,3 @@ export function createSubscribable<T>(signal: () => T): Subscribable<T> {
579
394
  },
580
395
  })
581
396
  }
582
-
583
- // --- serialization of loading / saving messages.
584
- // 1. A plugin saveMessage call can not be called simultaniously to avoid side effects - its an async function not controlled by us
585
- // 2. loading and saving must not run in "parallel".
586
- // - json plugin exports into separate file per language.
587
- // - saving a message in two different languages would lead to a write in de.json first
588
- // - 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
589
-
590
- /**
591
- * Messsage that loads messages from a plugin - this method synchronizes with the saveMessage funciton.
592
- * 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
593
- * load and execute it at the end of this load. subsequential loads will not be queued but the same promise will be reused
594
- *
595
- * - NOTE: this means that the parameters used to load like settingsValue and loadPlugin might not take into account. this has to be refactored
596
- * with the loadProject restructuring
597
- * @param fs
598
- * @param messagesQuery
599
- * @param settingsValue
600
- * @param loadPlugin
601
- * @returns void - updates the files and messages in of the project in place
602
- */
603
- async function loadMessagesViaPlugin(
604
- fs: NodeishFilesystem,
605
- lockDirPath: string,
606
- messageState: MessageState,
607
- messagesQuery: InlangProject["query"]["messages"],
608
- settingsValue: ProjectSettings,
609
- resolvedPluginApi: ResolvedPluginApi
610
- ) {
611
- const experimentalAliases = !!settingsValue.experimental?.aliases
612
-
613
- // loading is an asynchronous process - check if another load is in progress - queue this call if so
614
- if (messageState.isLoading) {
615
- if (!messageState.sheduledLoadMessagesViaPlugin) {
616
- messageState.sheduledLoadMessagesViaPlugin = createAwaitable()
617
- }
618
- // another load will take place right after the current one - its goingt to be idempotent form the current requested one - don't reschedule
619
- return messageState.sheduledLoadMessagesViaPlugin[0]
620
- }
621
-
622
- // set loading flag
623
- messageState.isLoading = true
624
- let lockTime: number | undefined = undefined
625
-
626
- try {
627
- lockTime = await acquireFileLock(fs as NodeishFilesystem, lockDirPath, "loadMessage")
628
- const loadedMessages = await makeTrulyAsync(
629
- resolvedPluginApi.loadMessages({
630
- settings: settingsValue,
631
- nodeishFs: fs,
632
- })
633
- )
634
-
635
- for (const loadedMessage of loadedMessages) {
636
- const loadedMessageClone = structuredClone(loadedMessage)
637
-
638
- const currentMessages = messagesQuery
639
- .getAll()
640
- // 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
641
- .filter(
642
- (message: any) =>
643
- (experimentalAliases ? message.alias["default"] : message.id) === loadedMessage.id
644
- )
645
-
646
- if (currentMessages.length > 1) {
647
- // NOTE: if we happen to find two messages witht the sam alias we throw for now
648
- // - this could be the case if one edits the aliase manualy
649
- throw new Error("more than one message with the same id or alias found ")
650
- } else if (currentMessages.length === 1) {
651
- // update message in place - leave message id and alias untouched
652
- loadedMessageClone.alias = {} as any
653
-
654
- // 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
655
- if (experimentalAliases) {
656
- loadedMessageClone.alias["default"] = loadedMessageClone.id
657
- loadedMessageClone.id = currentMessages[0]!.id
658
- }
659
-
660
- // NOTE stringifyMessage encodes messages independent from key order!
661
- const importedEnecoded = stringifyMessage(loadedMessageClone)
662
-
663
- // NOTE could use hash instead of the whole object JSON to save memory...
664
- if (messageState.messageLoadHash[loadedMessageClone.id] === importedEnecoded) {
665
- // debug("skipping upsert!")
666
- continue
667
- }
668
-
669
- // This logic is preventing cycles - could also be handled if update api had a parameter for who triggered update
670
- // e.g. when FS was updated, we don't need to write back to FS
671
- // update is synchronous, so update effect will be triggered immediately
672
- // 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
673
- messagesQuery.update({ where: { id: loadedMessageClone.id }, data: loadedMessageClone })
674
- // we load a fresh version - lets delete dirty flag that got created by the update
675
- delete messageState.messageDirtyFlags[loadedMessageClone.id]
676
- // NOTE could use hash instead of the whole object JSON to save memory...
677
- messageState.messageLoadHash[loadedMessageClone.id] = importedEnecoded
678
- } else {
679
- // message with the given alias does not exist so far
680
- loadedMessageClone.alias = {} as any
681
- // TODO #1585 we have to map the id of the importedMessage to the alias - change when import mesage provides importedMessage.alias
682
- if (experimentalAliases) {
683
- loadedMessageClone.alias["default"] = loadedMessageClone.id
684
-
685
- let currentOffset = 0
686
- let messsageId: string | undefined
687
- do {
688
- messsageId = humanIdHash(loadedMessageClone.id, currentOffset)
689
- if (messagesQuery.get({ where: { id: messsageId } })) {
690
- currentOffset += 1
691
- messsageId = undefined
692
- }
693
- } while (messsageId === undefined)
694
-
695
- // create a humanId based on a hash of the alias
696
- loadedMessageClone.id = messsageId
697
- }
698
-
699
- const importedEnecoded = stringifyMessage(loadedMessageClone)
700
-
701
- // add the message - this will trigger an async file creation in the backgound!
702
- messagesQuery.create({ data: loadedMessageClone })
703
- // we load a fresh version - lets delete dirty flag that got created by the create method
704
- delete messageState.messageDirtyFlags[loadedMessageClone.id]
705
- messageState.messageLoadHash[loadedMessageClone.id] = importedEnecoded
706
- }
707
- }
708
- await releaseLock(fs as NodeishFilesystem, lockDirPath, "loadMessage", lockTime)
709
- lockTime = undefined
710
-
711
- debug("loadMessagesViaPlugin: " + loadedMessages.length + " Messages processed ")
712
-
713
- messageState.isLoading = false
714
- } finally {
715
- if (lockTime !== undefined) {
716
- await releaseLock(fs as NodeishFilesystem, lockDirPath, "loadMessage", lockTime)
717
- }
718
- messageState.isLoading = false
719
- }
720
-
721
- const executingScheduledMessages = messageState.sheduledLoadMessagesViaPlugin
722
- if (executingScheduledMessages) {
723
- // a load has been requested during the load - executed it
724
-
725
- // reset sheduling to except scheduling again
726
- messageState.sheduledLoadMessagesViaPlugin = undefined
727
-
728
- // recall load unawaited to allow stack to pop
729
- loadMessagesViaPlugin(
730
- fs,
731
- lockDirPath,
732
- messageState,
733
- messagesQuery,
734
- settingsValue,
735
- resolvedPluginApi
736
- )
737
- .then(() => {
738
- // resolve the scheduled load message promise
739
- executingScheduledMessages[1]()
740
- })
741
- .catch((e: Error) => {
742
- // reject the scheduled load message promise
743
- executingScheduledMessages[2](e)
744
- })
745
- }
746
- }
747
-
748
- async function saveMessagesViaPlugin(
749
- fs: NodeishFilesystem,
750
- lockDirPath: string,
751
- messageState: MessageState,
752
- messagesQuery: InlangProject["query"]["messages"],
753
- settingsValue: ProjectSettings,
754
- resolvedPluginApi: ResolvedPluginApi
755
- ): Promise<void> {
756
- // queue next save if we have a save ongoing
757
- if (messageState.isSaving) {
758
- if (!messageState.sheduledSaveMessages) {
759
- messageState.sheduledSaveMessages = createAwaitable()
760
- }
761
-
762
- return messageState.sheduledSaveMessages[0]
763
- }
764
-
765
- // set isSavingFlag
766
- messageState.isSaving = true
767
-
768
- messageState.currentSaveMessagesViaPlugin = (async function () {
769
- const saveMessageHashes = {} as { [messageId: string]: string }
770
-
771
- // check if we have any dirty message - witho
772
- if (Object.keys(messageState.messageDirtyFlags).length == 0) {
773
- // nothing to save :-)
774
- debug("save was skipped - no messages marked as dirty... build!")
775
- messageState.isSaving = false
776
- return
777
- }
778
-
779
- let messageDirtyFlagsBeforeSave: typeof messageState.messageDirtyFlags | undefined
780
- let lockTime: number | undefined
781
- try {
782
- lockTime = await acquireFileLock(fs as NodeishFilesystem, lockDirPath, "saveMessage")
783
-
784
- // since it may takes some time to acquire the lock we check if the save is required still (loadMessage could have happend in between)
785
- if (Object.keys(messageState.messageDirtyFlags).length == 0) {
786
- debug("save was skipped - no messages marked as dirty... releasing lock again")
787
- messageState.isSaving = false
788
- // release lock in finally block
789
- return
790
- }
791
-
792
- const currentMessages = messagesQuery.getAll()
793
-
794
- const messagesToExport: Message[] = []
795
- for (const message of currentMessages) {
796
- if (messageState.messageDirtyFlags[message.id]) {
797
- const importedEnecoded = stringifyMessage(message)
798
- // NOTE: could use hash instead of the whole object JSON to save memory...
799
- saveMessageHashes[message.id] = importedEnecoded
800
- }
801
-
802
- const fixedExportMessage = { ...message }
803
- // 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
804
- if (settingsValue.experimental?.aliases) {
805
- fixedExportMessage.id = fixedExportMessage.alias["default"] ?? fixedExportMessage.id
806
- }
807
-
808
- messagesToExport.push(fixedExportMessage)
809
- }
810
-
811
- // wa are about to save the messages to the plugin - reset all flags now
812
- messageDirtyFlagsBeforeSave = { ...messageState.messageDirtyFlags }
813
- messageState.messageDirtyFlags = {}
814
-
815
- // NOTE: this assumes that the plugin will handle message ordering
816
- await resolvedPluginApi.saveMessages({
817
- settings: settingsValue,
818
- messages: messagesToExport,
819
- nodeishFs: fs,
820
- })
821
-
822
- for (const [messageId, messageHash] of Object.entries(saveMessageHashes)) {
823
- messageState.messageLoadHash[messageId] = messageHash
824
- }
825
-
826
- if (lockTime !== undefined) {
827
- await releaseLock(fs as NodeishFilesystem, lockDirPath, "saveMessage", lockTime)
828
- lockTime = undefined
829
- }
830
-
831
- // if there is a queued load, allow it to take the lock before we run additional saves.
832
- if (messageState.sheduledLoadMessagesViaPlugin) {
833
- debug("saveMessagesViaPlugin calling queued loadMessagesViaPlugin to share lock")
834
- await loadMessagesViaPlugin(
835
- fs,
836
- lockDirPath,
837
- messageState,
838
- messagesQuery,
839
- settingsValue,
840
- resolvedPluginApi
841
- )
842
- }
843
-
844
- messageState.isSaving = false
845
- } catch (err) {
846
- // something went wrong - add dirty flags again
847
- if (messageDirtyFlagsBeforeSave !== undefined) {
848
- for (const dirtyMessageId of Object.keys(messageDirtyFlagsBeforeSave)) {
849
- messageState.messageDirtyFlags[dirtyMessageId] = true
850
- }
851
- }
852
-
853
- if (lockTime !== undefined) {
854
- await releaseLock(fs as NodeishFilesystem, lockDirPath, "saveMessage", lockTime)
855
- lockTime = undefined
856
- }
857
- messageState.isSaving = false
858
-
859
- // ok an error
860
- throw new PluginSaveMessagesError({
861
- cause: err,
862
- })
863
- } finally {
864
- if (lockTime !== undefined) {
865
- await releaseLock(fs as NodeishFilesystem, lockDirPath, "saveMessage", lockTime)
866
- lockTime = undefined
867
- }
868
- messageState.isSaving = false
869
- }
870
- })()
871
-
872
- await messageState.currentSaveMessagesViaPlugin
873
-
874
- if (messageState.sheduledSaveMessages) {
875
- const executingSheduledSaveMessages = messageState.sheduledSaveMessages
876
- messageState.sheduledSaveMessages = undefined
877
-
878
- saveMessagesViaPlugin(
879
- fs,
880
- lockDirPath,
881
- messageState,
882
- messagesQuery,
883
- settingsValue,
884
- resolvedPluginApi
885
- )
886
- .then(() => {
887
- executingSheduledSaveMessages[1]()
888
- })
889
- .catch((e: Error) => {
890
- executingSheduledSaveMessages[2](e)
891
- })
892
- }
893
- }
894
-
895
- const maxRetries = 10
896
- const nProbes = 50
897
- const probeInterval = 100
898
- async function acquireFileLock(
899
- fs: NodeishFilesystem,
900
- lockDirPath: string,
901
- lockOrigin: string,
902
- tryCount: number = 0
903
- ): Promise<number> {
904
- if (tryCount > maxRetries) {
905
- throw new Error(lockOrigin + " exceeded maximum Retries (5) to acquire lockfile " + tryCount)
906
- }
907
-
908
- try {
909
- debugLock(lockOrigin + " tries to acquire a lockfile Retry Nr.: " + tryCount)
910
- await fs.mkdir(lockDirPath)
911
- const stats = await fs.stat(lockDirPath)
912
- debugLock(lockOrigin + " acquired a lockfile Retry Nr.: " + tryCount)
913
- return stats.mtimeMs
914
- } catch (error: any) {
915
- if (error.code !== "EEXIST") {
916
- // we only expect the error that the file exists already (locked by other process)
917
- throw error
918
- }
919
- }
920
-
921
- let currentLockTime: number
922
-
923
- try {
924
- const stats = await fs.stat(lockDirPath)
925
- currentLockTime = stats.mtimeMs
926
- } catch (fstatError: any) {
927
- if (fstatError.code === "ENOENT") {
928
- // lock file seems to be gone :) - lets try again
929
- debugLock(
930
- lockOrigin + " tryCount++ lock file seems to be gone :) - lets try again " + tryCount
931
- )
932
- return acquireFileLock(fs, lockDirPath, lockOrigin, tryCount + 1)
933
- }
934
- throw fstatError
935
- }
936
- debugLock(
937
- lockOrigin +
938
- " tries to acquire a lockfile - lock currently in use... starting probe phase " +
939
- tryCount
940
- )
941
-
942
- return new Promise((resolve, reject) => {
943
- let probeCounts = 0
944
- const scheduleProbationTimeout = () => {
945
- setTimeout(async () => {
946
- probeCounts += 1
947
- let lockFileStats: undefined | NodeishStats = undefined
948
- try {
949
- debugLock(
950
- lockOrigin + " tries to acquire a lockfile - check if the lock is free now " + tryCount
951
- )
952
-
953
- // alright lets give it another try
954
- lockFileStats = await fs.stat(lockDirPath)
955
- } catch (fstatError: any) {
956
- if (fstatError.code === "ENOENT") {
957
- debugLock(
958
- lockOrigin +
959
- " tryCount++ in Promise - tries to acquire a lockfile - lock file seems to be free now - try to acquire " +
960
- tryCount
961
- )
962
- const lock = acquireFileLock(fs, lockDirPath, lockOrigin, tryCount + 1)
963
- return resolve(lock)
964
- }
965
- return reject(fstatError)
966
- }
967
-
968
- // still the same locker! -
969
- if (lockFileStats.mtimeMs === currentLockTime) {
970
- if (probeCounts >= nProbes) {
971
- // ok maximum lock time ran up (we waitetd nProbes * probeInterval) - we consider the lock to be stale
972
- debugLock(
973
- lockOrigin +
974
- " tries to acquire a lockfile - lock not free - but stale lets drop it" +
975
- tryCount
976
- )
977
- try {
978
- await fs.rmdir(lockDirPath)
979
- } catch (rmLockError: any) {
980
- if (rmLockError.code === "ENOENT") {
981
- // lock already gone?
982
- // Option 1: The "stale process" decided to get rid of it
983
- // Option 2: Another process acquiring the lock and detected a stale one as well
984
- }
985
- return reject(rmLockError)
986
- }
987
- try {
988
- debugLock(
989
- lockOrigin +
990
- " tryCount++ same locker - try to acquire again after removing stale lock " +
991
- tryCount
992
- )
993
- const lock = await acquireFileLock(fs, lockDirPath, lockOrigin, tryCount + 1)
994
- return resolve(lock)
995
- } catch (lockAquireException) {
996
- return reject(lockAquireException)
997
- }
998
- } else {
999
- // lets schedule a new probation
1000
- return scheduleProbationTimeout()
1001
- }
1002
- } else {
1003
- try {
1004
- debugLock(
1005
- lockOrigin + " tryCount++ different locker - try to acquire again " + tryCount
1006
- )
1007
- const lock = await acquireFileLock(fs, lockDirPath, lockOrigin, tryCount + 1)
1008
- return resolve(lock)
1009
- } catch (error) {
1010
- return reject(error)
1011
- }
1012
- }
1013
- }, probeInterval)
1014
- }
1015
- scheduleProbationTimeout()
1016
- })
1017
- }
1018
-
1019
- async function releaseLock(
1020
- fs: NodeishFilesystem,
1021
- lockDirPath: string,
1022
- lockOrigin: string,
1023
- lockTime: number
1024
- ) {
1025
- debugLock(lockOrigin + " releasing the lock ")
1026
- try {
1027
- const stats = await fs.stat(lockDirPath)
1028
- if (stats.mtimeMs === lockTime) {
1029
- // this can be corrupt as welll since the last getStat and the current a modification could have occured :-/
1030
- await fs.rmdir(lockDirPath)
1031
- }
1032
- } catch (statError: any) {
1033
- debugLock(lockOrigin + " couldn't release the lock")
1034
- if (statError.code === "ENOENT") {
1035
- // ok seeks like the log was released by someone else
1036
- debugLock(lockOrigin + " WARNING - the lock was released by a different process")
1037
- return
1038
- }
1039
- debugLock(statError)
1040
- throw statError
1041
- }
1042
- }