oneuptime/Workers/Index.ts

63 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-10-19 14:32:10 +00:00
import { PostgresAppInstance } from 'CommonServer/Infrastructure/PostgresDatabase';
import Redis from 'CommonServer/Infrastructure/Redis';
import logger from 'CommonServer/Utils/Logger';
import App from 'CommonServer/Utils/StartServer';
2023-03-02 19:23:03 +00:00
import { QueueJob, QueueName } from 'CommonServer/Infrastructure/Queue';
import QueueWorker from 'CommonServer/Infrastructure/QueueWorker';
2022-12-18 13:15:39 +00:00
// Payments.
2022-11-22 13:26:22 +00:00
import './Jobs/PaymentProvider/CheckSubscriptionStatus';
2022-10-19 17:45:06 +00:00
2022-12-18 09:34:28 +00:00
// Announcements.
2022-12-18 09:29:03 +00:00
import './Jobs/Announcement/SendEmailToSubscribers';
2022-12-18 12:37:26 +00:00
// Incidents
import './Jobs/Incident/SendEmailToSubscribers';
2022-12-18 13:15:39 +00:00
// Scheduled Event
import './Jobs/ScheduledMaintenance/ChangeStateToOngoing';
import './Jobs/ScheduledMaintenance/SendEmailToSubscribers';
2022-11-28 18:26:07 +00:00
// Certs Routers
import StausPageCerts from './Jobs/StatusPageCerts/StausPageCerts';
2022-12-18 12:37:26 +00:00
// Express
2022-12-07 06:44:17 +00:00
import Express, { ExpressApplication } from 'CommonServer/Utils/Express';
2023-03-02 19:23:03 +00:00
import JobDictonary from './Utils/JobDictionary';
2022-11-28 18:26:07 +00:00
2022-10-19 14:32:10 +00:00
const APP_NAME: string = 'workers';
2022-11-28 18:26:07 +00:00
const app: ExpressApplication = Express.getExpressApp();
2022-12-07 06:44:17 +00:00
//cert routes.
2022-12-20 07:22:41 +00:00
app.use(`/${APP_NAME.toLocaleLowerCase()}`, StausPageCerts);
2022-11-28 18:26:07 +00:00
2022-10-19 14:32:10 +00:00
const init: Function = async (): Promise<void> => {
try {
// init the app
await App(APP_NAME);
// connect to the database.
await PostgresAppInstance.connect(
PostgresAppInstance.getDatasourceOptions()
);
// connect redis
await Redis.connect();
2023-03-02 19:23:03 +00:00
2023-03-02 19:36:06 +00:00
// Job process.
QueueWorker.getWorker(
2023-03-02 19:23:03 +00:00
QueueName.Worker,
async (job: QueueJob) => {
2023-03-02 19:36:06 +00:00
const name: string = job.name;
const funcToRun: Function = JobDictonary.getJobFunction(name);
2023-03-02 19:23:03 +00:00
await funcToRun();
},
{ concurrency: 10 }
);
2022-10-19 14:32:10 +00:00
} catch (err) {
logger.error('App Init Failed:');
logger.error(err);
}
};
2022-10-19 18:28:08 +00:00
init();