Skip to content

Commit

Permalink
fx: bug when listening for updates to config
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-onions committed Dec 10, 2024
1 parent e1049c4 commit 6d47091
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
21 changes: 9 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import express from 'express';
import path from 'path';
import { getConfig } from './config/config';
import logger from './logger';
import routes from './routes/index';
import { getRegisteredBuckets, setupMediator } from './openhim/openhim';
import { createMinioBucketListeners, ensureBucketExists } from './utils/minioClient';
import { getMediatorConfig, initializeBuckets, setupMediator } from './openhim/openhim';

const app = express();

Expand All @@ -15,15 +13,14 @@ app.listen(getConfig().port, async () => {

if (getConfig().runningMode !== 'testing' && getConfig().registerMediator) {
await setupMediator();
}

const buckets = await getRegisteredBuckets();

buckets.length === 0 && logger.warn('No buckets specified in the configuration');

for await (const { bucket, region } of buckets) {
await ensureBucketExists(bucket, region);
const mediatorConfig = await getMediatorConfig();
if (mediatorConfig) {
await initializeBuckets(mediatorConfig);
} else {
logger.error('Failed to fetch mediator config');
}
} else {
logger.info('Running in testing mode, skipping mediator setup');
}

createMinioBucketListeners(buckets.map((bucket) => bucket.bucket));
});
21 changes: 15 additions & 6 deletions src/openhim/openhim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,18 @@ export const setupMediator = async () => {
});

emitter.on('config', async (config: any) => {
logger.info('Received config from OpenHIM');

await initializeBuckets(config);
const mediatorConfig = {
config: {
minio_buckets_registry: config.minio_buckets_registry,
},
defaultChannelConfig: [],
endpoints: [],
urn: config.urn,
version: config.version,
name: config.name,
description: config.description,
};
await initializeBuckets(mediatorConfig);
});
});
});
Expand All @@ -83,7 +92,7 @@ export const setupMediator = async () => {
*
* @param mediatorConfig - The mediator config
*/
async function initializeBuckets(mediatorConfig: MediatorConfig) {
export async function initializeBuckets(mediatorConfig: MediatorConfig) {
const bucketsFromOpenhimConfig = mediatorConfig.config?.minio_buckets_registry as Bucket[];
const validBuckets: string[] = [];
const invalidBuckets: string[] = [];
Expand All @@ -93,7 +102,7 @@ async function initializeBuckets(mediatorConfig: MediatorConfig) {
logger.error(`Invalid bucket name ${bucket}, skipping`);
invalidBuckets.push(bucket);
} else {
await ensureBucketExists(bucket, region);
await ensureBucketExists(bucket, region, true);
validBuckets.push(bucket);
}
}
Expand All @@ -106,7 +115,7 @@ async function initializeBuckets(mediatorConfig: MediatorConfig) {
}
}

async function getMediatorConfig(): Promise<MediatorConfig | null> {
export async function getMediatorConfig(): Promise<MediatorConfig | null> {
logger.debug('Fetching mediator config from OpenHIM');
const mediatorConfig = resolveMediatorConfig();
const openhimConfig = resolveOpenhimConfig(mediatorConfig.urn);
Expand Down

0 comments on commit 6d47091

Please sign in to comment.