Skip to content

Commit

Permalink
refactor: cron
Browse files Browse the repository at this point in the history
  • Loading branch information
Murzbul committed Feb 28, 2024
1 parent 8ecf7b2 commit 461aba5
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 69 deletions.
3 changes: 3 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"secret": "mySecret",
"authorization": false
},
"service": {
"uri": "http://localhost:8080"
},
"dbConfig": {
"Mongoose": {
"uri": "DB_URI"
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
"type": "commonjs",
"engines": {
"node": ">=20.*"
}
Expand Down
11 changes: 5 additions & 6 deletions src/Main/Infrastructure/Crons/Cron.ts
Original file line number Diff line number Diff line change
@@ -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();
})();
}, {
Expand All @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions src/Main/Infrastructure/Crons/HelloCron.ts
Original file line number Diff line number Diff line change
@@ -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<void>
{
Logger.info('Hello world 2024');
}
}

export default HelloCron;
2 changes: 1 addition & 1 deletion src/Main/Infrastructure/Crons/TestCron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TestCron extends Cron

async task(): Promise<void>
{
Logger.info('hello world 2021');
Logger.info('Test cron');
}
}

Expand Down
55 changes: 0 additions & 55 deletions src/Main/Infrastructure/Factories/CronFactory.ts

This file was deleted.

50 changes: 50 additions & 0 deletions src/Main/Infrastructure/Factories/CronService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Cron from '../Crons/Cron';

export interface ICronService
{
setCrons(crons: Map<string, Cron>): void;
startAll(): void;
stopAll(): void;
}

export interface CronParams
{
executeCrons: boolean;
}

class CronService implements ICronService
{
#config: CronParams;
#crons: Map<string, Cron>;

constructor(config: CronParams)
{
this.#config = config;
}

setCrons(crons: Map<string, Cron>): 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;
11 changes: 11 additions & 0 deletions src/Shared/DI/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,8 +78,17 @@ container.register(SERVICES.AuthorizeService, {
return new AuthorizeSupabaseService(authRepository);
})
}, { lifecycle: Lifecycle.Transient });

container.register<IMessageBroker>('IMessageBroker', { useClass: RabbitMQMessageBroker }, { lifecycle: Lifecycle.Singleton });

container.register<ICronService>('ICronService', {
// @ts-ignore
useFactory: instanceCachingFactory(() =>
{
return new CronService({ executeCrons: config.executeCrons });
})
}, { lifecycle: Lifecycle.Transient });

// Factories
container.register<DatabaseFactory>(FACTORIES.IDatabaseFactory, {
// @ts-ignore
Expand All @@ -88,4 +98,5 @@ container.register<DatabaseFactory>(FACTORIES.IDatabaseFactory, {
})
}, { lifecycle: Lifecycle.Transient });


export default container;
8 changes: 5 additions & 3 deletions src/crons.ts
Original file line number Diff line number Diff line change
@@ -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<string, Cron> = new Map();
crons.set('TestCron', new TestCron());
crons.set('HelloCron', new HelloCron());

export default crons;
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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() =>
{
Expand Down Expand Up @@ -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>('ICronService');
cronService.setCrons(crons);
cronService.startAll();

// Message Broker
const messageBroker = DependencyInjector.inject<IMessageBroker>('IMessageBroker');
Expand Down

0 comments on commit 461aba5

Please sign in to comment.