snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s

This commit is contained in:
upstream-mirror
2026-09-16 01:21:02 +05:00
commit 30810eecd7
10161 changed files with 987110 additions and 0 deletions

2810
packages/cron/CHANGELOG.md Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,32 @@
{
"name": "@rocket.chat/cron",
"version": "0.1.60",
"private": true,
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"files": [
"/dist"
],
"scripts": {
"build": "rm -rf dist && tsc -p tsconfig.json",
"dev": "tsc -p tsconfig.json --watch --preserveWatchOutput",
"lint": "eslint .",
"lint:fix": "eslint --fix ."
},
"dependencies": {
"@rocket.chat/agenda": "workspace:^",
"@rocket.chat/core-typings": "workspace:^",
"@rocket.chat/logger": "workspace:^",
"@rocket.chat/models": "workspace:^",
"@rocket.chat/random": "workspace:^",
"mongodb": "6.16.0"
},
"devDependencies": {
"@rocket.chat/tsconfig": "workspace:*",
"eslint": "~9.39.5",
"typescript": "~5.9.3"
},
"volta": {
"extends": "../../package.json"
}
}

200
packages/cron/src/index.ts Normal file
View File

@@ -0,0 +1,200 @@
import { type Job, Agenda } from '@rocket.chat/agenda';
import { Logger } from '@rocket.chat/logger';
import { CronHistory } from '@rocket.chat/models';
import { Random } from '@rocket.chat/random';
import type { Db } from 'mongodb';
const logger = new Logger('Cron');
const runCronJobFunctionAndPersistResult = async (fn: () => Promise<any>, jobName: string): Promise<void> => {
const { insertedId } = await CronHistory.insertOne({
_id: Random.id(),
intendedAt: new Date(),
name: jobName,
startedAt: new Date(),
});
try {
const result = await fn();
await CronHistory.updateOne(
{ _id: insertedId },
{
$set: {
finishedAt: new Date(),
result,
},
},
);
return result;
} catch (error: any) {
await CronHistory.updateOne(
{ _id: insertedId },
{
$set: {
finishedAt: new Date(),
error: error?.stack ? error.stack : error,
},
},
);
throw error;
}
};
type ReservedJob = {
name: string;
callback: () => any | Promise<any>;
} & (
| {
schedule: string;
timestamped: false;
}
| {
when: Date;
timestamped: true;
}
);
export class AgendaCronJobs {
private reservedJobs: ReservedJob[] = [];
private scheduler: Agenda | undefined;
public get started(): boolean {
return Boolean(this.scheduler);
}
public async start(mongo: Db): Promise<void> {
this.scheduler = new Agenda({
mongo,
db: { collection: 'rocketchat_cron' },
defaultConcurrency: 1,
processEvery: '1 minute',
});
this.scheduler.on('start', (job: Job) => {
logger.debug({
msg: `Job "${job.attrs.name}" starting`,
jobId: job.attrs._id,
jobName: job.attrs.name,
nextRunAt: job.attrs.nextRunAt,
});
});
this.scheduler.on('complete', (job: Job) => {
logger.info({
msg: `Job "${job.attrs.name}" completed`,
jobId: job.attrs._id,
jobName: job.attrs.name,
lastRunAt: job.attrs.lastRunAt,
nextRunAt: job.attrs.nextRunAt,
duration:
job.attrs.lastFinishedAt && job.attrs.lastRunAt ? job.attrs.lastFinishedAt.getTime() - job.attrs.lastRunAt.getTime() : undefined,
});
});
this.scheduler.on('success', (job: Job) => {
logger.debug({
msg: `Job "${job.attrs.name}" succeeded`,
jobId: job.attrs._id,
jobName: job.attrs.name,
});
});
this.scheduler.on('fail', (err: unknown, job: Job) => {
logger.error({
msg: `Job "${job.attrs.name}" failed`,
jobId: job.attrs._id,
jobName: job.attrs.name,
err,
failCount: job.attrs.failCount,
failReason: job.attrs.failReason,
});
});
this.scheduler.on('error:database', (err: unknown) => {
logger.error({
msg: 'Database error in cron scheduler',
err,
});
});
this.scheduler.on('error', (err: unknown) => {
logger.error({
msg: 'Error in cron scheduler',
err,
});
});
this.scheduler.on('ready', () => {
logger.debug({ msg: 'Cron scheduler database ready' });
});
await this.scheduler.start();
for (const job of this.reservedJobs) {
if (job.timestamped) {
await this.addAtTimestamp(job.name, job.when, job.callback);
} else {
await this.add(job.name, job.schedule, job.callback);
}
}
this.reservedJobs = [];
}
public async add(name: string, schedule: string, callback: () => any | Promise<any>): Promise<void> {
if (!this.scheduler) {
return this.reserve({ name, schedule, callback, timestamped: false });
}
await this.define(name, callback);
await this.scheduler.every(schedule, name, {}, {});
logger.debug({ msg: `Cron job "${name}" scheduled`, jobName: name, schedule });
}
public async addAtTimestamp(name: string, when: Date, callback: () => any | Promise<any>): Promise<void> {
if (!this.scheduler) {
return this.reserve({ name, when, callback, timestamped: true });
}
await this.define(name, callback);
await this.scheduler.schedule(when, name, {});
logger.debug({ msg: `Cron job "${name}" scheduled at timestamp`, jobName: name, when });
}
public async remove(name: string): Promise<void> {
if (!this.scheduler) {
return this.unreserve(name);
}
await this.scheduler.cancel({ name });
logger.debug({ msg: `Cron job "${name}" removed`, jobName: name });
}
public async has(jobName: string): Promise<boolean> {
if (!this.scheduler) {
return Boolean(this.reservedJobs.find(({ name }) => name === jobName));
}
return this.scheduler.has({ name: jobName });
}
private async reserve(config: ReservedJob): Promise<void> {
this.reservedJobs = [...this.reservedJobs, config];
}
private async unreserve(jobName: string): Promise<void> {
this.reservedJobs = this.reservedJobs.filter(({ name }) => name !== jobName);
}
private async define(jobName: string, callback: () => any | Promise<any>): Promise<void> {
if (!this.scheduler) {
throw new Error('Scheduler is not running.');
}
this.scheduler.define(jobName, async () => {
await runCronJobFunctionAndPersistResult(async () => callback(), jobName);
});
}
}
export const cronJobs = new AgendaCronJobs();

View File

@@ -0,0 +1,10 @@
{
"extends": "@rocket.chat/tsconfig/server.json",
"compilerOptions": {
"rootDir": "./src",
"declaration": true,
"outDir": "./dist"
},
"include": ["./src/**/*"],
"exclude": ["./dist"]
}