Add table for storing jobs in db
This commit is contained in:
parent
2bd659a1b4
commit
f6a98e0e7e
7 changed files with 47 additions and 4 deletions
|
@ -20,7 +20,7 @@ model AlgoliaConfiguration {
|
|||
|
||||
model IndexJob {
|
||||
id Int @id @default(autoincrement())
|
||||
jobId Int @unique
|
||||
jobId Int @unique // todo make it ID instead?
|
||||
createdAt DateTime @default(now())
|
||||
createdBy String
|
||||
status String // probably enum
|
||||
|
|
|
@ -14,12 +14,19 @@ export class IndexingJobRepository {
|
|||
});
|
||||
}
|
||||
|
||||
getJobs(saleorApiUrl: string) {
|
||||
return this.prisma.indexJob.findMany({
|
||||
where: {
|
||||
ownerSaleor: saleorApiUrl,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
createPendingJob(
|
||||
saleorApiUrl: string,
|
||||
job: {
|
||||
jobId: number;
|
||||
createdByEmail: string;
|
||||
secretKey: string;
|
||||
}
|
||||
) {
|
||||
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);
|
||||
|
|
|
@ -4,6 +4,9 @@ export const register = async () => {
|
|||
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");
|
||||
|
||||
/**
|
||||
* Next does not refresh this file, so it will be hard to develop
|
||||
*/
|
||||
await import("./worker/runner").catch();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { createProtectedHandler } from "@saleor/app-sdk/handlers/next";
|
||||
import { saleorApp } from "../../../saleor-app";
|
||||
import { runIndexSaleorProducts } from "../../worker/index-saleor-products/index-saleor-products";
|
||||
import { indexingJobRepository } from "../../domain/indexing-job/IndexingJobRepository";
|
||||
|
||||
export default createProtectedHandler(
|
||||
async (req, res, ctx) => {
|
||||
|
@ -11,6 +12,11 @@ export default createProtectedHandler(
|
|||
console.log("Added job");
|
||||
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();
|
||||
},
|
||||
saleorApp.apl,
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
import { createProtectedHandler } from "@saleor/app-sdk/handlers/next";
|
||||
import { saleorApp } from "../../../saleor-app";
|
||||
import { indexingJobRepository } from "../../domain/indexing-job/IndexingJobRepository";
|
||||
|
||||
export default createProtectedHandler(
|
||||
async (req, res) => {
|
||||
// todo https://github.com/graphile/worker/issues/330
|
||||
async (req, res, ctx) => {
|
||||
const jobs = await indexingJobRepository.getJobs(ctx.authData.saleorApiUrl);
|
||||
|
||||
return res.json(jobs);
|
||||
},
|
||||
saleorApp.apl,
|
||||
["MANAGE_APPS"]
|
||||
|
|
|
@ -2,6 +2,7 @@ import { Task } from "graphile-worker/dist/interfaces";
|
|||
import { z } from "zod";
|
||||
import { getProductsAndSendToAlgolia } from "./get-products-and-send-to-algolia";
|
||||
import { getWorkerUtils } from "../worker-utils";
|
||||
import { indexingJobRepository } from "../../domain/indexing-job/IndexingJobRepository";
|
||||
|
||||
const payloadSchema = z.object({
|
||||
saleorApiUrl: z.string().url(),
|
||||
|
@ -22,6 +23,12 @@ export const IndexSaleorProducts: Task = async (payload, helpers) => {
|
|||
* Perform some business logic
|
||||
*/
|
||||
await getProductsAndSendToAlgolia(typedPayload.saleorApiUrl);
|
||||
|
||||
await indexingJobRepository.updateJobStatus(
|
||||
typedPayload.saleorApiUrl,
|
||||
Number(helpers.job.id),
|
||||
"SUCCESS"
|
||||
);
|
||||
};
|
||||
|
||||
export const IndexSaleorProductsJobName = "IndexSaleorProducts";
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue