feat: Re-compute the given items cost job.

feat: Optimize the architecture.
This commit is contained in:
Ahmed Bouhuolia
2020-08-18 02:28:08 +02:00
parent 4e68a7db71
commit d423365a19
44 changed files with 1605 additions and 798 deletions

View File

@@ -0,0 +1,11 @@
import Agenda from 'agenda';
import config from '@/../config/config';
export default ({ mongoConnection }) => {
return new Agenda({
mongo: mongoConnection,
db: { collection: config.agenda.dbCollection },
processEvery: config.agenda.pooltime,
maxConcurrency: config.agenda.concurrency,
});
};

View File

@@ -0,0 +1,20 @@
import { Container } from 'typedi';
import LoggerInstance from '@/services/Logger';
import agendaFactory from '@/loaders/agenda';
export default ({ mongoConnection, knex }) => {
try {;
const agendaInstance = agendaFactory({ mongoConnection });
Container.set('agenda', agendaInstance);
Container.set('logger', LoggerInstance)
Container.set('knex', knex);
LoggerInstance.info('Agenda has been injected into container');
return { agenda: agendaInstance };
} catch (e) {
LoggerInstance.error('Error on dependency injector loader: %o', e);
throw e;
}
};

View File

View File

@@ -0,0 +1,21 @@
import express from 'express';
import helmet from 'helmet';
import boom from 'express-boom';
import errorHandler from 'errorhandler';
import i18n from 'i18n';
import fileUpload from 'express-fileupload';
import routes from '@/http';
export default ({ app }) => {
// Express configuration.
app.set('port', 3000);
app.use(helmet());
app.use(errorHandler());
app.use(boom());
app.use(express.json());
app.use(fileUpload({
createParentPath: true,
}));
routes(app);
};

View File

@@ -0,0 +1,32 @@
import Logger from '@/services/Logger';
import mongooseLoader from '@/loaders/mongoose';
import jobsLoader from '@/loaders/jobs';
import expressLoader from '@/loaders/express';
import databaseLoader from '@/database/knex';
import dependencyInjectorLoader from '@/loaders/dependencyInjector';
import objectionLoader from '@/database/objection';
// We have to import at least all the events once so they can be triggered
import '@/loaders/events';
export default async ({ expressApp }) => {
const mongoConnection = await mongooseLoader();
Logger.info('MongoDB loaded and connected!');
// Initialize the system database once app started.
const knex = databaseLoader();
// Initialize the objection.js from knex instance.
objectionLoader({ knex });
// It returns the agenda instance because it's needed in the subsequent loaders
const { agenda } = await dependencyInjectorLoader({
mongoConnection,
knex,
});
await jobsLoader({ agenda });
Logger.info('Jobs loaded');
expressLoader({ app: expressApp });
Logger.info('Express loaded');
};

View File

@@ -0,0 +1,17 @@
import Agenda from 'agenda';
import WelcomeEmailJob from '@/Jobs/welcomeEmail';
import ComputeItemCost from '@/Jobs/ComputeItemCost';
export default ({ agenda }: { agenda: Agenda }) => {
agenda.define(
'welcome-email',
{ priority: 'high' },
new WelcomeEmailJob().handler,
);
agenda.define(
'compute-item-cost',
{ priority: 'high' },
new ComputeItemCost().handler,
);
agenda.start();
};

View File

@@ -0,0 +1,11 @@
import mongoose from 'mongoose';
import { Db } from 'mongodb';
import config from '@/../config/config';
export default async (): Promise<Db> => {
const connection = await mongoose.connect(
config.mongoDb.databaseURL,
{ useNewUrlParser: true, useCreateIndex: true },
);
return connection.connection.db;
};