Skip to content

Commit

Permalink
feat(history-query): reset cache on history query (item) update
Browse files Browse the repository at this point in the history
  • Loading branch information
burgerni10 committed Jun 21, 2023
1 parent b9b7750 commit 2263e9e
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 89 deletions.
9 changes: 7 additions & 2 deletions backend/src/engine/history-query-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,13 @@ export default class HistoryQueryEngine extends BaseEngine {
await this.historyQueries.get(historyId)?.updateItem(item);
}

async stopHistoryQuery(historyId: string): Promise<void> {
await this.historyQueries.get(historyId)?.stop();
async stopHistoryQuery(historyId: string, resetCache = false): Promise<void> {
const historyQuery = this.historyQueries.get(historyId);
if (!historyQuery) {
return;
}

await historyQuery.stop(resetCache);
this.historyQueries.delete(historyId);
}

Expand Down
23 changes: 19 additions & 4 deletions backend/src/engine/history-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,17 @@ const createdSouth = {
connect: jest.fn(),
historyQueryHandler: jest.fn(),
addItem: jest.fn(),
deleteItem: jest.fn()
deleteItem: jest.fn(),
resetCache: jest.fn()
};
const createdNorth = {
start: jest.fn(),
stop: jest.fn(),
connect: jest.fn(),
cacheValues: jest.fn(),
cacheFile: jest.fn(),
resetCache: jest.fn()
};
const createdNorth = { start: jest.fn(), stop: jest.fn(), connect: jest.fn(), cacheValues: jest.fn(), cacheFile: jest.fn() };

describe('HistoryQuery enabled', () => {
beforeEach(async () => {
Expand Down Expand Up @@ -193,8 +201,13 @@ describe('HistoryQuery enabled', () => {
await historyQuery.start();
await historyQuery.stop();
expect(clearIntervalSpy).toHaveBeenCalledTimes(1);
expect(createdSouth.stop);
expect(createdNorth.stop);
expect(createdSouth.stop).toHaveBeenCalledTimes(1);
expect(createdNorth.stop).toHaveBeenCalledTimes(1);
expect(createdNorth.resetCache).not.toHaveBeenCalled();
expect(createdSouth.resetCache).not.toHaveBeenCalled();
await historyQuery.stop(true);
expect(createdNorth.resetCache).toHaveBeenCalledTimes(1);
expect(createdSouth.resetCache).toHaveBeenCalledTimes(1);
});

it('should properly finish', async () => {
Expand Down Expand Up @@ -333,6 +346,8 @@ describe('HistoryQuery disabled', () => {
await historyQuery.stop();
expect(clearIntervalSpy).not.toHaveBeenCalled();
expect(createdSouth.stop).not.toHaveBeenCalled();
expect(createdSouth.resetCache).not.toHaveBeenCalled();
expect(createdNorth.stop).not.toHaveBeenCalled();
expect(createdNorth.resetCache).not.toHaveBeenCalled();
});
});
8 changes: 7 additions & 1 deletion backend/src/engine/history-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,21 @@ export default class HistoryQuery {
/**
* Stop history query
*/
async stop(): Promise<void> {
async stop(resetCache = false): Promise<void> {
if (this.finishInterval) {
clearInterval(this.finishInterval);
}
if (this.south) {
await this.south.stop();
if (resetCache) {
await this.south.resetCache();
}
}
if (this.north) {
await this.north.stop();
if (resetCache) {
await this.north.resetCache();
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions backend/src/north/north-connector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const getValuesToSendMock = jest.fn();
const getFileToSend = jest.fn();
const valueCacheIsEmpty = jest.fn();
const fileCacheIsEmpty = jest.fn();
const removeAllErrorFiles = jest.fn();
const removeAllCacheFiles = jest.fn();

// Mock services
jest.mock('../service/repository.service');
Expand Down Expand Up @@ -53,10 +55,11 @@ jest.mock(
{ filename: 'file3.name', modificationDate: '', size: 3 }
]),
cacheFile: jest.fn(),
removeErrorFiles: jest.fn(),
removeFiles: jest.fn(),
retryErrorFiles: jest.fn(),
removeAllErrorFiles: jest.fn(),
removeAllErrorFiles: removeAllErrorFiles,
retryAllErrorFiles: jest.fn(),
removeAllCacheFiles: removeAllCacheFiles,
getFileToSend: getFileToSend,
removeFileFromQueue: jest.fn(),
manageErroredFiles: jest.fn(),
Expand Down Expand Up @@ -466,4 +469,10 @@ describe('NorthConnector disabled', () => {
expect(anotherLogger.trace).toHaveBeenCalledTimes(1);
expect(logger.trace).not.toHaveBeenCalled();
});

it('should reset cache', async () => {
await north.resetCache();
expect(removeAllErrorFiles).toHaveBeenCalledTimes(1);
expect(removeAllCacheFiles).toHaveBeenCalledTimes(1);
});
});
7 changes: 6 additions & 1 deletion backend/src/north/north-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ export default class NorthConnector {
*/
async removeErrorFiles(filenames: Array<string>): Promise<void> {
this.logger.trace(`Removing ${filenames.length} error files from North connector "${this.configuration.name}"...`);
await this.fileCacheService.removeErrorFiles(filenames);
await this.fileCacheService.removeFiles(this.fileCacheService.errorFolder, filenames);
}

/**
Expand Down Expand Up @@ -374,4 +374,9 @@ export default class NorthConnector {
setLogger(value: pino.Logger) {
this.logger = value;
}

async resetCache(): Promise<void> {
await this.fileCacheService.removeAllErrorFiles();
await this.fileCacheService.removeAllCacheFiles();
}
}
6 changes: 6 additions & 0 deletions backend/src/repository/south-cache.repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,10 @@ describe('South cache repository', () => {
expect(database.prepare).toHaveBeenCalledWith('DELETE FROM cache_history WHERE scan_mode_id = ?;');
expect(run).toHaveBeenCalledWith('id1');
});

it('should reset cache', () => {
repository.resetDatabase();
expect(database.prepare).toHaveBeenCalledWith('DELETE FROM cache_history;');
expect(run).toHaveBeenCalledTimes(1);
});
});
5 changes: 5 additions & 0 deletions backend/src/repository/south-cache.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ export default class SouthCacheRepository {
const query = `DELETE FROM ${SOUTH_CACHE_TABLE} WHERE scan_mode_id = ?;`;
this.database.prepare(query).run(id);
}

resetDatabase(): void {
const query = `DELETE FROM ${SOUTH_CACHE_TABLE};`;
this.database.prepare(query).run();
}
}
27 changes: 22 additions & 5 deletions backend/src/service/cache/file-cache.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe('FileCache', () => {
it('should remove error files', async () => {
const filenames = ['file1.name', 'file2.name', 'file3.name'];

await cache.removeErrorFiles(filenames);
await cache.removeFiles(cache.errorFolder, filenames);
expect(fs.unlink).toHaveBeenNthCalledWith(1, path.join(path.resolve('myCacheFolder', 'files-errors'), filenames[0]));
expect(fs.unlink).toHaveBeenNthCalledWith(2, path.join(path.resolve('myCacheFolder', 'files-errors'), filenames[1]));
expect(fs.unlink).toHaveBeenNthCalledWith(3, path.join(path.resolve('myCacheFolder', 'files-errors'), filenames[2]));
Expand Down Expand Up @@ -160,18 +160,35 @@ describe('FileCache', () => {
it('should remove all error files when the error folder is not empty', async () => {
const filenames = ['file1.name', 'file2.name', 'file3.name'];
fs.readdir = jest.fn().mockReturnValue(Promise.resolve(filenames));
cache.removeErrorFiles = jest.fn();
cache.removeFiles = jest.fn();

await cache.removeAllErrorFiles();
expect(cache.removeErrorFiles).toHaveBeenCalledWith(filenames);
expect(cache.removeFiles).toHaveBeenCalledWith(cache.errorFolder, filenames);
});

it('should not remove any error file when the error folder is empty', async () => {
fs.readdir = jest.fn().mockReturnValue(Promise.resolve([]));
cache.removeErrorFiles = jest.fn();
cache.removeFiles = jest.fn();

await cache.removeAllErrorFiles();
expect(cache.removeErrorFiles).not.toHaveBeenCalled();
expect(cache.removeFiles).not.toHaveBeenCalled();
});

it('should remove all cache files when the folder is not empty', async () => {
const filenames = ['file1.name', 'file2.name', 'file3.name'];
fs.readdir = jest.fn().mockReturnValue(Promise.resolve(filenames));
cache.removeFiles = jest.fn();

await cache.removeAllCacheFiles();
expect(cache.removeFiles).toHaveBeenCalledWith(cache.fileFolder, filenames);
});

it('should not remove any cache file when the folder is empty', async () => {
fs.readdir = jest.fn().mockReturnValue(Promise.resolve([]));
cache.removeFiles = jest.fn();

await cache.removeAllCacheFiles();
expect(cache.removeFiles).not.toHaveBeenCalled();
});

it('should retry all error files when the error folder is not empty', async () => {
Expand Down
Loading

0 comments on commit 2263e9e

Please sign in to comment.