Resume async tasks at server startup
MODIFY
src/async-task/AsyncTaskManager.ts
src/async-task/AsyncTaskManager.ts
@@ -17,7 +17,21 @@ import Utils from '../utils/Utils';
const MODULE_NAME = 'AsyncTaskManager';
export default class AsyncTaskManager {
- private static asyncTaskConfig = Configuration.getAsyncTaskConfig();
+ private static asyncTaskConfig;
+
+ public static async init(): Promise<void> {
+ // Get the conf
+ AsyncTaskManager.asyncTaskConfig = Configuration.getAsyncTaskConfig();
+ // Active?
+ if (AsyncTaskManager.asyncTaskConfig.active) {
+ // Turn all Running task to Pending
+ const updatedAsyncTasks = await AsyncTaskStorage.updateRunningAsyncTaskToPending();
+ // Run it
+ if (updatedAsyncTasks > 0) {
+ void AsyncTaskManager.handleAsyncTasks();
+ }
+ }
+ }
public static async handleAsyncTasks(): Promise<void> {
// Active?

MODIFY
src/start.ts
src/start.ts
+import AsyncTaskManager from './async-task/AsyncTaskManager';
import CentralRestServer from './server/rest/CentralRestServer';
import CentralSystemConfiguration from './types/configuration/CentralSystemConfiguration';
import CentralSystemRestServiceConfiguration from './types/configuration/CentralSystemRestServiceConfiguration';
@@ -130,6 +131,10 @@ export default class Bootstrap {
// Init the Scheduler
// -------------------------------------------------------------------------
await SchedulerManager.init();
+ // -------------------------------------------------------------------------
+ // Init the Asyn Task
+ // -------------------------------------------------------------------------
+ await AsyncTaskManager.init();
// Locks remain in storage if server crashes
// Delete acquired database locks with same hostname
await LockingManager.cleanupLocks(Configuration.isCloudFoundry() || Utils.isDevelopmentEnv());

MODIFY
src/storage/mongodb/AsyncTaskStorage.ts
src/storage/mongodb/AsyncTaskStorage.ts
@@ -132,6 +132,19 @@ export default class AsyncTaskStorage {
};
}
+ public static async updateRunningAsyncTaskToPending(): Promise<number> {
+ // Debug
+ const uniqueTimerID = Logging.traceStart(Constants.DEFAULT_TENANT, MODULE_NAME, 'updateRunningAsyncTaskToPending');
+ // Delete the AsyncTask
+ const result = await global.database.getCollection<AsyncTask>(Constants.DEFAULT_TENANT, 'asynctasks').updateMany(
+ { 'status': AsyncTaskStatus.RUNNING },
+ { '$set': { 'status': AsyncTaskStatus.PENDING }}
+ );
+ // Debug
+ await Logging.traceEnd(Constants.DEFAULT_TENANT, MODULE_NAME, 'updateRunningAsyncTaskToPending', uniqueTimerID);
+ return result.modifiedCount;
+ }
+
public static async deleteAsyncTask(id: string): Promise<void> {
// Debug
const uniqueTimerID = Logging.traceStart(Constants.DEFAULT_TENANT, MODULE_NAME, 'deleteAsyncTask');

Resume async tasks at server startup