@intuitionrobotics/thunderstorm
Version:
41 lines • 2.63 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CleanupScheduler = exports.CleanupScheduler_Class = void 0;
const ts_common_1 = require("@intuitionrobotics/ts-common");
const firebase_function_1 = require("@intuitionrobotics/firebase/app-backend/functions/firebase-function");
const FirebaseModule_1 = require("@intuitionrobotics/firebase/app-backend/FirebaseModule");
const dispatch_onCleanupSchedulerAct = new ts_common_1.Dispatcher("__onCleanupSchedulerAct");
class CleanupScheduler_Class extends firebase_function_1.FirebaseScheduledFunction {
constructor() {
super("CleanupScheduler");
this.onScheduledEvent = () => __awaiter(this, void 0, void 0, function* () {
const cleanupStatusCollection = FirebaseModule_1.FirebaseModule.createAdminSession().getFirestore().getCollection('cleanup-status', ["moduleKey"]);
const cleanups = dispatch_onCleanupSchedulerAct.dispatchModule();
yield Promise.all(cleanups.map((cleanupItem) => __awaiter(this, void 0, void 0, function* () {
const doc = yield cleanupStatusCollection.queryUnique({ where: { moduleKey: cleanupItem.moduleKey } });
if (doc && doc.timestamp + cleanupItem.interval > (0, ts_common_1.currentTimeMillies)())
return;
try {
yield cleanupItem.cleanup();
yield cleanupStatusCollection.upsert({ timestamp: (0, ts_common_1.currentTimeMillies)(), moduleKey: cleanupItem.moduleKey });
}
catch (e) {
this.logWarning(`cleanup of ${cleanupItem.moduleKey} has failed with error '${e}'`);
}
})));
});
this.setSchedule('every 1 hours');
}
}
exports.CleanupScheduler_Class = CleanupScheduler_Class;
exports.CleanupScheduler = new CleanupScheduler_Class();
//# sourceMappingURL=CleanupScheduler.js.map