oneuptime/Workflow/Index.ts

84 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-02-20 11:49:00 +00:00
import Express, {
ExpressApplication,
ExpressRequest,
ExpressResponse,
} from 'CommonServer/Utils/Express';
2023-02-01 13:08:45 +00:00
import App from 'CommonServer/Utils/StartServer';
import { PostgresAppInstance } from 'CommonServer/Infrastructure/PostgresDatabase';
import Redis from 'CommonServer/Infrastructure/Redis';
import logger from 'CommonServer/Utils/Logger';
2023-02-16 22:16:24 +00:00
import ManualAPI from './API/Manual';
2023-02-16 22:45:22 +00:00
import ComponentCode from './API/ComponentCode';
import { QueueJob, QueueName } from 'CommonServer/Infrastructure/Queue';
import QueueWorker from 'CommonServer/Infrastructure/QueueWorker';
import RunWorkflow from './Services/RunWorkflow';
import { JSONObject } from 'Common/Types/JSON';
import ObjectID from 'Common/Types/ObjectID';
2023-02-28 14:09:44 +00:00
import WorkflowAPI from './API/Workflow';
2023-08-19 19:49:24 +00:00
import { ClickhouseAppInstance } from 'CommonServer/Infrastructure/ClickhouseDatabase';
2023-02-01 13:08:45 +00:00
const APP_NAME: string = 'workflow';
const app: ExpressApplication = Express.getExpressApp();
2023-02-17 16:00:02 +00:00
app.use(`/${APP_NAME}/manual`, new ManualAPI().router);
2023-02-16 22:16:24 +00:00
2023-02-28 14:09:44 +00:00
app.use(`/${APP_NAME}`, new WorkflowAPI().router);
2023-02-20 11:49:00 +00:00
app.get(
`/${APP_NAME}/docs/:componentName`,
(req: ExpressRequest, res: ExpressResponse) => {
2023-02-20 14:51:57 +00:00
res.sendFile(
__dirname +
2023-03-02 19:36:06 +00:00
'/Docs/ComponentDocumentation/' +
req.params['componentName']
2023-02-20 14:51:57 +00:00
);
2023-02-20 11:49:00 +00:00
}
);
2023-02-19 12:10:54 +00:00
2023-08-10 17:25:15 +00:00
const init: () => Promise<void> = async (): Promise<void> => {
2023-02-01 13:08:45 +00:00
try {
// connect to the database.
await PostgresAppInstance.connect(
PostgresAppInstance.getDatasourceOptions()
);
2023-02-21 19:58:23 +00:00
app.use(`/${APP_NAME}`, new ComponentCode().router);
2023-02-17 12:03:12 +00:00
2023-02-21 19:58:23 +00:00
// init the app
await App(APP_NAME);
2023-02-21 21:23:03 +00:00
2023-02-01 13:08:45 +00:00
// connect redis
await Redis.connect();
2023-03-02 19:23:03 +00:00
2023-08-21 11:04:28 +00:00
await ClickhouseAppInstance.connect(
ClickhouseAppInstance.getDatasourceOptions()
);
2023-08-19 19:49:24 +00:00
2023-03-02 19:23:03 +00:00
// Job process.
QueueWorker.getWorker(
QueueName.Workflow,
async (job: QueueJob) => {
await new RunWorkflow().runWorkflow({
workflowId: new ObjectID(job.data['workflowId'] as string),
workflowLogId: job.data['workflowLogId']
? new ObjectID(job.data['workflowLogId'] as string)
: null,
arguments: job.data.data as JSONObject,
timeout: 5000,
});
},
{ concurrency: 10 }
);
2023-02-01 13:08:45 +00:00
} catch (err) {
logger.error('App Init Failed:');
logger.error(err);
}
};
2023-08-10 18:00:27 +00:00
init().catch((err: Error) => {
logger.error(err);
});
2023-02-01 13:08:45 +00:00
export default app;