@hiennc24/import-job
v1.0.0
Published
Reusable async import-job lifecycle: entity, repository, service and Moleculer mixin with AMQP progress publishing
Maintainers
Readme
@hiennc24/import-job
Reusable async import-job infrastructure for Moleculer services: persisted job lifecycle
(pending → processing → completed | completed_with_errors | failed), atomic progress counters,
throttled AMQP progress publishing, and query actions via a service mixin.
Each host service owns its own import_jobs collection (tenant DB routed by meta.domain
through @hiennc24/mongoose).
Usage
import {
ImportJobService,
ImportJobRepository,
createImportJobMixin
} from '@hiennc24/import-job';
import SERVICE_BROKER from '@hiennc24/constant';
const importJobService = new ImportJobService({
publisher: broker, // Moleculer broker with @moleculer/channels middleware
progressChannel: SERVICE_BROKER.CHANNEL_NAMES.SHARED.IMPORT_JOB_PROGRESS
});
broker.createService({
name: 'svc-organization.employees',
mixins: [
createImportJobMixin({
service: importJobService,
actionNames: {
getImportJob: SERVICE_BROKER.SVC_ORGANIZATION_EMPLOYEES.ACTION_GET_IMPORT_JOB,
getImportJobPage: SERVICE_BROKER.SVC_ORGANIZATION_EMPLOYEES.ACTION_GET_IMPORT_JOB_PAGE
}
})
]
});Worker side:
const job = await importJobService.create({ type: 'employee', mode, fileId, createdBy, domain }, meta);
// ... consumer:
await importJobService.start(job._id, totalRows, meta);
await importJobService.updateProgress(job._id, { processedDelta: 500, successDelta: 480, errorDelta: 20 }, meta);
await importJobService.complete(job._id, { errorReportFileId }, meta);Guarantees
- One active job per
{domain, type}— enforced by a partial unique index (active: true);createthrowsImportJobActiveError(code 409). - Throttled progress — non-terminal publishes at most once per
throttleMs(default 1s) per job; terminal transitions always publish. - Publish failures never break lifecycle transitions — Mongo state is the source of truth.
Scripts
npm run build— tsc tolib/npm test— mocha unit tests
