-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #261 from swisstopo/feature/assets-247-asset-viewer
Automatically sync search index
- Loading branch information
Showing
8 changed files
with
113 additions
and
43 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
52 changes: 10 additions & 42 deletions
52
apps/server-asset-sg/src/features/assets/sync/asset-sync.controller.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
90 changes: 90 additions & 0 deletions
90
apps/server-asset-sg/src/features/assets/sync/asset-sync.service.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,90 @@ | ||
import fs from 'fs/promises'; | ||
import { Injectable, OnApplicationBootstrap } from '@nestjs/common'; | ||
import { SchedulerRegistry } from '@nestjs/schedule'; | ||
import { CronJob } from 'cron'; | ||
import { AssetRepo } from '@/features/assets/asset.repo'; | ||
import { AssetSearchService } from '@/features/assets/search/asset-search.service'; | ||
|
||
@Injectable() | ||
export class AssetSyncService implements OnApplicationBootstrap { | ||
constructor( | ||
private readonly assetSearchService: AssetSearchService, | ||
private readonly schedulerRegistry: SchedulerRegistry, | ||
private readonly assetRepo: AssetRepo | ||
) {} | ||
async onApplicationBootstrap() { | ||
const syncFileExists = await this.isSyncRunning(); | ||
if (syncFileExists) { | ||
void fs.rm(assetSyncFile); | ||
} | ||
|
||
if (process.env.ANONYMOUS_MODE === 'true') { | ||
console.log('Anonymous Mode is activated. Search Index will be automatically synced.'); | ||
await this.startSyncIfIndexOutOfSync(); | ||
|
||
const every20Minutes = '*/20 * * * *'; | ||
const job = new CronJob(every20Minutes, () => this.startSyncIfIndexOutOfSync()); | ||
this.schedulerRegistry.addCronJob('elasticIndexSync', job); | ||
job.start(); | ||
} | ||
} | ||
|
||
async show(): Promise<AssetSyncState | null> { | ||
try { | ||
const data = await fs.readFile(assetSyncFile, { encoding: 'utf-8' }); | ||
return JSON.parse(data); | ||
} catch (e) { | ||
if ((e as { code?: string }).code === 'ENOENT') { | ||
return null; | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
async isSyncRunning() { | ||
return await fs | ||
.access(assetSyncFile) | ||
.then(() => true) | ||
.catch(() => false); | ||
} | ||
|
||
async start(): Promise<void> { | ||
if (await this.isSyncRunning()) { | ||
console.debug('AssetSyncService.start: Sync already running.'); | ||
return; | ||
} | ||
|
||
const writeProgress = (progress: number): Promise<void> => { | ||
const state: AssetSyncState = { progress: parseFloat(progress.toFixed(3)) }; | ||
const data = JSON.stringify(state); | ||
return fs.writeFile(assetSyncFile, data, { encoding: 'utf-8' }); | ||
}; | ||
|
||
await writeProgress(0); | ||
setTimeout(async () => { | ||
await this.assetSearchService.syncWithDatabase(writeProgress); | ||
await fs.rm(assetSyncFile); | ||
}); | ||
} | ||
|
||
private async startSyncIfIndexOutOfSync() { | ||
console.debug(`startSyncIfIndexOutOfSync.`); | ||
const numberOfAssets = await this.assetRepo.count(); | ||
const numberOfIndexedAssets = await this.assetSearchService.count(); | ||
console.debug(`Found ${numberOfAssets} Assets and ${numberOfIndexedAssets} Indexed documents.`); | ||
if (numberOfAssets !== numberOfIndexedAssets) { | ||
await this.start(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* The file into which the progress of the current asset sync is written. | ||
* This allows the sync to be shared across all users and requests without requiring a database entry. | ||
* Note that the file path is relative to the project root, _not_ to this file. | ||
*/ | ||
const assetSyncFile = './asset-sync-progress.tmp.json'; | ||
|
||
interface AssetSyncState { | ||
progress: number; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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