diff --git a/config/default.json b/config/default.json index 0dde4af0..95468c34 100644 --- a/config/default.json +++ b/config/default.json @@ -14,6 +14,9 @@ "secret": "mySecret", "authorization": false }, + "service": { + "uri": "http://localhost:8080" + }, "dbConfig": { "Mongoose": { "uri": "DB_URI" diff --git a/package.json b/package.json index a0ea8c29..522dcff6 100644 --- a/package.json +++ b/package.json @@ -113,6 +113,7 @@ "ts-node": "^10.9.2", "typescript": "^5.3.3" }, + "type": "commonjs", "engines": { "node": ">=20.*" } diff --git a/src/Main/Infrastructure/Crons/Cron.ts b/src/Main/Infrastructure/Crons/Cron.ts index d39e1fab..0135b9a0 100644 --- a/src/Main/Infrastructure/Crons/Cron.ts +++ b/src/Main/Infrastructure/Crons/Cron.ts @@ -1,17 +1,16 @@ import { ScheduledTask, schedule } from 'node-cron'; -import Logger from '../../../Shared/Helpers/Logger'; abstract class Cron { - private scheduledTask: ScheduledTask; + #scheduledTask: ScheduledTask; constructor(scheduled = false) { - this.scheduledTask = schedule(this.time(), () => + this.#scheduledTask = schedule(this.time(), () => { void (async() => { - Logger.info(`Running ${this.cronName()}`); + console.info(`Running ${this.cronName()}`); await this.task(); })(); }, { @@ -21,12 +20,12 @@ abstract class Cron start(): void { - this.scheduledTask.start(); + this.#scheduledTask.start(); } stop(): void { - this.scheduledTask.stop(); + this.#scheduledTask.stop(); } abstract time(): string; diff --git a/src/Main/Infrastructure/Crons/HelloCron.ts b/src/Main/Infrastructure/Crons/HelloCron.ts new file mode 100644 index 00000000..f75a6f54 --- /dev/null +++ b/src/Main/Infrastructure/Crons/HelloCron.ts @@ -0,0 +1,22 @@ +import Cron from './Cron'; +import Logger from '../../../Shared/Helpers/Logger'; + +class HelloCron extends Cron +{ + cronName(): string + { + return HelloCron.name; + } + + time(): string + { + return '* * * * *'; + } + + async task(): Promise + { + Logger.info('Hello world 2024'); + } +} + +export default HelloCron; diff --git a/src/Main/Infrastructure/Crons/TestCron.ts b/src/Main/Infrastructure/Crons/TestCron.ts index a67aaaaa..91f6b0d9 100644 --- a/src/Main/Infrastructure/Crons/TestCron.ts +++ b/src/Main/Infrastructure/Crons/TestCron.ts @@ -15,7 +15,7 @@ class TestCron extends Cron async task(): Promise { - Logger.info('hello world 2021'); + Logger.info('Test cron'); } } diff --git a/src/Main/Infrastructure/Factories/CronFactory.ts b/src/Main/Infrastructure/Factories/CronFactory.ts deleted file mode 100644 index 96c2ce6a..00000000 --- a/src/Main/Infrastructure/Factories/CronFactory.ts +++ /dev/null @@ -1,55 +0,0 @@ -import crons from '../../../crons'; -import MainConfig from '../../../Config/MainConfig'; - -class CronFactory -{ - private readonly config = MainConfig.getInstance(); - - private crons = { - ...crons - }; - - start(name: keyof CronFactory['crons'] = null): void - { - const executeCrons: boolean = this.config.getConfig().executeCrons; - - if (name) - { - if (executeCrons) - { - this.one(name, 'start'); - } - } - else if (executeCrons) - { - this.all('start'); - } - } - - stop(name: keyof CronFactory['crons'] = null): void - { - if (name) - { - this.one(name, 'stop'); - } - else - { - this.all('stop'); - } - } - - private one(name: keyof CronFactory['crons'], method: 'start' | 'stop') - { - (new this.crons[name]())[method](); - } - - private all(method: 'start' | 'stop'): void - { - for (const name of Object.keys(this.crons)) - { - (new this.crons[< keyof CronFactory['crons']> name]())[method](); - } - } -} - -export default CronFactory; diff --git a/src/Main/Infrastructure/Factories/CronService.ts b/src/Main/Infrastructure/Factories/CronService.ts new file mode 100644 index 00000000..f328cf12 --- /dev/null +++ b/src/Main/Infrastructure/Factories/CronService.ts @@ -0,0 +1,50 @@ +import Cron from '../Crons/Cron'; + +export interface ICronService +{ + setCrons(crons: Map): void; + startAll(): void; + stopAll(): void; +} + +export interface CronParams +{ + executeCrons: boolean; +} + +class CronService implements ICronService +{ + #config: CronParams; + #crons: Map; + + constructor(config: CronParams) + { + this.#config = config; + } + + setCrons(crons: Map): void + { + this.#crons = crons; + } + + startAll(): void + { + if (this.#config.executeCrons) + { + for (const [_, cron] of this.#crons) + { + cron.start(); + } + } + } + + stopAll(): void + { + for (const [_, cron] of this.#crons) + { + cron.stop(); + } + } +} + +export default CronService; diff --git a/src/Shared/DI/container.ts b/src/Shared/DI/container.ts index 9a01f96d..214dc3d4 100644 --- a/src/Shared/DI/container.ts +++ b/src/Shared/DI/container.ts @@ -27,6 +27,7 @@ import CacheRepository from '../../Main/Infrastructure/Repositories/CacheReposit import DatabaseFactory from '../../Main/Infrastructure/Factories/DatabaseFactory'; import { IMessageBroker } from '../Infrastructure/IMessageBroker'; import RabbitMQMessageBroker from '../Infrastructure/RabbitMQMessageBroker'; +import CronService, { ICronService } from '../../Main/Infrastructure/Factories/CronService'; const config = MainConfig.getInstance().getConfig(); const defaultDbConfig = config.dbConfig.default; @@ -77,8 +78,17 @@ container.register(SERVICES.AuthorizeService, { return new AuthorizeSupabaseService(authRepository); }) }, { lifecycle: Lifecycle.Transient }); + container.register('IMessageBroker', { useClass: RabbitMQMessageBroker }, { lifecycle: Lifecycle.Singleton }); +container.register('ICronService', { + // @ts-ignore + useFactory: instanceCachingFactory(() => + { + return new CronService({ executeCrons: config.executeCrons }); + }) +}, { lifecycle: Lifecycle.Transient }); + // Factories container.register(FACTORIES.IDatabaseFactory, { // @ts-ignore @@ -88,4 +98,5 @@ container.register(FACTORIES.IDatabaseFactory, { }) }, { lifecycle: Lifecycle.Transient }); + export default container; diff --git a/src/crons.ts b/src/crons.ts index 1a5dd16d..beb1e319 100644 --- a/src/crons.ts +++ b/src/crons.ts @@ -1,7 +1,9 @@ import TestCron from './Main/Infrastructure/Crons/TestCron'; +import HelloCron from './Main/Infrastructure/Crons/HelloCron'; +import Cron from './Main/Infrastructure/Crons/Cron'; -const crons = { - TestCron -}; +const crons: Map = new Map(); +crons.set('TestCron', new TestCron()); +crons.set('HelloCron', new HelloCron()); export default crons; diff --git a/src/index.ts b/src/index.ts index 5c391ae6..d9c2b6b2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,7 @@ import { FACTORIES, REPOSITORIES } from './Shared/DI/Injects'; import MainConfig from './Config/MainConfig'; import DatabaseFactory from './Main/Infrastructure/Factories/DatabaseFactory'; -import CronFactory from './Main/Infrastructure/Factories/CronFactory'; +import { ICronService } from './Main/Infrastructure/Factories/CronService'; import AppBootstrapFactory from './Main/Presentation/Factories/AppBootstrapFactory'; import ICreateConnection from './Main/Infrastructure/Database/ICreateConnection'; import Logger from './Shared/Helpers/Logger'; @@ -18,6 +18,7 @@ import SendMessageEvent from './Notification/Domain/Events/SendMessageEvent'; import EmailEvent from './Auth/Infrastructure/Events/EmailEvent'; import ICacheDataAccess from './Main/Infrastructure/Repositories/ICacheDataAccess'; import { IMessageBroker } from './Shared/Infrastructure/IMessageBroker'; +import crons from './crons'; void (async() => { @@ -57,9 +58,10 @@ void (async() => eventHandler.setEvent(new EmailEvent()); eventHandler.setEvent(new SendMessageEvent()); - // Create cron - const cronFactory = new CronFactory(); - cronFactory.start(); + // Create Cron Service + const cronService = DependencyInjector.inject('ICronService'); + cronService.setCrons(crons); + cronService.startAll(); // Message Broker const messageBroker = DependencyInjector.inject('IMessageBroker');