Add 1h expiration date on migration lock
MODIFY
src/migration/MigrationHandler.ts
src/migration/MigrationHandler.ts
@@ -67,11 +67,10 @@ export default class MigrationHandler {
return;
}
// Create a Lock for migration
- const migrationLock = LockingManager.createExclusiveLock(Constants.DEFAULT_TENANT, LockEntity.DATABASE, 'migration');
+ const migrationLock = LockingManager.createExclusiveLock(Constants.DEFAULT_TENANT, LockEntity.DATABASE, 'migration', 3600);
if (await LockingManager.acquire(migrationLock)) {
try {
const startMigrationTime = moment();
- const currentMigrationTasks: MigrationTask[] = [];
// Log
await Logging.logInfo({
tenantID: Constants.DEFAULT_TENANT,
@@ -80,6 +79,61 @@ export default class MigrationHandler {
message: `Running ${processAsyncTasksOnly ? 'asynchronous' : 'synchronous'} migration tasks...`
});
// Create tasks
+ const migrationTasks = MigrationHandler.createMigrationTasks();
+ // Get the already done migrations from the DB
+ const migrationTasksCompleted = await MigrationStorage.getMigrations();
+ // Check
+ for (const migrationTask of migrationTasks) {
+ // Check if not already done
+ const foundMigrationTaskCompleted = migrationTasksCompleted.find((migrationTaskCompleted) =>
+ // Same name and version
+ (migrationTask.getName() === migrationTaskCompleted.name &&
+ migrationTask.getVersion() === migrationTaskCompleted.version)
+ );
+ // Already processed?
+ if (foundMigrationTaskCompleted) {
+ continue;
+ }
+ // Check
+ if (migrationTask.isAsynchronous() && processAsyncTasksOnly) {
+ // Execute Async
+ await MigrationHandler.executeTask(migrationTask);
+ } else if (!migrationTask.isAsynchronous() && !processAsyncTasksOnly) {
+ // Execute Sync
+ await MigrationHandler.executeTask(migrationTask);
+ }
+ }
+ // Log Total Processing Time
+ const totalMigrationTimeSecs = moment.duration(moment().diff(startMigrationTime)).asSeconds();
+ await Logging.logInfo({
+ tenantID: Constants.DEFAULT_TENANT,
+ action: ServerAction.MIGRATION,
+ module: MODULE_NAME, method: 'migrate',
+ message: `The ${processAsyncTasksOnly ? 'asynchronous' : 'synchronous'} migration has been run in ${totalMigrationTimeSecs} secs`
+ });
+ } catch (error) {
+ await Logging.logError({
+ tenantID: Constants.DEFAULT_TENANT,
+ action: ServerAction.MIGRATION,
+ module: MODULE_NAME, method: 'migrate',
+ message: error.message,
+ detailedMessages: { error: error.message, stack: error.stack }
+ });
+ } finally {
+ // Release lock
+ await LockingManager.release(migrationLock);
+ }
+ }
+ // Process async tasks one by one
+ if (!processAsyncTasksOnly) {
+ setTimeout(() => {
+ MigrationHandler.migrate(true).catch(() => { });
+ }, 5000);
+ }
+ }
+
+ private static createMigrationTasks(): MigrationTask[] {
+ const currentMigrationTasks: MigrationTask[] = [];
currentMigrationTasks.push(new SiteUsersHashIDsTask());
currentMigrationTasks.push(new AddTransactionRefundStatusTask());
currentMigrationTasks.push(new AddSensitiveDataInSettingsTask());
@@ -131,56 +185,7 @@ export default class MigrationHandler {
currentMigrationTasks.push(new CleanUpCarUsersWithDeletedUsersTask());
currentMigrationTasks.push(new ChangeAssetIssuerFieldTask());
currentMigrationTasks.push(new AddVisualIDPropertyToTagsTask());
- // Get the already done migrations from the DB
- const migrationTasksDone = await MigrationStorage.getMigrations();
- // Check
- for (const currentMigrationTask of currentMigrationTasks) {
- // Check if not already done
- const migrationTaskDone = migrationTasksDone.find((migrationTask) =>
- // Same name and version
- ((currentMigrationTask.getName() === migrationTask.name) &&
- (currentMigrationTask.getVersion() === migrationTask.version))
- );
- // Already processed?
- if (migrationTaskDone) {
- continue;
- }
- // Check
- if (currentMigrationTask.isAsynchronous() && processAsyncTasksOnly) {
- // Execute Async
- await MigrationHandler.executeTask(currentMigrationTask);
- } else if (!currentMigrationTask.isAsynchronous() && !processAsyncTasksOnly) {
- // Execute Sync
- await MigrationHandler.executeTask(currentMigrationTask);
- }
- }
- // Log Total Processing Time
- const totalMigrationTimeSecs = moment.duration(moment().diff(startMigrationTime)).asSeconds();
- await Logging.logInfo({
- tenantID: Constants.DEFAULT_TENANT,
- action: ServerAction.MIGRATION,
- module: MODULE_NAME, method: 'migrate',
- message: `The ${processAsyncTasksOnly ? 'asynchronous' : 'synchronous'} migration has been run in ${totalMigrationTimeSecs} secs`
- });
- } catch (error) {
- await Logging.logError({
- tenantID: Constants.DEFAULT_TENANT,
- action: ServerAction.MIGRATION,
- module: MODULE_NAME, method: 'migrate',
- message: error.message,
- detailedMessages: { error: error.message, stack: error.stack }
- });
- } finally {
- // Release lock
- await LockingManager.release(migrationLock);
- }
- }
- // Process async tasks one by one
- if (!processAsyncTasksOnly) {
- setTimeout(() => {
- MigrationHandler.migrate(true).catch(() => { });
- }, 5000);
- }
+ return currentMigrationTasks;
}
private static async executeTask(currentMigrationTask: MigrationTask): Promise<void> {

Add 1h expiration date on migration lock