Cleanup also default tenant logs
MODIFY
src/scheduler/tasks/LoggingDatabaseTableCleanupTask.ts
src/scheduler/tasks/LoggingDatabaseTableCleanupTask.ts
+import { LoggingDatabaseTableCleanupTaskConfig, TaskConfig } from '../../types/TaskConfig';
+
+import Constants from '../../utils/Constants';
import { LockEntity } from '../../types/Locking';
import LockingManager from '../../locking/LockingManager';
import Logging from '../../utils/Logging';
-import { LoggingDatabaseTableCleanupTaskConfig } from '../../types/TaskConfig';
import LoggingStorage from '../../storage/mongodb/LoggingStorage';
import SchedulerTask from '../SchedulerTask';
import { ServerAction } from '../../types/Server';
@@ -11,20 +13,32 @@ import moment from 'moment';
const MODULE_NAME = 'LoggingDatabaseTableCleanupTask';
export default class LoggingDatabaseTableCleanupTask extends SchedulerTask {
+ public async run(name: string, config: TaskConfig): Promise<void> {
+ // Delete
+ await this.deleteLogs(Constants.DEFAULT_TENANT, config);
+ // Call for all Tenants
+ await super.run(name, config);
+ }
+
async processTenant(tenant: Tenant, config: LoggingDatabaseTableCleanupTaskConfig): Promise<void> {
+ // Delete
+ await this.deleteLogs(tenant.id, config);
+ }
+
+ private async deleteLogs(tenantID: string, config: LoggingDatabaseTableCleanupTaskConfig) {
// Get the lock
- const logsCleanUpLock = LockingManager.createExclusiveLock(tenant.id, LockEntity.LOGGING, 'cleanup');
+ const logsCleanUpLock = LockingManager.createExclusiveLock(tenantID, LockEntity.LOGGING, 'cleanup');
if (await LockingManager.acquire(logsCleanUpLock)) {
try {
// Delete date
const deleteUpToDate = moment().subtract(config.retentionPeriodWeeks, 'w').startOf('week').toDate();
// Delete
- let result = await LoggingStorage.deleteLogs(tenant.id, deleteUpToDate);
+ let result = await LoggingStorage.deleteLogs(tenantID, deleteUpToDate);
// Ok?
if (result.ok === 1) {
// Ok
Logging.logSecurityInfo({
- tenantID: tenant.id,
+ tenantID: tenantID,
action: ServerAction.LOGS_CLEANUP,
module: MODULE_NAME, method: 'run',
message: `${result.n} Log(s) have been deleted before '${moment(deleteUpToDate).format('DD/MM/YYYY h:mm A')}'`
@@ -32,7 +46,7 @@ export default class LoggingDatabaseTableCleanupTask extends SchedulerTask {
} else {
// Error
Logging.logError({
- tenantID: tenant.id,
+ tenantID: tenantID,
action: ServerAction.LOGS_CLEANUP,
module: MODULE_NAME, method: 'run',
message: `An error occurred when deleting Logs before '${moment(deleteUpToDate).format('DD/MM/YYYY h:mm A')}'`,
@@ -42,12 +56,12 @@ export default class LoggingDatabaseTableCleanupTask extends SchedulerTask {
// Delete date
const securityDeleteUpToDate: Date = moment().subtract(config.securityRetentionPeriodWeeks, 'w').startOf('week').toDate();
// Delete Security Logs
- result = await LoggingStorage.deleteSecurityLogs(tenant.id, securityDeleteUpToDate);
+ result = await LoggingStorage.deleteSecurityLogs(tenantID, securityDeleteUpToDate);
// Ok?
if (result.ok === 1) {
// Ok
Logging.logSecurityInfo({
- tenantID: tenant.id,
+ tenantID: tenantID,
action: ServerAction.LOGS_CLEANUP,
module: MODULE_NAME, method: 'run',
message: `${result.n} Security Log(s) have been deleted before '${moment(securityDeleteUpToDate).format('DD/MM/YYYY h:mm A')}'`
@@ -55,7 +69,7 @@ export default class LoggingDatabaseTableCleanupTask extends SchedulerTask {
} else {
// Error
Logging.logSecurityError({
- tenantID: tenant.id,
+ tenantID: tenantID,
action: ServerAction.LOGS_CLEANUP,
module: MODULE_NAME, method: 'run',
message: `An error occurred when deleting Security Logs before '${moment(securityDeleteUpToDate).format('DD/MM/YYYY h:mm A')}'`,
@@ -64,7 +78,7 @@ export default class LoggingDatabaseTableCleanupTask extends SchedulerTask {
}
} catch (error) {
// Log error
- Logging.logActionExceptionMessage(tenant.id, ServerAction.LOGS_CLEANUP, error);
+ Logging.logActionExceptionMessage(tenantID, ServerAction.LOGS_CLEANUP, error);
} finally {
// Release the lock
await LockingManager.release(logsCleanUpLock);

Cleanup also default tenant logs