@intuitionrobotics/thunderstorm
Version:
28 lines • 1.55 kB
JavaScript
import { currentTimeMillies, Dispatcher } from "@intuitionrobotics/ts-common";
import { FirebaseScheduledFunction } from "@intuitionrobotics/firebase/app-backend/functions/firebase-function";
import { FirebaseModule } from "@intuitionrobotics/firebase/app-backend/FirebaseModule";
const dispatch_onCleanupSchedulerAct = new Dispatcher("__onCleanupSchedulerAct");
export class CleanupScheduler_Class extends FirebaseScheduledFunction {
constructor() {
super("CleanupScheduler");
this.setSchedule('every 1 hours');
}
onScheduledEvent = async () => {
const cleanupStatusCollection = FirebaseModule.createAdminSession().getFirestore().getCollection('cleanup-status', ["moduleKey"]);
const cleanups = dispatch_onCleanupSchedulerAct.dispatchModule();
await Promise.all(cleanups.map(async (cleanupItem) => {
const doc = await cleanupStatusCollection.queryUnique({ where: { moduleKey: cleanupItem.moduleKey } });
if (doc && doc.timestamp + cleanupItem.interval > currentTimeMillies())
return;
try {
await cleanupItem.cleanup();
await cleanupStatusCollection.upsert({ timestamp: currentTimeMillies(), moduleKey: cleanupItem.moduleKey });
}
catch (e) {
this.logWarning(`cleanup of ${cleanupItem.moduleKey} has failed with error '${e}'`);
}
}));
};
}
export const CleanupScheduler = new CleanupScheduler_Class();
//# sourceMappingURL=CleanupScheduler.js.map