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
5 changes: 0 additions & 5 deletions integrations/sample-app/app/boot/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import config from '#config/index';
import { IntentAppContainer, IntentProvidersFactory } from '@intentjs/core';
import { AppServiceProvider } from '#boot/sp/app';
import { ConsoleServiceProvider } from '#boot/sp/console';

export class ApplicationContainer extends IntentAppContainer {
build() {
/**
Expand All @@ -16,10 +15,6 @@ export class ApplicationContainer extends IntentAppContainer {
* Register our main application service providers.
*/
this.add(AppServiceProvider);

/**
* Registering our console commands service providers.
*/
this.add(ConsoleServiceProvider);
}
}
80 changes: 14 additions & 66 deletions integrations/sample-app/app/boot/sp/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { IntentApplicationContext, ServiceProvider } from '@intentjs/core';
import {
IntentApplicationContext,
ModuleRef,
ServiceProvider,
} from '@intentjs/core';
import { OrderPlacedListener } from '#events/listeners/sample-listener';
import { QueueJobs } from '#jobs/job';
import { UserDbRepository } from '#repositories/userDbRepository';
Expand Down Expand Up @@ -38,71 +42,15 @@ export class AppServiceProvider extends ServiceProvider {
/**
* Bootstrap any application service here.
*/
boot(app: IntentApplicationContext) {
/**
* Schedule Intent Command to run daily.
*/

Schedule.exec('ls -la')
.everyTwoSeconds()
.appendOutputToFile('output.txt')
.emailOutputTo('[email protected]')
.run();

// Schedule.command('send:email')
// // .days([Schedule.MONDAY, Schedule.THURSDAY])
// .hourly()
// .timezone('America/Chicago')
// .between('8:00', '17:00')
// .run();

/**
* Simple callback, with lifecycle methods `before` and `after`.
*/
// Schedule.call(() => {
// console.log('inside the schedule method');
// return 'hello';
// })
// .purpose('sample scheduler')
// .before(() => console.log('this will run before the cron'))
// .after((output: any) =>
// console.log('this will run after the cron', output),
// )
// .onSuccess((result) =>
// console.log('this will run on success the cron', result),
// )
// .onFailure((error) =>
// console.log('this will run on failure the cron', error),
// )
// // .pingBefore('https://webhook.site/79dcb789-869b-459d-9ba9-638aae449328')
// .thenPing('https://webhook.site/79dcb789-869b-459d-9ba9-638aae449328')
// .weekends()
// .everyTwoSeconds()
// // .pingOnSuccess('https://webhook.site/79dcb789-869b-459d-9ba9-638aae449328')
// .when(() => true)
// .appendOutputToFile('output.txt')
// .run();
boot(app: IntentApplicationContext) {}

/**
* Running a job every day at 5AM.
*/
// Schedule.job({
// job: 'process_abandoned_cart',
// data: { from: '2024-04-16', to: '2024-04-17' },
// })
// .purpose('cron dispatching job every day at 5AM')
// .everyFiveSeconds()
// .weekends()
// .run();
/**
* Shutdown any application service here.
*/
shutdown(app: IntentApplicationContext) {}

// Schedule.command('emails:send')
// .daily()
// .onSuccess((result) => {
// console.log('emails:send on success', result);
// })
// .onFailure((error: Error) => {
// console.log('emails:send on failure', error);
// })
// .run();
}
/**
* Register any schedules here.
*/
async schedules(ref: ModuleRef): Promise<void> {}
}
45 changes: 23 additions & 22 deletions integrations/sample-app/app/boot/sp/console.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
import { IntentApplicationContext, ServiceProvider } from '@intentjs/core';
import { IntentApplicationContext } from '@intentjs/core';
import { TestCacheConsoleCommand } from '#console/cache';
import { GreetingCommand } from '#console/greeting';
import { TestLogConsoleCommand } from '#console/log';
import { TestMailConsoleCommand } from '#console/mailer';
import { TestQueueConsoleCommand } from '#console/queue';
import { TestStorageConsoleCommand } from '#console/storage';
import { Dispatch } from '@intentjs/core/queue';
import { ModuleRef, ServiceProvider } from '@intentjs/core';
import { Schedule } from '@intentjs/core/schedule';

