Skip to content

Commit 532a405

Browse files
committed
feat(core): enhance scheduling system and refactor service providers
- Introduced new lifecycle methods for service providers: shutdown and schedules. - Updated ApplicationContainer to handle service provider schedules during boot. - Refactored AppServiceProvider and ConsoleServiceProvider to implement new lifecycle methods. - Added configuration options for scheduling in app settings, including timezone and threading. - Improved Schedule class to support dynamic scheduling based on configuration. - Added new console command for starting scheduled tasks. - Bumped version to 0.1.58.
1 parent 9f87946 commit 532a405

File tree

18 files changed

+199
-116
lines changed

18 files changed

+199
-116
lines changed

integrations/sample-app/app/boot/container.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import config from '#config/index';
22
import { IntentAppContainer, IntentProvidersFactory } from '@intentjs/core';
33
import { AppServiceProvider } from '#boot/sp/app';
44
import { ConsoleServiceProvider } from '#boot/sp/console';
5-
65
export class ApplicationContainer extends IntentAppContainer {
76
build() {
87
/**
@@ -16,10 +15,6 @@ export class ApplicationContainer extends IntentAppContainer {
1615
* Register our main application service providers.
1716
*/
1817
this.add(AppServiceProvider);
19-
20-
/**
21-
* Registering our console commands service providers.
22-
*/
2318
this.add(ConsoleServiceProvider);
2419
}
2520
}
Lines changed: 14 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars */
2-
import { IntentApplicationContext, ServiceProvider } from '@intentjs/core';
2+
import {
3+
IntentApplicationContext,
4+
ModuleRef,
5+
ServiceProvider,
6+
} from '@intentjs/core';
37
import { OrderPlacedListener } from '#events/listeners/sample-listener';
48
import { QueueJobs } from '#jobs/job';
59
import { UserDbRepository } from '#repositories/userDbRepository';
@@ -38,71 +42,15 @@ export class AppServiceProvider extends ServiceProvider {
3842
/**
3943
* Bootstrap any application service here.
4044
*/
41-
boot(app: IntentApplicationContext) {
42-
/**
43-
* Schedule Intent Command to run daily.
44-
*/
45-
46-
Schedule.exec('ls -la')
47-
.everyTwoSeconds()
48-
.appendOutputToFile('output.txt')
49-
.emailOutputTo('vinayak@tryhanalabs.com')
50-
.run();
51-
52-
// Schedule.command('send:email')
53-
// // .days([Schedule.MONDAY, Schedule.THURSDAY])
54-
// .hourly()
55-
// .timezone('America/Chicago')
56-
// .between('8:00', '17:00')
57-
// .run();
58-
59-
/**
60-
* Simple callback, with lifecycle methods `before` and `after`.
61-
*/
62-
// Schedule.call(() => {
63-
// console.log('inside the schedule method');
64-
// return 'hello';
65-
// })
66-
// .purpose('sample scheduler')
67-
// .before(() => console.log('this will run before the cron'))
68-
// .after((output: any) =>
69-
// console.log('this will run after the cron', output),
70-
// )
71-
// .onSuccess((result) =>
72-
// console.log('this will run on success the cron', result),
73-
// )
74-
// .onFailure((error) =>
75-
// console.log('this will run on failure the cron', error),
76-
// )
77-
// // .pingBefore('https://webhook.site/79dcb789-869b-459d-9ba9-638aae449328')
78-
// .thenPing('https://webhook.site/79dcb789-869b-459d-9ba9-638aae449328')
79-
// .weekends()
80-
// .everyTwoSeconds()
81-
// // .pingOnSuccess('https://webhook.site/79dcb789-869b-459d-9ba9-638aae449328')
82-
// .when(() => true)
83-
// .appendOutputToFile('output.txt')
84-
// .run();
45+
boot(app: IntentApplicationContext) {}
8546

86-
/**
87-
* Running a job every day at 5AM.
88-
*/
89-
// Schedule.job({
90-
// job: 'process_abandoned_cart',
91-
// data: { from: '2024-04-16', to: '2024-04-17' },
92-
// })
93-
// .purpose('cron dispatching job every day at 5AM')
94-
// .everyFiveSeconds()
95-
// .weekends()
96-
// .run();
47+
/**
48+
* Shutdown any application service here.
49+
*/
50+
shutdown(app: IntentApplicationContext) {}
9751

98-
// Schedule.command('emails:send')
99-
// .daily()
100-
// .onSuccess((result) => {
101-
// console.log('emails:send on success', result);
102-
// })
103-
// .onFailure((error: Error) => {
104-
// console.log('emails:send on failure', error);
105-
// })
106-
// .run();
107-
}
52+
/**
53+
* Register any schedules here.
54+
*/
55+
async schedules(ref: ModuleRef): Promise<void> {}
10856
}
Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
1-
import { IntentApplicationContext, ServiceProvider } from '@intentjs/core';
1+
import { IntentApplicationContext } from '@intentjs/core';
22
import { TestCacheConsoleCommand } from '#console/cache';
33
import { GreetingCommand } from '#console/greeting';
44
import { TestLogConsoleCommand } from '#console/log';
55
import { TestMailConsoleCommand } from '#console/mailer';
66
import { TestQueueConsoleCommand } from '#console/queue';
77
import { TestStorageConsoleCommand } from '#console/storage';
8-
import { Dispatch } from '@intentjs/core/queue';
8+
import { ModuleRef, ServiceProvider } from '@intentjs/core';
9+
import { Schedule } from '@intentjs/core/schedule';
910

