This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
export async function processNextInventoryExportJob({ jobRepository, exportInventory }) {
|
||||
const claimed = await jobRepository.claimNext();
|
||||
if (!claimed) return null;
|
||||
|
||||
try {
|
||||
const output = await exportInventory(claimed);
|
||||
await jobRepository.complete(claimed.id, output);
|
||||
return { ...claimed, status: 'COMPLETED', output };
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Inventory export failed.';
|
||||
await jobRepository.fail(claimed.id, message);
|
||||
return { ...claimed, status: 'FAILED', error: message };
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,11 @@ import { createPricingService } from "../../api/src/modules/pricing/services/pri
|
||||
import { createPricingProviderRegistry } from "../../api/src/modules/pricing/providers/pricing-provider.js";
|
||||
import { createFixturePricingProvider } from "../../api/src/modules/pricing/providers/fixture-pricing-provider.js";
|
||||
import { createHttpPricingProvider } from "../../api/src/modules/pricing/providers/http-pricing-provider.js";
|
||||
import { createInventoryItemRepository } from "../../api/src/modules/inventory/repositories/inventory-item-repository.js";
|
||||
import { createInventoryItemService } from "../../api/src/modules/inventory/services/inventory-item-service.js";
|
||||
import { createInventoryExportJobRepository } from "../../api/src/modules/exports/repositories/inventory-export-job-repository.js";
|
||||
import { createAdminExportService } from "../../api/src/modules/exports/services/admin-export-service.js";
|
||||
import { processNextInventoryExportJob } from "./inventory-export-worker.js";
|
||||
import { processNextPersistentImageDerivativeJob } from "./image-derivative-worker.js";
|
||||
import {
|
||||
processNextPersistentPricingRefreshJob,
|
||||
@@ -50,6 +55,13 @@ async function runWorker() {
|
||||
const pricingRefreshJobRepository = createPricingRefreshJobRepository(
|
||||
database.db,
|
||||
);
|
||||
const inventoryExportJobRepository = createInventoryExportJobRepository(database.db);
|
||||
const adminExportService = createAdminExportService({
|
||||
inventoryExportJobRepository,
|
||||
inventoryItemService: createInventoryItemService({
|
||||
inventoryItemRepository: createInventoryItemRepository(database.db),
|
||||
}),
|
||||
});
|
||||
const providers = [];
|
||||
if (process.env.PRICING_FIXTURE_PROVIDER_ENABLED === "true")
|
||||
providers.push(createFixturePricingProvider());
|
||||
@@ -82,6 +94,13 @@ async function runWorker() {
|
||||
}
|
||||
|
||||
async function processOne() {
|
||||
const exportResult = await processNextInventoryExportJob({
|
||||
jobRepository: inventoryExportJobRepository,
|
||||
exportInventory: (job) => adminExportService.inventoryJson({ sellerId: job.sellerId }),
|
||||
});
|
||||
if (exportResult)
|
||||
console.log(`Inventory export job ${exportResult.id} ${exportResult.status.toLowerCase()}.`);
|
||||
|
||||
const result = await processNextPersistentImageDerivativeJob({
|
||||
jobRepository,
|
||||
async loadSource(sourceKey) {
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
|
||||
import { processNextInventoryExportJob } from '../src/inventory-export-worker.js';
|
||||
|
||||
test('completes a claimed inventory export job', async () => {
|
||||
const calls = [];
|
||||
const result = await processNextInventoryExportJob({
|
||||
jobRepository: {
|
||||
async claimNext() { return { id: 'job-1', status: 'PROCESSING' }; },
|
||||
async complete(id, output) { calls.push(['complete', id, output]); },
|
||||
},
|
||||
async exportInventory() { return [{ id: 'item-1' }]; },
|
||||
});
|
||||
|
||||
assert.equal(result.status, 'COMPLETED');
|
||||
assert.deepEqual(calls, [['complete', 'job-1', [{ id: 'item-1' }]]]);
|
||||
});
|
||||
|
||||
test('fails an inventory export job when generation errors', async () => {
|
||||
const calls = [];
|
||||
const result = await processNextInventoryExportJob({
|
||||
jobRepository: {
|
||||
async claimNext() { return { id: 'job-1', status: 'PROCESSING' }; },
|
||||
async fail(id, error) { calls.push(['fail', id, error]); },
|
||||
},
|
||||
async exportInventory() { throw new Error('export unavailable'); },
|
||||
});
|
||||
|
||||
assert.equal(result.status, 'FAILED');
|
||||
assert.deepEqual(calls, [['fail', 'job-1', 'export unavailable']]);
|
||||
});
|
||||
Reference in New Issue
Block a user