Skip to content

Commit cfe2687

Browse files
Merge pull request #13 from hodfords-solutions/vendor/upgrade-nestjs-11
vendor: upgrade nestjs version to 11
2 parents b51b176 + c397b4c commit cfe2687

10 files changed

+2183
-1616
lines changed

README.md

+22-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const commandConfig = CommandModule.register();
2828

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

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

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

170+
### List all scheduled cron jobs
171+
172+
```bash
173+
npm run wz-command list-cron-jobs
174+
```
175+
176+
```bash
177+
wz-command list-cron-jobs
178+
```
179+
180+
### Run specific cron jobs
181+
182+
```bash
183+
npm run wz-command run-cron-jobs -- --jobs <jobName>
184+
```
185+
186+
```bash
187+
wz-command run-cron-jobs --jobs <jobName>
188+
```
189+
170190
## License 📝
171191

172192
This project is licensed under the MIT License

lib/command.module.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { MakeMigrationCommand } from './commands/make-migration.command';
99
import { MakeModuleCommand } from './commands/make-module.command';
1010
import { MakeRepositoryCommand } from './commands/make-repository.command';
1111
import { MakeServiceCommand } from './commands/make-service.command';
12+
import { ListCronJobsCommand } from 'lib/commands/list-cron-jobs.command';
13+
import { RunCronJobsCommand } from './commands/run-cron-jobs.command';
1214

1315
@Module({})
1416
export class CommandModule {
@@ -22,7 +24,9 @@ export class CommandModule {
2224
MakeEntityCommand,
2325
MakeControllerCommand,
2426
MakeDtoCommand,
25-
MakeRepositoryCommand
27+
MakeRepositoryCommand,
28+
ListCronJobsCommand,
29+
RunCronJobsCommand
2630
];
2731
if (isEnableTypeorm) {
2832
providers.push(MakeMigrationCommand);
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { Command } from '../decorators/command.decorator';
3+
import { BaseCommand } from './base.command';
4+
import { SchedulerRegistry } from '@nestjs/schedule';
5+
6+
@Command({
7+
signature: 'list-cron-jobs',
8+
description: 'List all current cron jobs'
9+
})
10+
@Injectable()
11+
export class ListCronJobsCommand extends BaseCommand {
12+
constructor(private readonly schedulerRegistry: SchedulerRegistry) {
13+
super();
14+
}
15+
16+
public handle(): void {
17+
try {
18+
const jobs = this.schedulerRegistry.getCronJobs();
19+
20+
if (!jobs.size) {
21+
console.warn('No cron jobs found.');
22+
return;
23+
}
24+
const jobList = Array.from(jobs.entries()).map(([name, job]) => ({
25+
['Job Name']: name,
26+
['Cron']: job.cronTime.source
27+
}));
28+
console.log('\nCurrent Cron Jobs:\n');
29+
console.table(jobList);
30+
} catch (error) {
31+
console.error('Failed to retrieve cron jobs:', error);
32+
}
33+
}
34+
}

lib/commands/run-cron-jobs.command.ts

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { Command } from '../decorators/command.decorator';
3+
import { BaseCommand } from './base.command';
4+
import { SchedulerRegistry } from '@nestjs/schedule';
5+
6+
@Command({
7+
signature: 'run-cron-jobs',
8+
description: 'Run specified cron jobs',
9+
options: [
10+
{
11+
value: '--jobs <jobs...>',
12+
description: 'List of cron jobs to run'
13+
}
14+
]
15+
})
16+
@Injectable()
17+
export class RunCronJobsCommand extends BaseCommand {
18+
constructor(private readonly schedulerRegistry: SchedulerRegistry) {
19+
super();
20+
}
21+
22+
public async handle(): Promise<void> {
23+
const { jobs } = this.program.opts();
24+
25+
if (!jobs || jobs.length === 0) {
26+
console.warn('No cron jobs specified.');
27+
return;
28+
}
29+
30+
console.log(`Executing cron jobs: ${jobs.join(', ')}`);
31+
32+
await Promise.all(
33+
jobs.map(async (jobName: string) => {
34+
const job = this.schedulerRegistry.getCronJob(jobName);
35+
if (!job) {
36+
console.warn(`Cron job "${jobName}" not found.`);
37+
return;
38+
}
39+
console.log(`Executing cron job "${jobName}"...`);
40+
job.waitForCompletion = true;
41+
await job.fireOnTick();
42+
})
43+
);
44+
45+
console.log('Cron job execution completed.');
46+
}
47+
}

0 commit comments

Comments
 (0)