A simple batch processing module for Nest, inspired by Spring Batch.
Example usage:
// Import the Batch module into your module
@Module({
imports: [
NestBatchModule.register({
chunkSize: 2,
maxRetries: 3,
retryDelay: 3000,
}),
]
})
// The Batch module exports JobLauncher and JobFactory providers
constructor(
private readonly jobLauncher: JobLauncher,
private readonly jobFactory: JobFactory,
private readonly githubApiService: GithubApiService,
private readonly mongoRepository: MongoJobRepository,
) {}
@Cron(CronExpression.EVERY_MINUTE)
async runBatch() {
// Create the batch job instance
const job = this.jobFactory
.jobBuilder('Github-api-bacth-job')
.listeners([new GithubJobListener()])
.repository(this.mongoRepository)
.addStep({
reader: new GithubApiReader(this.githubApiService),
processor: new GithubApiProcessor(),
writer: new GithubApiWriter(),
listeners: [new GithubStepListener()],
name: 'Github-repo-processing-step',
})
.build();
// run batch job
const jobExecution = await this.jobLauncher.run(job);
this.logger.log(`Job Execution Id: ${jobExecution.getId()}`);
}
For more detailed usage please refer to the sample code under apps/github-api