Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const commandConfig = CommandModule.register();

```javascript
import { Module } from '@nestjs/common';
import { CommandModule } from '@hodfords/nestjs-command';
import { commandConfig } from '~config/command.config';

@Module({
imports: [commandConfig],
Expand All @@ -46,7 +46,7 @@ import { CommandService } from '@hodfords/nestjs-command';
import { commandConfig } from '~config/command.config';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const app = await NestFactory.createApplicationContext(AppModule);
const commandService: CommandService = app.select(commandConfig).get(CommandService, { strict: true });
await commandService.exec();
await app.close();
Expand Down Expand Up @@ -167,6 +167,26 @@ npm run wz-command make-service <file-name> -- --module <module-name>
wz-command make-service <file-name> --module <module-name>
```

### List all scheduled cron jobs

```bash
npm run wz-command list-cron-jobs
```

```bash
wz-command list-cron-jobs
```

### Run specific cron jobs

```bash
npm run wz-command run-cron-jobs -- --jobs <jobName>
```

```bash
wz-command run-cron-jobs --jobs <jobName>
```

## License 📝

This project is licensed under the MIT License
6 changes: 5 additions & 1 deletion lib/command.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { MakeMigrationCommand } from './commands/make-migration.command';
import { MakeModuleCommand } from './commands/make-module.command';
import { MakeRepositoryCommand } from './commands/make-repository.command';
import { MakeServiceCommand } from './commands/make-service.command';
import { ListCronJobsCommand } from 'lib/commands/list-cron-jobs.command';
import { RunCronJobsCommand } from './commands/run-cron-jobs.command';

@Module({})
export class CommandModule {
Expand All @@ -22,7 +24,9 @@ export class CommandModule {
MakeEntityCommand,
MakeControllerCommand,
MakeDtoCommand,
MakeRepositoryCommand
MakeRepositoryCommand,
ListCronJobsCommand,
RunCronJobsCommand
];
if (isEnableTypeorm) {
providers.push(MakeMigrationCommand);
Expand Down
34 changes: 34 additions & 0 deletions lib/commands/list-cron-jobs.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Injectable } from '@nestjs/common';
import { Command } from '../decorators/command.decorator';
import { BaseCommand } from './base.command';
import { SchedulerRegistry } from '@nestjs/schedule';

@Command({
signature: 'list-cron-jobs',
description: 'List all current cron jobs'
})
@Injectable()
export class ListCronJobsCommand extends BaseCommand {
constructor(private readonly schedulerRegistry: SchedulerRegistry) {
super();
}

public handle(): void {
try {
const jobs = this.schedulerRegistry.getCronJobs();

if (!jobs.size) {
console.warn('No cron jobs found.');
return;
}
const jobList = Array.from(jobs.entries()).map(([name, job]) => ({
['Job Name']: name,
['Cron']: job.cronTime.source
}));
console.log('\nCurrent Cron Jobs:\n');
console.table(jobList);
} catch (error) {
console.error('Failed to retrieve cron jobs:', error);
}
}
}
47 changes: 47 additions & 0 deletions lib/commands/run-cron-jobs.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Injectable } from '@nestjs/common';
import { Command } from '../decorators/command.decorator';
import { BaseCommand } from './base.command';
import { SchedulerRegistry } from '@nestjs/schedule';

@Command({
signature: 'run-cron-jobs',
description: 'Run specified cron jobs',
options: [
{
value: '--jobs <jobs...>',
description: 'List of cron jobs to run'
}
]
})
@Injectable()
export class RunCronJobsCommand extends BaseCommand {
constructor(private readonly schedulerRegistry: SchedulerRegistry) {
super();
}

public async handle(): Promise<void> {
const { jobs } = this.program.opts();

if (!jobs || jobs.length === 0) {
console.warn('No cron jobs specified.');
return;
}

console.log(`Executing cron jobs: ${jobs.join(', ')}`);

await Promise.all(
jobs.map(async (jobName: string) => {
const job = this.schedulerRegistry.getCronJob(jobName);
if (!job) {
console.warn(`Cron job "${jobName}" not found.`);
return;
}
console.log(`Executing cron job "${jobName}"...`);
job.waitForCompletion = true;
await job.fireOnTick();
})
);

console.log('Cron job execution completed.');
}
}
Loading