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

View File

@@ -69,6 +69,7 @@ export abstract class AbstractBatchJobStrategy
err: unknown,
result: T
): Promise<void> {
// TODO just throw to be handled by the subscriber
return await this.atomicPhase_(async (transactionManager) => {
const batchJob = (await this.batchJobService_
.withTransaction(transactionManager)
@@ -95,7 +96,7 @@ export abstract class AbstractBatchJobStrategy
},
result: {
...result,
errors: [...existingErrors, resultError],
errors: [...existingErrors, resultError.message],
},
})
} else {

View File

@@ -1,16 +1,5 @@
import {
AfterLoad,
BeforeInsert,
Column,
Entity,
JoinColumn,
ManyToOne,
} from "typeorm"
import {
BatchJobResultError,
BatchJobResultStatDescriptor,
BatchJobStatus,
} from "../types/batch-job"
import { AfterLoad, BeforeInsert, Column, Entity, JoinColumn, ManyToOne, } from "typeorm"
import { BatchJobResultError, BatchJobResultStatDescriptor, BatchJobStatus, } from "../types/batch-job"
import { DbAwareColumn, resolveDbType } from "../utils/db-aware-column"
import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity"
@@ -37,7 +26,7 @@ export class BatchJob extends SoftDeletableEntity {
count?: number
advancement_count?: number
progress?: number
errors?: BatchJobResultError[]
errors?: (BatchJobResultError | string)[]
stat_descriptors?: BatchJobResultStatDescriptor[]
file_key?: string
file_size?: number

View File

@@ -96,6 +96,7 @@ class BatchJobService extends TransactionBaseService {
eventBusService,
strategyResolverService,
}: InjectedDependencies) {
// eslint-disable-next-line prefer-rest-params
super(arguments[0])
this.manager_ = manager
@@ -339,7 +340,7 @@ class BatchJobService extends TransactionBaseService {
async setFailed(
batchJobOrId: string | BatchJob,
error?: BatchJobResultError
error?: BatchJobResultError | string
): Promise<BatchJob | never> {
return await this.atomicPhase_(async () => {
let batchJob = batchJobOrId as BatchJob

View File

@@ -33,45 +33,44 @@ class BatchJobSubscriber {
}
preProcessBatchJob = async (data): Promise<void> => {
await this.manager_.transaction(async (manager) => {
const batchJobServiceTx = this.batchJobService_.withTransaction(manager)
const batchJob = await batchJobServiceTx.retrieve(data.id)
try {
await this.manager_.transaction(async (manager) => {
const batchJobServiceTx = this.batchJobService_.withTransaction(manager)
const batchJob = await batchJobServiceTx.retrieve(data.id)
const batchJobStrategy = this.strategyResolver_.resolveBatchJobByType(
batchJob.type
)
const batchJobStrategy = this.strategyResolver_.resolveBatchJobByType(
batchJob.type
)
try {
await batchJobStrategy
.withTransaction(manager)
.preProcessBatchJob(batchJob.id)
await batchJobServiceTx.setPreProcessingDone(batchJob.id)
} catch (e) {
await this.batchJobService_.setFailed(batchJob.id, e.message)
throw e
}
})
})
} catch (e) {
await this.batchJobService_.setFailed(data.id, e.message)
throw e
}
}
processBatchJob = async (data): Promise<void> => {
await this.manager_.transaction(async (manager) => {
const batchJobServiceTx = this.batchJobService_.withTransaction(manager)
const batchJob = await batchJobServiceTx.retrieve(data.id)
try {
await this.manager_.transaction(async (manager) => {
const batchJobServiceTx = this.batchJobService_.withTransaction(manager)
const batchJob = await batchJobServiceTx.retrieve(data.id)
const batchJobStrategy = this.strategyResolver_.resolveBatchJobByType(
batchJob.type
)
const batchJobStrategy = this.strategyResolver_.resolveBatchJobByType(
batchJob.type
)
await batchJobServiceTx.setProcessing(batchJob.id)
try {
await batchJobServiceTx.setProcessing(batchJob.id)
await batchJobStrategy.withTransaction(manager).processJob(batchJob.id)
await batchJobServiceTx.complete(batchJob.id)
} catch (e) {
await this.batchJobService_.setFailed(batchJob.id, e.message)
throw e
}
})
})
} catch (e) {
await this.batchJobService_.setFailed(data.id, e.message)
throw e
}
}
}