Add Hourly, Daily, Weekly, and Monthly cron function to workers

This commit is contained in:
timothycarambat
2023-10-18 17:29:43 -07:00
parent 1f950c7609
commit 9c012b46a1
5 changed files with 210 additions and 3 deletions
+2 -1
View File
@@ -83,8 +83,9 @@ const Queue = {
});
},
updateJob: async function (jobId, status, result) {
updateJob: async function (jobId = null, status, result) {
try {
if (!jobId) return null;
const updatedJob = await prisma.jobs.update({
where: { id: Number(jobId) },
data: {
+6
View File
@@ -1,6 +1,12 @@
const prisma = require("../utils/prisma");
const RagTest = {
schedules: {
hourly: "hourly",
daily: "daily",
weekly: "weekly",
monthly: "monthly",
},
status: {
running: "running",
failed: "failed",
+193
View File
@@ -0,0 +1,193 @@
const { RagTest } = require('../../../../backend/models/ragTest');
const { InngestClient } = require('../../../utils/inngest');
const runHourlyRagTest = InngestClient.createFunction(
{
id: 'rag-testing-hourly-cron',
name: 'RAG Test for Workspace - Hourly Cron',
},
{ cron: 'TZ=America/Los_Angeles * * * * *' },
async () => {
try {
console.log(`\n\x1b[33m[CRON: Running hourly RAG Test Cron]\x1b[0m`);
const eligibleTests = await RagTest.where(
{
enabled: true,
frequencyType: RagTest.schedules.hourly,
},
null,
null,
{
id: true,
enabled: true,
organization: true,
}
);
if (eligibleTests.length === 0) return;
console.log(
`\x1b[34m[INFO]\x1b[0m ${eligibleTests.length} eligible tests to run. Fanning out workers.`
);
eligibleTests.forEach(async (test) => {
await InngestClient.send({
name: 'rag-test/run',
data: {
jobId: null, // not associated with a job.
organization: test.organization,
testId: test.id,
},
});
});
return;
} catch (e) {
console.error(e.message);
return;
}
}
);
const runDailyRagTest = InngestClient.createFunction(
{
id: 'rag-testing-daily-cron',
name: 'RAG Test for Workspace - Daily Cron',
},
{ cron: 'TZ=America/Los_Angeles 0 18 * * *' },
async () => {
try {
console.log(`\n\x1b[33m[CRON: Running daily RAG Test Cron]\x1b[0m`);
const eligibleTests = await RagTest.where(
{
enabled: true,
frequencyType: RagTest.schedules.daily,
},
null,
null,
{
id: true,
enabled: true,
organization: true,
}
);
if (eligibleTests.length === 0) return;
console.log(
`\x1b[34m[INFO]\x1b[0m ${eligibleTests.length} eligible tests to run. Fanning out workers.`
);
eligibleTests.forEach(async (test) => {
await InngestClient.send({
name: 'rag-test/run',
data: {
jobId: null, // not associated with a job.
organization: test.organization,
testId: test.id,
},
});
});
return;
} catch (e) {
console.error(e.message);
return;
}
}
);
const runWeeklyRagTest = InngestClient.createFunction(
{
id: 'rag-testing-weekly-cron',
name: 'RAG Test for Workspace - Weekly Cron',
},
{ cron: 'TZ=America/Los_Angeles 0 18 * * WED' },
async () => {
try {
console.log(`\n\x1b[33m[CRON: Running weekly RAG Test Cron]\x1b[0m`);
const eligibleTests = await RagTest.where(
{
enabled: true,
frequencyType: RagTest.schedules.weekly,
},
null,
null,
{
id: true,
enabled: true,
organization: true,
}
);
if (eligibleTests.length === 0) return;
console.log(
`\x1b[34m[INFO]\x1b[0m ${eligibleTests.length} eligible tests to run. Fanning out workers.`
);
eligibleTests.forEach(async (test) => {
await InngestClient.send({
name: 'rag-test/run',
data: {
jobId: null, // not associated with a job.
organization: test.organization,
testId: test.id,
},
});
});
return;
} catch (e) {
console.error(e.message);
return;
}
}
);
const runMonthlyRagTest = InngestClient.createFunction(
{
id: 'rag-testing-monthly-cron',
name: 'RAG Test for Workspace - Monthly Cron',
},
{ cron: 'TZ=America/Los_Angeles 0 18 15 * *' },
async () => {
try {
console.log(`\n\x1b[33m[CRON: Running monthly RAG Test Cron]\x1b[0m`);
const eligibleTests = await RagTest.where(
{
enabled: true,
frequencyType: RagTest.schedules.monthly,
},
null,
null,
{
id: true,
enabled: true,
organization: true,
}
);
if (eligibleTests.length === 0) return;
console.log(
`\x1b[34m[INFO]\x1b[0m ${eligibleTests.length} eligible tests to run. Fanning out workers.`
);
eligibleTests.forEach(async (test) => {
await InngestClient.send({
name: 'rag-test/run',
data: {
jobId: null, // not associated with a job.
organization: test.organization,
testId: test.id,
},
});
});
return;
} catch (e) {
console.error(e.message);
return;
}
}
);
module.exports = {
runHourlyRagTest,
runDailyRagTest,
runWeeklyRagTest,
runMonthlyRagTest,
};
+2 -2
View File
@@ -12,13 +12,13 @@ const {
} = require('../../../backend/utils/vectordatabases/providers');
const { InngestClient } = require('../../utils/inngest');
const DELTA_THRESHOLD = 0.3;
const DELTA_THRESHOLD = 0.25;
const runRAGTest = InngestClient.createFunction(
{ name: 'RAG Test for Workspace' },
{ event: 'rag-test/run' },
async ({ event, step: _step, logger }) => {
var result = {};
const { organization, testId, jobId } = event.data;
const { organization, testId, jobId = null } = event.data;
const { run } = await RagTest.createRun(testId, RagTest.status.running);
try {
+7
View File
@@ -36,6 +36,7 @@ const { addWeaviateDocuments } = require("./functions/addWeaviateDocuments");
const { migrateOrganization } = require("./functions/migrateOrganization");
const { resetOrganization } = require("./functions/resetOrganization");
const { runRAGTest } = require("./functions/runRAGTest");
const { runHourlyRagTest, runDailyRagTest, runWeeklyRagTest, runMonthlyRagTest } = require("./functions/cron/ragTesting");
const app = express();
app.use(cors({ origin: true }));
@@ -104,6 +105,12 @@ app.use(
// RAGTesting
runRAGTest,
// Cron Jobs
runHourlyRagTest,
runDailyRagTest,
runWeeklyRagTest,
runMonthlyRagTest,
], { landingPage: true })
);