Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#138): move polling to openhim channel config #139

Merged
merged 15 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mediator/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export const OPENMRS = {
timeout: Number(getEnvironmentVariable('REQUEST_TIMEOUT', '5000'))
};

export const SYNC_INTERVAL = getEnvironmentVariable('SYNC_INTERVAL', '60000');
//export const SYNC_INTERVAL = getEnvironmentVariable('SYNC_INTERVAL', '60000');
witash marked this conversation as resolved.
Show resolved Hide resolved
// hard code sync interval to 1 minute because it is hard coded in mediator config
export const SYNC_INTERVAL = '60000';
witash marked this conversation as resolved.
Show resolved Hide resolved

function getEnvironmentVariable(env: string, def: string) {
if (process.env.NODE_ENV === 'test') {
Expand Down
21 changes: 20 additions & 1 deletion mediator/config/openmrs_mediator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,30 @@ export const openMRSMediatorConfig = {
version: '1.0.0',
name: 'OpenMRS Mediator',
description: 'A mediator to sync CHT data with OpenMRS',
defaultChannelConfig: [
{
name: 'OpenMRS Sync',
urlPattern: '^/trigger$',
routes: [
{
name: 'OpenMRS polling Mediator',
host: 'mediator',
path: '/openmrs/sync',
port: 6000,
primary: true,
type: 'http',
},
],
allow: ['interop'],
type: 'polling',
pollingSchedule: '1 minute'
},
],
endpoints: [
{
name: 'OpenMRS Mediator',
host: 'mediator',
path: '/',
path: '/openmrs/sync',
port: '6000',
primary: true,
type: 'http',
Expand Down
21 changes: 6 additions & 15 deletions mediator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { mediatorConfig } from './config/mediator';
import { openMRSMediatorConfig } from './config/openmrs_mediator';
import { logger } from './logger';
import bodyParser from 'body-parser';
import {PORT, OPENHIM, SYNC_INTERVAL, OPENMRS} from './config';
import {PORT, OPENHIM, OPENMRS} from './config';
import patientRoutes from './src/routes/patient';
import serviceRequestRoutes from './src/routes/service-request';
import encounterRoutes from './src/routes/encounter';
import organizationRoutes from './src/routes/organization';
import endpointRoutes from './src/routes/endpoint';
import chtRoutes from './src/routes/cht';
import openMRSRoutes from './src/routes/openmrs';
import { registerMediatorCallback } from './src/utils/openhim';
import { syncPatients, syncEncounters } from './src/utils/openmrs_sync'
import os from 'os';

const {registerMediator} = require('openhim-mediator-utils');
Expand All @@ -34,30 +34,21 @@ app.use('/encounter', encounterRoutes);
app.use('/organization', organizationRoutes);
app.use('/endpoint', endpointRoutes);

// routes for cht docs
// routes for CHT docs
app.use('/cht', chtRoutes);

// routes for OpenMRS
app.use('/openmrs', openMRSRoutes);

if (process.env.NODE_ENV !== 'test') {
app.listen(PORT, () => logger.info(`Server listening on port ${PORT}`));

// TODO => inject the 'port' and 'http scheme' into 'mediatorConfig'
registerMediator(OPENHIM, mediatorConfig, registerMediatorCallback);

// if OPENMRS is specified, register its mediator
// and start the sync background task
if (OPENMRS.url) {
registerMediator(OPENHIM, openMRSMediatorConfig, registerMediatorCallback);
// start patient and ecnounter sync in the background
setInterval(async () => {
try {
const startTime = new Date();
startTime.setHours(startTime.getHours() - 1);
await syncPatients(startTime);
await syncEncounters(startTime);
} catch (error: any) {
logger.error(error);
}
}, Number(SYNC_INTERVAL));
}
}

Expand Down
15 changes: 15 additions & 0 deletions mediator/src/controllers/openmrs.ts
witash marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { logger } from '../../logger';
import { syncPatients, syncEncounters } from '../utils/openmrs_sync'

export async function sync() {
try {
const startTime = new Date();
startTime.setHours(startTime.getHours() - 1);
witash marked this conversation as resolved.
Show resolved Hide resolved
await syncPatients(startTime);
await syncEncounters(startTime);
witash marked this conversation as resolved.
Show resolved Hide resolved
return { status: 200, data: { message: `OpenMRS sync completed successfully`} };
} catch(error: any) {
logger.error(error);
return { status: 500, data: { message: `Error during OpenMRS Sync`} };
}
}
12 changes: 12 additions & 0 deletions mediator/src/routes/openmrs.ts
witash marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Router } from 'express';
import { requestHandler } from '../utils/request';
import { sync } from '../controllers/openmrs'

const router = Router();

router.get(
'/sync',
requestHandler((req) => sync())
);

export default router;
Loading