-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*add retry mechanism to try deleting folder *add logic to delete queu…
…e events
- Loading branch information
1 parent
a707cbc
commit 6d1cb35
Showing
15 changed files
with
207 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/context/virtual-drive/files/application/FileFolderContainerDetector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { FolderFinder } from '../../folders/application/FolderFinder'; | ||
import { File } from '../../files/domain/File'; | ||
import { Folder } from '../../folders/domain/Folder'; | ||
import { FileRepository } from '../domain/FileRepository'; | ||
|
||
export class FileFolderContainerDetector { | ||
constructor( | ||
private readonly repository: FileRepository, | ||
private readonly folderFinder: FolderFinder | ||
) {} | ||
|
||
run(contentId: File['contentsId'], folderContentId: Folder['uuid']): boolean { | ||
const file = this.repository.searchByPartial({ contentsId: contentId }); | ||
const folder = this.folderFinder.findFromId(file?.folderId); | ||
const [_, folderUuid] = folder.placeholderId.split(':'); | ||
return folderUuid === folderContentId; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/context/virtual-drive/folders/application/FolderContainerDetector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Folder } from '../domain/Folder'; | ||
import { FolderRepository } from '../domain/FolderRepository'; | ||
|
||
export class FolderContainerDetector { | ||
constructor(private readonly repository: FolderRepository) {} | ||
|
||
run( | ||
fodlerContentId: Folder['uuid'], | ||
parentFolderContentId: Folder['uuid'] | ||
): boolean { | ||
const folder = this.repository.searchByPartial({ uuid: fodlerContentId }); | ||
|
||
if (!folder) { | ||
throw new Error('Folder not found'); | ||
} | ||
|
||
const parent = this.repository.searchByPartial({ | ||
id: folder.parentId as number, | ||
}); | ||
|
||
if (!parent) { | ||
throw new Error('Parent folder not found'); | ||
} | ||
|
||
return parent.uuid === parentFolderContentId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/context/virtual-drive/folders/application/RetryFolderDeleter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import Logger from 'electron-log'; | ||
import { FolderDeleter } from './FolderDeleter'; | ||
import { MaxRetriesDeletingFolderError } from '../domain/errors/MaxRetriesDeletingFolderError'; | ||
|
||
export class RetryFolderDeleter { | ||
private static NUMBER_OF_RETRIES = 2; | ||
private static MILLISECOND_BETWEEN_TRIES = 1_000; | ||
private static INITIAL_DELAY = 100; | ||
constructor(private readonly deleter: FolderDeleter) {} | ||
async retryDeleter(asyncFunction: () => Promise<any>) { | ||
let retryCount = 0; | ||
|
||
while (retryCount <= RetryFolderDeleter.NUMBER_OF_RETRIES) { | ||
try { | ||
const result = await asyncFunction(); | ||
return result; | ||
} catch (error: unknown) { | ||
if (error instanceof Error) { | ||
Logger.warn( | ||
`Folder deleter attempt ${retryCount + 1} failed: ${error.message}` | ||
); | ||
} else { | ||
Logger.warn( | ||
`Folder deleter attempt ${ | ||
retryCount + 1 | ||
} failed with an unknown error.` | ||
); | ||
} | ||
|
||
await new Promise((resolve) => { | ||
setTimeout(resolve, RetryFolderDeleter.MILLISECOND_BETWEEN_TRIES); | ||
}); | ||
|
||
retryCount++; | ||
} | ||
} | ||
throw new MaxRetriesDeletingFolderError( | ||
RetryFolderDeleter.NUMBER_OF_RETRIES | ||
); | ||
} | ||
|
||
async run(folder: string): Promise<any> { | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, RetryFolderDeleter.INITIAL_DELAY); | ||
}); | ||
|
||
const deleter = () => this.deleter.run(folder); | ||
return this.retryDeleter(deleter); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/context/virtual-drive/folders/domain/errors/MaxRetriesDeletingFolderError.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class MaxRetriesDeletingFolderError extends Error { | ||
constructor(retriesNumber: number) { | ||
super(`Max retries (${retriesNumber}) reached. Deleter still failed.`); | ||
} | ||
} |
Oops, something went wrong.