This commit is contained in:
Ajay Ramachandran
2021-07-12 11:05:14 -04:00
9 changed files with 321 additions and 2 deletions

View File

@@ -77,7 +77,8 @@ addDefaults(config, {
name: "vipUsers"
}]
},
diskCache: null
diskCache: null,
crons: null
});
// Add defaults

View File

@@ -0,0 +1,63 @@
import { CronJob } from "cron";
import { config as serverConfig } from "../config";
import { Logger } from "../utils/logger";
import { db } from "../databases/databases";
import { DBSegment } from "../types/segments.model";
const jobConfig = serverConfig?.crons?.downvoteSegmentArchive;
export const archiveDownvoteSegment = async (dayLimit: number, voteLimit: number, runTime?: number): Promise<number> => {
const timeNow = runTime || new Date().getTime();
const threshold = dayLimit * 86400000;
Logger.info(`DownvoteSegmentArchiveJob starts at ${timeNow}`);
try {
// insert into archive sponsorTime
await db.prepare(
'run',
`INSERT INTO "archivedSponsorTimes"
SELECT *
FROM "sponsorTimes"
WHERE "votes" < ? AND (? - "timeSubmitted") > ?`,
[
voteLimit,
timeNow,
threshold
]
) as DBSegment[];
} catch (err) {
Logger.error('Execption when insert segment in archivedSponsorTimes');
Logger.error(err);
return 1;
}
// remove from sponsorTime
try {
await db.prepare(
'run',
'DELETE FROM "sponsorTimes" WHERE "votes" < ? AND (? - "timeSubmitted") > ?',
[
voteLimit,
timeNow,
threshold
]
) as DBSegment[];
} catch (err) {
Logger.error('Execption when deleting segment in sponsorTimes');
Logger.error(err);
return 1;
}
Logger.info('DownvoteSegmentArchiveJob finished');
return 0;
};
const DownvoteSegmentArchiveJob = new CronJob(
jobConfig?.schedule || new Date(1),
() => archiveDownvoteSegment(jobConfig?.timeThresholdInDays, jobConfig?.voteThreshold)
);
export default DownvoteSegmentArchiveJob;

13
src/cronjob/index.ts Normal file
View File

@@ -0,0 +1,13 @@
import { Logger } from "../utils/logger";
import { config } from "../config";
import DownvoteSegmentArchiveJob from "./downvoteSegmentArchiveJob";
export function startAllCrons (): void {
if (config?.crons?.enabled) {
Logger.info("Crons started");
DownvoteSegmentArchiveJob.start();
} else {
Logger.info("Crons dissabled");
}
}

View File

@@ -2,13 +2,17 @@ import {config} from "./config";
import {initDb} from './databases/databases';
import {createServer} from "./app";
import {Logger} from "./utils/logger";
import {startAllCrons} from "./cronjob";
async function init() {
await initDb();
createServer(() => {
Logger.info("Server started on port " + config.port + ".");
// ignite cron job after server created
startAllCrons();
});
}
init();

View File

@@ -44,6 +44,7 @@ export interface SBSConfig {
postgres?: PoolConfig;
dumpDatabase?: DumpDatabase;
diskCache: CacheOptions;
crons: CronJobOptions;
}
export interface WebhookConfig {
@@ -81,3 +82,17 @@ export interface DumpDatabaseTable {
name: string;
order?: string;
}
export interface CronJobDefault {
schedule: string;
}
export interface CronJobOptions {
enabled: boolean;
downvoteSegmentArchive: CronJobDefault & DownvoteSegmentArchiveCron;
}
export interface DownvoteSegmentArchiveCron {
voteThreshold: number;
timeThresholdInDays: number;
}