From 3f27b4470b84c0046ad118b1b467d4c7d82689b2 Mon Sep 17 00:00:00 2001 From: pinks Date: Mon, 18 Sep 2023 01:01:09 +0200 Subject: [PATCH] chore: update to ikv 0.3 --- bot/img2imgCommand.ts | 2 +- bot/queueCommand.ts | 4 ++-- bot/txt2imgCommand.ts | 2 +- db/jobStore.ts | 11 ++++++++--- deps.ts | 2 +- tasks/processJobs.ts | 2 +- tasks/returnHangedJobs.ts | 2 +- tasks/updateJobStatusMsgs.ts | 2 +- 8 files changed, 16 insertions(+), 11 deletions(-) diff --git a/bot/img2imgCommand.ts b/bot/img2imgCommand.ts index d3ef9c6..4632f4b 100644 --- a/bot/img2imgCommand.ts +++ b/bot/img2imgCommand.ts @@ -32,7 +32,7 @@ async function img2img( return; } - const jobs = await jobStore.getBy("status.type", "waiting"); + const jobs = await jobStore.getBy("status.type", { value: "waiting" }); if (jobs.length >= ctx.session.global.maxJobs) { await ctx.reply( `The queue is full. Try again later. (Max queue size: ${ctx.session.global.maxJobs})`, diff --git a/bot/queueCommand.ts b/bot/queueCommand.ts index 09d7648..d7a733b 100644 --- a/bot/queueCommand.ts +++ b/bot/queueCommand.ts @@ -11,9 +11,9 @@ export async function queueCommand(ctx: Grammy.CommandContext) { handleFutureUpdates().catch((err) => logger().warning(`Updating queue message failed: ${err}`)); async function getMessageText() { - const processingJobs = await jobStore.getBy("status.type", "processing") + const processingJobs = await jobStore.getBy("status.type", { value: "processing" }) .then((jobs) => jobs.map((job) => ({ ...job.value, place: 0 }))); - const waitingJobs = await jobStore.getBy("status.type", "waiting") + const waitingJobs = await jobStore.getBy("status.type", { value: "waiting" }) .then((jobs) => jobs.map((job, index) => ({ ...job.value, place: index + 1 }))); const jobs = [...processingJobs, ...waitingJobs]; const { bold } = GrammyParseMode; diff --git a/bot/txt2imgCommand.ts b/bot/txt2imgCommand.ts index 12db715..c0fd9b1 100644 --- a/bot/txt2imgCommand.ts +++ b/bot/txt2imgCommand.ts @@ -27,7 +27,7 @@ async function txt2img(ctx: Context, match: string, includeRepliedTo: boolean): return; } - const jobs = await jobStore.getBy("status.type", "waiting"); + const jobs = await jobStore.getBy("status.type", { value: "waiting" }); if (jobs.length >= ctx.session.global.maxJobs) { await ctx.reply( `The queue is full. Try again later. (Max queue size: ${ctx.session.global.maxJobs})`, diff --git a/db/jobStore.ts b/db/jobStore.ts index 25c70c2..acc986d 100644 --- a/db/jobStore.ts +++ b/db/jobStore.ts @@ -38,7 +38,12 @@ export interface JobSchema { }; } -export const jobStore = new IKV.Store(db, "job", { - schema: new IKV.Schema(), - indices: ["status.type"], +type JobIndices = { + "status.type": JobSchema["status"]["type"]; +}; + +export const jobStore = new IKV.Store(db, "job", { + indices: { + "status.type": { getValue: (job) => job.status.type }, + }, }); diff --git a/deps.ts b/deps.ts index 9d45cfe..d581cee 100644 --- a/deps.ts +++ b/deps.ts @@ -5,7 +5,7 @@ export * as Collections from "https://deno.land/std@0.201.0/collections/mod.ts"; export * as Base64 from "https://deno.land/std@0.201.0/encoding/base64.ts"; export * as AsyncX from "https://deno.land/x/async@v2.0.2/mod.ts"; export * as ULID from "https://deno.land/x/ulid@v0.3.0/mod.ts"; -export * as IKV from "https://deno.land/x/indexed_kv@v0.2.0/mod.ts"; +export * as IKV from "https://deno.land/x/indexed_kv@v0.3.0/mod.ts"; export * as Grammy from "https://deno.land/x/grammy@v1.18.1/mod.ts"; export * as GrammyTypes from "https://deno.land/x/grammy_types@v3.2.0/mod.ts"; export * as GrammyAutoQuote from "https://deno.land/x/grammy_autoquote@v1.1.2/mod.ts"; diff --git a/tasks/processJobs.ts b/tasks/processJobs.ts index 78ed6b0..2f7edd9 100644 --- a/tasks/processJobs.ts +++ b/tasks/processJobs.ts @@ -33,7 +33,7 @@ export async function processJobs(): Promise { await new Promise((resolve) => setTimeout(resolve, 1000)); try { - const jobs = await jobStore.getBy("status.type", "waiting"); + const jobs = await jobStore.getBy("status.type", { value: "waiting" }); // get first waiting job which hasn't errored in last minute const job = jobs.find((job) => job.value.status.type === "waiting" && diff --git a/tasks/returnHangedJobs.ts b/tasks/returnHangedJobs.ts index 673f60b..635204f 100644 --- a/tasks/returnHangedJobs.ts +++ b/tasks/returnHangedJobs.ts @@ -11,7 +11,7 @@ export async function returnHangedJobs(): Promise { while (true) { try { await new Promise((resolve) => setTimeout(resolve, 5000)); - const jobs = await jobStore.getBy("status.type", "processing"); + const jobs = await jobStore.getBy("status.type", { value: "processing" }); for (const job of jobs) { if (job.value.status.type !== "processing") continue; // if job wasn't updated for 2 minutes, return it to the queue diff --git a/tasks/updateJobStatusMsgs.ts b/tasks/updateJobStatusMsgs.ts index fa2bb51..2935acc 100644 --- a/tasks/updateJobStatusMsgs.ts +++ b/tasks/updateJobStatusMsgs.ts @@ -12,7 +12,7 @@ export async function updateJobStatusMsgs(): Promise { while (true) { try { await new Promise((resolve) => setTimeout(resolve, 5000)); - const jobs = await jobStore.getBy("status.type", "waiting"); + const jobs = await jobStore.getBy("status.type", { value: "waiting" }); for (const [index, job] of jobs.entries()) { if (job.value.status.type !== "waiting" || !job.value.status.message) continue; await bot.api.editMessageText(