1011
export class ConsoleServiceProvider extends ServiceProvider {
11-
/**
12-
* Register any application services here.
13-
*/
1412
register() {
15-
this.bind(GreetingCommand, TestCacheConsoleCommand);
16-
this.bind(TestStorageConsoleCommand);
17-
this.bind(TestLogConsoleCommand);
18-
this.bind(TestQueueConsoleCommand);
19-
this.bind(TestMailConsoleCommand);
13+
this.bind(
14+
TestCacheConsoleCommand,
15+
GreetingCommand,
16+
TestLogConsoleCommand,
17+
TestQueueConsoleCommand,
18+
TestMailConsoleCommand,
19+
TestStorageConsoleCommand,
20+
);
2021
}
2122

2223
/**
23-
* Bootstrap any application service here.
24-
*
24+
* Bootstrap any application services here.
2525
*/
26-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
27-
boot(app: IntentApplicationContext) {
28-
let i = 0;
29-
setInterval(async () => {
30-
await Dispatch({
31-
job: 'redis_job',
32-
data: { count: i++ },
33-
});
34-
}, 1000);
35-
}
26+
boot(app: IntentApplicationContext) {}
27+
28+
/**
29+
* Shutdown any application services here.
30+
*/
31+
shutdown(app: IntentApplicationContext) {}
32+
33+
/**
34+
* Register any schedules here.
35+
*/
36+
async schedules(ref: ModuleRef): Promise<void> {}
3637
}

integrations/sample-app/config/app.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,10 @@ export default configNamespace(
9898
profilesSampleRate: 1.0,
9999
integrateNodeProfile: true,
100100
},
101+
102+
schedules: {
103+
runInAnotherThread: true,
104+
timezone: 'Asia/Kolkata',
105+
},
101106
}),
102107
);

integrations/sample-app/config/database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { knexSnakeCaseMappers } from 'objection';
55
export default configNamespace(
66
'db',
77
(): DatabaseOptions => ({
8-
isGlobal: true,
8+
isGlobal: false,
99
default: 'pg',
1010
connections: {
1111
pg: {

packages/core/lib/application/interface.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ export interface AppConfig {
1818
validationErrorSerializer?: GenericClass;
1919
};
2020
sentry?: SentryConfig;
21+
22+
schedules?: {
23+
runInAnotherThread?: boolean;
24+
timezone?: string;
25+
};
2126
}

packages/core/lib/foundation/actuator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IntentHttpServer } from '../rest/foundation/server.js';
2-
import { IntentProcess } from './intent-process.js';
2+
import { IntentConsoleProcess } from './intent-process.js';
33

44
type ContainerImporterType = () => any;
55

@@ -22,8 +22,8 @@ export class Actuator {
2222
* Get the intent process
2323
* @returns Get the intent process
2424
*/
25-
cli(): IntentProcess {
26-
return new IntentProcess(this);
25+
cli(): IntentConsoleProcess {
26+
return new IntentConsoleProcess(this);
2727
}
2828

2929
http(): IntentHttpServer {

packages/core/lib/foundation/app-container.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Provider } from '@nestjs/common';
22
import { IntentApplicationContext, Type } from '../interfaces/index.js';
33
import { ImportType, ServiceProvider } from './service-provider.js';
4+
import { ModuleRef } from '@nestjs/core';
5+
import { SchedulerRegistry } from '../scheduler/metadata.js';
46

57
export abstract class IntentAppContainer {
68
static serviceProviders: ServiceProvider[] = [];
@@ -33,7 +35,14 @@ export abstract class IntentAppContainer {
3335

3436
async boot(app: IntentApplicationContext): Promise<void> {
3537
for (const serviceProvider of IntentAppContainer.serviceProviders) {
36-
serviceProvider.boot(app);
38+
await serviceProvider.boot(app);
39+
await serviceProvider.schedules(app.get(ModuleRef));
40+
}
41+
}
42+
43+
async shutdown(app: IntentApplicationContext): Promise<void> {
44+
for (const serviceProvider of IntentAppContainer.serviceProviders) {
45+
await serviceProvider.shutdown(app);
3746
}
3847
}
3948

packages/core/lib/foundation/container-factory.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ import { NestFactory } from '@nestjs/core';
22
import type { IntentApplicationContext, Type } from '../interfaces/index.js';
33
import { ModuleBuilder } from './module-builder.js';
44
import { IntentAppContainer } from './app-container.js';
5+
import { ServiceProvider } from './service-provider.js';
56

67
export class ContainerFactory {
78
static async createStandalone(
89
containerCls: Type<IntentAppContainer>,
10+
extraServiceProviders: Type<ServiceProvider>[] = [],
911
): Promise<IntentApplicationContext> {
1012
const container = new containerCls();
1113

14+
if (extraServiceProviders.length > 0) {
15+
container.add(...extraServiceProviders);
16+
}
17+
1218
container.build();
1319

1420
/**
@@ -24,9 +30,9 @@ export class ContainerFactory {
2430
});
2531

2632
/**
27-
* Run the `boot` method of the main application container
33+
* Run the `boot` & `schedules` method of the main application container
2834
*/
29-
container.boot(app);
35+
await container.boot(app);
3036

3137
return app;
3238
}

packages/core/lib/foundation/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './container-factory.js';
55
export * from './decorators.js';
66
export * from './actuator.js';
77
export * from './intent-process.js';
8+
export { ModuleRef } from '@nestjs/core';

0 commit comments

Comments
 (0)