Skip to content

Commit

Permalink
feat: Log next scheduler run time
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxxMD committed Apr 4, 2024
1 parent 135cb5c commit 4a43d7b
Show file tree
Hide file tree
Showing 5 changed files with 649 additions and 657 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"body-parser": "^1.19.0",
"common-tags": "^1.8.2",
"concat-stream": "^2.0.0",
"croner": "^6.0.7",
"croner": "^7.0.5",
"dayjs": "^1.11.8",
"deepmerge": "^4.3.1",
"discord.js": "^14.14.1",
Expand Down
5 changes: 2 additions & 3 deletions src/common/funcs/processPendingDigests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {TautulliRequest} from "../db/models/TautulliRequest.js";
import {buildMessages} from "../../discord/builder.js";
import {ErrorWithCause} from "pony-cause";

export const processPendingDigests = async (digest: DigestData, parentLogger: Logger, id?: string) => {
const digestLogger = childLogger(parentLogger, `Digest ${id ?? digest.slug}`);
export const processPendingDigests = async (digest: DigestData, parentLogger: Logger) => {

const {
slug,
Expand All @@ -16,7 +15,7 @@ export const processPendingDigests = async (digest: DigestData, parentLogger: Lo
}
} = digest;

const logger = childLogger(digestLogger, slug);
const logger = childLogger(parentLogger, slug);

let sentEvents = 0;
let sentMessages = 0;
Expand Down
8 changes: 5 additions & 3 deletions src/scheduler/scheduler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { childLogger, Logger } from "@foxxmd/logging";
import dayjs from "dayjs";
import {CronJob, SimpleIntervalJob, ToadScheduler} from "toad-scheduler";
import {createHeartbeatTask} from "./tasks/heartbeatTask.js";
import {OperatorConfig} from "../common/infrastructure/OperatorConfig.js";
import {createProcessPendingDigestsTask} from "./tasks/createProcessPendingDigestsTask.js";
import {mergeArr} from "../utils/index.js";
import {Cron} from 'croner';

export const initScheduler = (config: OperatorConfig, parentLogger: Logger) => {
const scheduler = new ToadScheduler()
Expand All @@ -18,11 +19,12 @@ export const initScheduler = (config: OperatorConfig, parentLogger: Logger) => {
const crons = Array.isArray(digest.cron) ? digest.cron : [digest.cron];
for(const cron of crons) {
try {
const nextCron = Cron(cron).nextRun();
scheduler.addCronJob(new CronJob({
cronExpression: cron,
},
createProcessPendingDigestsTask(`${digest.name !== undefined ? `${digest.name} ` : ''}${cron}`, digest, parentLogger)));
logger.info(`Digest ${digest.slug} will run at ${cron}`);
createProcessPendingDigestsTask(`${digest.name !== undefined ? `${digest.name} ` : ''}${cron}`, digest, parentLogger, cron)));
logger.info(`Digest ${digest.slug} started with CRON ${cron.padEnd(16, ' ')} -> Next run at ${dayjs(nextCron).format()}`);
} catch (e) {
logger.error(new Error(`Failed to create digest for cron ${cron}`, {cause: e}))
}
Expand Down
15 changes: 9 additions & 6 deletions src/scheduler/tasks/createProcessPendingDigestsTask.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
import { childLogger, Logger } from "@foxxmd/logging";
import dayjs from "dayjs";
import {AsyncTask} from "toad-scheduler";
import {PromisePool} from "@supercharge/promise-pool";
import {Cron} from 'croner';
import {DigestData} from "../../common/infrastructure/OperatorConfig.js";
import {processPendingDigests} from "../../common/funcs/processPendingDigests.js";

export const createProcessPendingDigestsTask = (id: string, digest: DigestData, parentLogger: Logger) => {
const digestLogger = childLogger(parentLogger, `Digest ${id}`);
export const createProcessPendingDigestsTask = (id: string, digest: DigestData, parentLogger: Logger, cron?: string) => {
const digestLogger = childLogger(parentLogger, `Cron ${id}`);
return new AsyncTask(
`Digest - ${id}`,
`Digest - ${id} ${digest.slug}`,
(): Promise<any> => {
return PromisePool
.withConcurrency(1)
.for([digest])
.process(async (digest: DigestData) => {
await processPendingDigests(digest, parentLogger, id);
await processPendingDigests(digest, digestLogger);
}).then(({results, errors}) => {
if (errors.length > 0) {
digestLogger.error(`Encountered errors!`);
for (const err of errors) {
digestLogger.error(err);
}
} else {
//digestLogger.info(`TOTAL: Sent ${results.reduce((acc, curr) => acc += curr.sentEmbeds, 0)} embeds in ${results.reduce((acc, curr) => acc += curr.sentMessages, 0)} messages`);
}
if(cron !== undefined) {
digestLogger.info(`Next Run at ${dayjs(Cron(cron).nextRun()).format()}`);
}
});
},
Expand Down
Loading

0 comments on commit 4a43d7b

Please sign in to comment.