-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from webdevcody/feat-scheduler
Scheduler/Models table
- Loading branch information
Showing
8 changed files
with
149 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { cronJobs } from "convex/server"; | ||
import { internal } from "./_generated/api"; | ||
|
||
const crons = cronJobs(); | ||
|
||
crons.interval( | ||
"run games for all active models", | ||
{ minutes: 5 }, | ||
internal.models.runActiveModelsGames, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
import { internal } from "./_generated/api"; | ||
import { internalMutation } from "./_generated/server"; | ||
import { seedMaps } from "./maps"; | ||
|
||
export default internalMutation({ | ||
handler: async (ctx) => { | ||
const maps = await ctx.db.query("maps").first(); | ||
if (maps) return; | ||
await seedMaps(ctx, {}); | ||
await ctx.runMutation(internal.maps.seedMaps); | ||
await ctx.runMutation(internal.models.seedModels); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { AI_MODELS } from "./constants"; | ||
import { api } from "./_generated/api"; | ||
import { internalMutation, query } from "./_generated/server"; | ||
|
||
export const runActiveModelsGames = internalMutation({ | ||
handler: async (ctx) => { | ||
const models = await ctx.runQuery(api.models.getActiveModels); | ||
|
||
await Promise.all( | ||
models.map((model) => | ||
ctx.runMutation(api.games.startNewGame, { modelId: model.slug }), | ||
), | ||
); | ||
}, | ||
}); | ||
|
||
export const seedModels = internalMutation({ | ||
handler: async (ctx) => { | ||
const models = await ctx.db.query("models").collect(); | ||
const promises = []; | ||
|
||
for (const model of AI_MODELS) { | ||
const existingModel = models.find((it) => it.slug === model.model); | ||
|
||
if (existingModel !== undefined) { | ||
continue; | ||
} | ||
|
||
promises.push( | ||
ctx.db.insert("models", { | ||
slug: model.model, | ||
name: model.name, | ||
active: true, | ||
}), | ||
); | ||
} | ||
|
||
await Promise.all(promises); | ||
}, | ||
}); | ||
|
||
export const getActiveModels = query({ | ||
handler: async (ctx) => { | ||
return await ctx.db | ||
.query("models") | ||
.withIndex("by_active", (q) => q.eq("active", true)) | ||
.collect(); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters