Skip to content

Commit 4cdc36d

Browse files
committed
feat: added cron jobs and speedtest job
1 parent cb98195 commit 4cdc36d

6 files changed

Lines changed: 76 additions & 1 deletion

File tree

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"google-tts-api": "^2.0.2",
4040
"groq-sdk": "^0.27.0",
4141
"is-spaghetti-code": "^1.0.1",
42+
"node-cron": "^4.2.1",
4243
"npmlog": "^7.0.1",
4344
"ollama": "^0.5.16",
4445
"openai": "^5.10.2",

src/commands/fork.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default async function (msg: Message) {
1717
https://github.com/mrepol742/project-canis
1818
1919
\`Canis for Telegram\`
20-
https://github.com/mrepol742/project-canis-ts
20+
https://github.com/mrepol742/project-canis-tg
2121
2222
This bot is not affiliated, endorsed, partner, or connected to Meta.
2323
Use it at your own RISK.

src/cron.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import cron from "node-cron";
2+
import speedtestJob, { info as speedtestInfo } from "./jobs/speedtest";
3+
import log from "./components/utils/log";
4+
5+
interface CronJobInfo {
6+
name: string;
7+
description?: string;
8+
schedule: string;
9+
runOnStartup?: boolean;
10+
}
11+
12+
interface CronJob {
13+
info: CronJobInfo;
14+
job: () => Promise<void> | void;
15+
}
16+
17+
// register more cron jobin here
18+
const jobs: CronJob[] = [{ info: speedtestInfo, job: speedtestJob }];
19+
20+
export function registerCronJobs() {
21+
for (const { info, job } of jobs) {
22+
cron.schedule(info.schedule, job);
23+
log.info(info.name, `Registered cron job on (${info.schedule})`);
24+
25+
if (info.runOnStartup) {
26+
log.info(info.name, "Running on startup...");
27+
job();
28+
}
29+
}
30+
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import dotenv from "dotenv";
22
dotenv.config();
33

44
import "./instrument";
5+
import { registerCronJobs } from "./cron";
56
import { client } from "./components/client";
67
import { checkRequirements } from "./components/utils/requirements";
78
import log from "./components/utils/log";
@@ -28,6 +29,7 @@ async function main() {
2829
await Promise.all([
2930
monitor.start(),
3031
phishtank.startAutoUpdateLoop(),
32+
registerCronJobs(),
3133
(async () => {
3234
if (autoUpdateDaily || autoUpdateTimer) return;
3335
await phishtank.init();

src/jobs/speedtest.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import speedTest from "speedtest-net";
2+
import log from "../components/utils/log";
3+
import redis from "../components/redis";
4+
5+
const CACHE_KEY = "speedtest:result";
6+
const CACHE_TTL = 60 * 60; // 1 hour
7+
8+
export const info = {
9+
name: "SpeedTestJob",
10+
description: "Runs an internet speed test and logs results.",
11+
schedule: "0 * * * *", // every hour
12+
runOnStartup: true,
13+
};
14+
15+
export default async function () {
16+
try {
17+
const results = await speedTest({ acceptLicense: true, acceptGdpr: true });
18+
console.log("")// to create new line
19+
log.info(
20+
"SpeedTestJob",
21+
`Download: ${results.download.bandwidth / 125000} Mbps`,
22+
);
23+
log.info(
24+
"SpeedTestJob",
25+
`Upload: ${results.upload.bandwidth / 125000} Mbps`,
26+
);
27+
log.info("SpeedTestJob", `Ping: ${results.ping.latency} ms`);
28+
await redis.set(CACHE_KEY, JSON.stringify(results), { EX: CACHE_TTL });
29+
} catch (err) {
30+
log.error("SpeedTestJob", err);
31+
}
32+
}

0 commit comments

Comments
 (0)