fix(medusa): Batch job not saving the errors properly (#2812)

This commit is contained in:
Adrien de Peretti
2022-12-16 20:13:53 +01:00
committed by GitHub
parent c16522d6ce
commit 5e4decbc1c
6 changed files with 54 additions and 54 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
fix: Batch job not saving the errors properly
@@ -9,7 +9,9 @@ const adminSeeder = require("../../../helpers/admin-seeder")
const batchJobSeeder = require("../../../helpers/batch-job-seeder") const batchJobSeeder = require("../../../helpers/batch-job-seeder")
const userSeeder = require("../../../helpers/user-seeder") const userSeeder = require("../../../helpers/user-seeder")
const { simpleProductFactory } = require("../../../factories") const { simpleProductFactory } = require("../../../factories")
const { simpleProductCollectionFactory } = require("../../../factories/simple-product-collection-factory"); const {
simpleProductCollectionFactory,
} = require("../../../factories/simple-product-collection-factory")
const adminReqConfig = { const adminReqConfig = {
headers: { headers: {
@@ -50,8 +52,8 @@ describe("Product import batch job", () => {
let medusaProcess let medusaProcess
let dbConnection let dbConnection
let collectionHandle1 = "test-collection1" const collectionHandle1 = "test-collection1"
let collectionHandle2 = "test-collection2" const collectionHandle2 = "test-collection2"
beforeAll(async () => { beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
@@ -79,11 +81,14 @@ describe("Product import batch job", () => {
await batchJobSeeder(dbConnection) await batchJobSeeder(dbConnection)
await adminSeeder(dbConnection) await adminSeeder(dbConnection)
await userSeeder(dbConnection) await userSeeder(dbConnection)
await simpleProductCollectionFactory(dbConnection, [{ await simpleProductCollectionFactory(dbConnection, [
handle: collectionHandle1 {
}, { handle: collectionHandle1,
handle: collectionHandle2 },
}]) {
handle: collectionHandle2,
},
])
}) })
afterEach(async () => { afterEach(async () => {
@@ -226,7 +231,7 @@ describe("Product import batch job", () => {
], ],
collection: expect.objectContaining({ collection: expect.objectContaining({
handle: collectionHandle1, handle: collectionHandle1,
}) }),
}), }),
expect.objectContaining({ expect.objectContaining({
title: "Test product", title: "Test product",
@@ -288,7 +293,7 @@ describe("Product import batch job", () => {
tags: [], tags: [],
collection: expect.objectContaining({ collection: expect.objectContaining({
handle: collectionHandle1, handle: collectionHandle1,
}) }),
}), }),
// UPDATED PRODUCT // UPDATED PRODUCT
expect.objectContaining({ expect.objectContaining({
@@ -384,8 +389,8 @@ describe("Product import batch job", () => {
}), }),
], ],
collection: expect.objectContaining({ collection: expect.objectContaining({
handle: collectionHandle2 handle: collectionHandle2,
}) }),
}), }),
]) ])
) )
@@ -69,6 +69,7 @@ export abstract class AbstractBatchJobStrategy
err: unknown, err: unknown,
result: T result: T
): Promise<void> { ): Promise<void> {
// TODO just throw to be handled by the subscriber
return await this.atomicPhase_(async (transactionManager) => { return await this.atomicPhase_(async (transactionManager) => {
const batchJob = (await this.batchJobService_ const batchJob = (await this.batchJobService_
.withTransaction(transactionManager) .withTransaction(transactionManager)
@@ -95,7 +96,7 @@ export abstract class AbstractBatchJobStrategy
}, },
result: { result: {
...result, ...result,
errors: [...existingErrors, resultError], errors: [...existingErrors, resultError.message],
}, },
}) })
} else { } else {
+3 -14
View File
@@ -1,16 +1,5 @@
import { import { AfterLoad, BeforeInsert, Column, Entity, JoinColumn, ManyToOne, } from "typeorm"
AfterLoad, import { BatchJobResultError, BatchJobResultStatDescriptor, BatchJobStatus, } from "../types/batch-job"
BeforeInsert,
Column,
Entity,
JoinColumn,
ManyToOne,
} from "typeorm"
import {
BatchJobResultError,
BatchJobResultStatDescriptor,
BatchJobStatus,
} from "../types/batch-job"
import { DbAwareColumn, resolveDbType } from "../utils/db-aware-column" import { DbAwareColumn, resolveDbType } from "../utils/db-aware-column"
import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity" import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity"
@@ -37,7 +26,7 @@ export class BatchJob extends SoftDeletableEntity {
count?: number count?: number
advancement_count?: number advancement_count?: number
progress?: number progress?: number
errors?: BatchJobResultError[] errors?: (BatchJobResultError | string)[]
stat_descriptors?: BatchJobResultStatDescriptor[] stat_descriptors?: BatchJobResultStatDescriptor[]
file_key?: string file_key?: string
file_size?: number file_size?: number
+2 -1
View File
@@ -96,6 +96,7 @@ class BatchJobService extends TransactionBaseService {
eventBusService, eventBusService,
strategyResolverService, strategyResolverService,
}: InjectedDependencies) { }: InjectedDependencies) {
// eslint-disable-next-line prefer-rest-params
super(arguments[0]) super(arguments[0])
this.manager_ = manager this.manager_ = manager
@@ -339,7 +340,7 @@ class BatchJobService extends TransactionBaseService {
async setFailed( async setFailed(
batchJobOrId: string | BatchJob, batchJobOrId: string | BatchJob,
error?: BatchJobResultError error?: BatchJobResultError | string
): Promise<BatchJob | never> { ): Promise<BatchJob | never> {
return await this.atomicPhase_(async () => { return await this.atomicPhase_(async () => {
let batchJob = batchJobOrId as BatchJob let batchJob = batchJobOrId as BatchJob
+25 -26
View File
@@ -33,45 +33,44 @@ class BatchJobSubscriber {
} }
preProcessBatchJob = async (data): Promise<void> => { preProcessBatchJob = async (data): Promise<void> => {
await this.manager_.transaction(async (manager) => { try {
const batchJobServiceTx = this.batchJobService_.withTransaction(manager) await this.manager_.transaction(async (manager) => {
const batchJob = await batchJobServiceTx.retrieve(data.id) const batchJobServiceTx = this.batchJobService_.withTransaction(manager)
const batchJob = await batchJobServiceTx.retrieve(data.id)
const batchJobStrategy = this.strategyResolver_.resolveBatchJobByType( const batchJobStrategy = this.strategyResolver_.resolveBatchJobByType(
batchJob.type batchJob.type
) )
try {
await batchJobStrategy await batchJobStrategy
.withTransaction(manager) .withTransaction(manager)
.preProcessBatchJob(batchJob.id) .preProcessBatchJob(batchJob.id)
await batchJobServiceTx.setPreProcessingDone(batchJob.id) await batchJobServiceTx.setPreProcessingDone(batchJob.id)
} catch (e) { })
await this.batchJobService_.setFailed(batchJob.id, e.message) } catch (e) {
throw e await this.batchJobService_.setFailed(data.id, e.message)
} throw e
}) }
} }
processBatchJob = async (data): Promise<void> => { processBatchJob = async (data): Promise<void> => {
await this.manager_.transaction(async (manager) => { try {
const batchJobServiceTx = this.batchJobService_.withTransaction(manager) await this.manager_.transaction(async (manager) => {
const batchJob = await batchJobServiceTx.retrieve(data.id) const batchJobServiceTx = this.batchJobService_.withTransaction(manager)
const batchJob = await batchJobServiceTx.retrieve(data.id)
const batchJobStrategy = this.strategyResolver_.resolveBatchJobByType( const batchJobStrategy = this.strategyResolver_.resolveBatchJobByType(
batchJob.type batchJob.type
) )
await batchJobServiceTx.setProcessing(batchJob.id) await batchJobServiceTx.setProcessing(batchJob.id)
try {
await batchJobStrategy.withTransaction(manager).processJob(batchJob.id) await batchJobStrategy.withTransaction(manager).processJob(batchJob.id)
await batchJobServiceTx.complete(batchJob.id) await batchJobServiceTx.complete(batchJob.id)
} catch (e) { })
await this.batchJobService_.setFailed(batchJob.id, e.message) } catch (e) {
throw e await this.batchJobService_.setFailed(data.id, e.message)
} throw e
}) }
} }
} }