export class ConsoleServiceProvider extends ServiceProvider {
/**
* Register any application services here.
*/
register() {
this.bind(GreetingCommand, TestCacheConsoleCommand);
this.bind(TestStorageConsoleCommand);
this.bind(TestLogConsoleCommand);
this.bind(TestQueueConsoleCommand);
this.bind(TestMailConsoleCommand);
this.bind(
TestCacheConsoleCommand,
GreetingCommand,
TestLogConsoleCommand,
TestQueueConsoleCommand,
TestMailConsoleCommand,
TestStorageConsoleCommand,
);
}

/**
* Bootstrap any application service here.
*
* Bootstrap any application services here.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
boot(app: IntentApplicationContext) {
let i = 0;
setInterval(async () => {
await Dispatch({
job: 'redis_job',
data: { count: i++ },
});
}, 1000);
}
boot(app: IntentApplicationContext) {}

/**
* Shutdown any application services here.
*/
shutdown(app: IntentApplicationContext) {}

/**
* Register any schedules here.
*/
async schedules(ref: ModuleRef): Promise<void> {}
}
5 changes: 5 additions & 0 deletions integrations/sample-app/config/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,10 @@ export default configNamespace(
profilesSampleRate: 1.0,
integrateNodeProfile: true,
},

schedules: {
runInAnotherThread: true,
timezone: 'Asia/Kolkata',
},
}),
);
2 changes: 1 addition & 1 deletion integrations/sample-app/config/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { knexSnakeCaseMappers } from 'objection';
export default configNamespace(
'db',
(): DatabaseOptions => ({
isGlobal: true,
isGlobal: false,
default: 'pg',
connections: {
pg: {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/lib/application/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ export interface AppConfig {
validationErrorSerializer?: GenericClass;
};
sentry?: SentryConfig;

schedules?: {
runInAnotherThread?: boolean;
timezone?: string;
};
}
6 changes: 3 additions & 3 deletions packages/core/lib/foundation/actuator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IntentHttpServer } from '../rest/foundation/server.js';
import { IntentProcess } from './intent-process.js';
import { IntentConsoleProcess } from './intent-process.js';

type ContainerImporterType = () => any;

Expand All @@ -22,8 +22,8 @@ export class Actuator {
* Get the intent process
* @returns Get the intent process
*/
cli(): IntentProcess {
return new IntentProcess(this);
cli(): IntentConsoleProcess {
return new IntentConsoleProcess(this);
}

http(): IntentHttpServer {
Expand Down
11 changes: 10 additions & 1 deletion packages/core/lib/foundation/app-container.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Provider } from '@nestjs/common';
import { IntentApplicationContext, Type } from '../interfaces/index.js';
import { ImportType, ServiceProvider } from './service-provider.js';
import { ModuleRef } from '@nestjs/core';
import { SchedulerRegistry } from '../scheduler/metadata.js';

export abstract class IntentAppContainer {
static serviceProviders: ServiceProvider[] = [];
Expand Down Expand Up @@ -33,7 +35,14 @@ export abstract class IntentAppContainer {

async boot(app: IntentApplicationContext): Promise<void> {
for (const serviceProvider of IntentAppContainer.serviceProviders) {
serviceProvider.boot(app);
await serviceProvider.boot(app);
await serviceProvider.schedules(app.get(ModuleRef));
}
}

async shutdown(app: IntentApplicationContext): Promise<void> {
for (const serviceProvider of IntentAppContainer.serviceProviders) {
await serviceProvider.shutdown(app);
}
}

Expand Down
10 changes: 8 additions & 2 deletions packages/core/lib/foundation/container-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import { NestFactory } from '@nestjs/core';
import type { IntentApplicationContext, Type } from '../interfaces/index.js';
import { ModuleBuilder } from './module-builder.js';
import { IntentAppContainer } from './app-container.js';
import { ServiceProvider } from './service-provider.js';

export class ContainerFactory {
static async createStandalone(
containerCls: Type<IntentAppContainer>,
extraServiceProviders: Type<ServiceProvider>[] = [],
): Promise<IntentApplicationContext> {
const container = new containerCls();

if (extraServiceProviders.length > 0) {
container.add(...extraServiceProviders);
}

container.build();

/**
Expand All @@ -24,9 +30,9 @@ export class ContainerFactory {
});

/**
* Run the `boot` method of the main application container
* Run the `boot` & `schedules` method of the main application container
*/
container.boot(app);
await container.boot(app);

return app;
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/lib/foundation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './container-factory.js';
export * from './decorators.js';
export * from './actuator.js';
export * from './intent-process.js';
export { ModuleRef } from '@nestjs/core';
2 changes: 1 addition & 1 deletion packages/core/lib/foundation/intent-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Actuator } from './actuator.js';
import { ContainerFactory } from './container-factory.js';
import yargs from 'yargs-parser';

export class IntentProcess {
export class IntentConsoleProcess {
constructor(private readonly actuator: Actuator) {}

async handle(args: string[]): Promise<void> {
Expand Down
Loading
Loading