Skip to content

Commit

Permalink
ability to turn off and change cron interval via env var
Browse files Browse the repository at this point in the history
  • Loading branch information
webdevcody committed Oct 16, 2024
1 parent cb073ba commit 14e0af7
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ If you want to mock all models - in convex, please add the following environment

- `npx convex env set MOCK_MODELS true`
- `npx convex env set FLAG_TEST_PAGE true`
- `npx convex env set FLAG_CRON_JOBS true`

Add optional environment variable/s for simulating real AI models without mockup responses(when mockup flags are set to FALSE):

Expand Down
4 changes: 4 additions & 0 deletions convex/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ export const AI_MODEL_IDS = AI_MODELS.map((model) => model.model);
export const PLAY_DELAY = process.env.PLAY_DELAY
? parseInt(process.env.PLAY_DELAY)
: 0;

export const CRON_INTERVAL = process.env.CRON_INTERVAL
? parseInt(process.env.CRON_INTERVAL)
: 60;
3 changes: 2 additions & 1 deletion convex/crons.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { cronJobs } from "convex/server";
import { internal } from "./_generated/api";
import { CRON_INTERVAL } from "./constants";

const crons = cronJobs();

crons.interval(
"run games for all active models",
{ minutes: 60 },
{ minutes: CRON_INTERVAL },
internal.models.runActiveModelsGames,
);

Expand Down
1 change: 1 addition & 0 deletions convex/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const getFlags = query({
handler: async (ctx) => {
return {
showTestPage: process.env.FLAG_TEST_PAGE === "true",
enableCronJobs: process.env.FLAG_CRON_JOBS === "true",
};
},
});
5 changes: 5 additions & 0 deletions convex/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { internalMutation, query } from "./_generated/server";

export const runActiveModelsGames = internalMutation({
handler: async (ctx) => {
const flags = await ctx.runQuery(api.flags.getFlags);
if (!flags.enableCronJobs) {
return;
}

const models = await ctx.runQuery(api.models.getActiveModels);

await Promise.all(
Expand Down

0 comments on commit 14e0af7

Please sign in to comment.