Add table for storing jobs in db

This commit is contained in:
Lukasz Ostrowski 2023-06-07 13:51:26 +02:00
parent 2bd659a1b4
commit f6a98e0e7e
7 changed files with 47 additions and 4 deletions

View file

@ -20,7 +20,7 @@ model AlgoliaConfiguration {
model IndexJob { model IndexJob {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
jobId Int @unique jobId Int @unique // todo make it ID instead?
createdAt DateTime @default(now()) createdAt DateTime @default(now())
createdBy String createdBy String
status String // probably enum status String // probably enum

View file

@ -14,12 +14,19 @@ export class IndexingJobRepository {
}); });
} }
getJobs(saleorApiUrl: string) {
return this.prisma.indexJob.findMany({
where: {
ownerSaleor: saleorApiUrl,
},
});
}
createPendingJob( createPendingJob(
saleorApiUrl: string, saleorApiUrl: string,
job: { job: {
jobId: number; jobId: number;
createdByEmail: string; createdByEmail: string;
secretKey: string;
} }
) { ) {
return this.prisma.indexJob.create({ return this.prisma.indexJob.create({
@ -31,6 +38,18 @@ export class IndexingJobRepository {
}, },
}); });
} }
// todo should repository verify saleorApiUrl for protection?
updateJobStatus(saleorApiUrl: string, jobId: number, status: "ERROR" | "SUCCESS") {
return this.prisma.indexJob.update({
where: {
jobId: jobId,
},
data: {
status,
},
});
}
} }
export const indexingJobRepository = new IndexingJobRepository(prisma); export const indexingJobRepository = new IndexingJobRepository(prisma);

View file

@ -4,6 +4,9 @@ export const register = async () => {
if (process.env.RUN_WORKER_IN_NEXT_PROCESS === "true" && process.env.NEXT_RUNTIME === "nodejs") { if (process.env.RUN_WORKER_IN_NEXT_PROCESS === "true" && process.env.NEXT_RUNTIME === "nodejs") {
console.log("RUN_WORKER_IN_NEXT_PROCESS env is set, will inject worker to Next.js process"); console.log("RUN_WORKER_IN_NEXT_PROCESS env is set, will inject worker to Next.js process");
/**
* Next does not refresh this file, so it will be hard to develop
*/
await import("./worker/runner").catch(); await import("./worker/runner").catch();
} }

View file

@ -1,6 +1,7 @@
import { createProtectedHandler } from "@saleor/app-sdk/handlers/next"; import { createProtectedHandler } from "@saleor/app-sdk/handlers/next";
import { saleorApp } from "../../../saleor-app"; import { saleorApp } from "../../../saleor-app";
import { runIndexSaleorProducts } from "../../worker/index-saleor-products/index-saleor-products"; import { runIndexSaleorProducts } from "../../worker/index-saleor-products/index-saleor-products";
import { indexingJobRepository } from "../../domain/indexing-job/IndexingJobRepository";
export default createProtectedHandler( export default createProtectedHandler(
async (req, res, ctx) => { async (req, res, ctx) => {
@ -11,6 +12,11 @@ export default createProtectedHandler(
console.log("Added job"); console.log("Added job");
console.log(job.id); console.log(job.id);
await indexingJobRepository.createPendingJob(ctx.authData.saleorApiUrl, {
jobId: Number(job.id),
createdByEmail: "some-name-todo", // todo add to sdk
});
return res.status(200).end(); return res.status(200).end();
}, },
saleorApp.apl, saleorApp.apl,

View file

@ -1,9 +1,12 @@
import { createProtectedHandler } from "@saleor/app-sdk/handlers/next"; import { createProtectedHandler } from "@saleor/app-sdk/handlers/next";
import { saleorApp } from "../../../saleor-app"; import { saleorApp } from "../../../saleor-app";
import { indexingJobRepository } from "../../domain/indexing-job/IndexingJobRepository";
export default createProtectedHandler( export default createProtectedHandler(
async (req, res) => { async (req, res, ctx) => {
// todo https://github.com/graphile/worker/issues/330 const jobs = await indexingJobRepository.getJobs(ctx.authData.saleorApiUrl);
return res.json(jobs);
}, },
saleorApp.apl, saleorApp.apl,
["MANAGE_APPS"] ["MANAGE_APPS"]

View file

@ -2,6 +2,7 @@ import { Task } from "graphile-worker/dist/interfaces";
import { z } from "zod"; import { z } from "zod";
import { getProductsAndSendToAlgolia } from "./get-products-and-send-to-algolia"; import { getProductsAndSendToAlgolia } from "./get-products-and-send-to-algolia";
import { getWorkerUtils } from "../worker-utils"; import { getWorkerUtils } from "../worker-utils";
import { indexingJobRepository } from "../../domain/indexing-job/IndexingJobRepository";
const payloadSchema = z.object({ const payloadSchema = z.object({
saleorApiUrl: z.string().url(), saleorApiUrl: z.string().url(),
@ -22,6 +23,12 @@ export const IndexSaleorProducts: Task = async (payload, helpers) => {
* Perform some business logic * Perform some business logic
*/ */
await getProductsAndSendToAlgolia(typedPayload.saleorApiUrl); await getProductsAndSendToAlgolia(typedPayload.saleorApiUrl);
await indexingJobRepository.updateJobStatus(
typedPayload.saleorApiUrl,
Number(helpers.job.id),
"SUCCESS"
);
}; };
export const IndexSaleorProductsJobName = "IndexSaleorProducts"; export const IndexSaleorProductsJobName = "IndexSaleorProducts";

View file

@ -21,6 +21,11 @@ async function main() {
}, },
}); });
runner.events.on("job:error", ({ job, error, worker }) => {
// todo try to rub prisma here
console.log(job);
});
await runner.promise; await runner.promise;
} }