Skip to content

Commit

Permalink
Event Stream Service
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Sherman <[email protected]>
  • Loading branch information
usingtechnology committed Jul 26, 2024
1 parent 4ad4007 commit 9d45d5a
Show file tree
Hide file tree
Showing 19 changed files with 1,048 additions and 9 deletions.
10 changes: 10 additions & 0 deletions .devcontainer/chefs_local/local.json.sample
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@
"proxy": "352f7c24819086bf3df5a38c1a40586045f73e0007440c9d27d59ee8560e3fe7"
}
},
"features": {
"eventStreamService": true
},
"eventStreamService": {
"servers": "localhost:4222,localhost:4223,localhost:4224",
"streamName": "CHEFS",
"domain": "forms",
"username": "chefs",
"password": "password"
},
"serviceClient": {
"commonServices": {
"ches": {
Expand Down
45 changes: 43 additions & 2 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@ const middleware = require('./src/forms/common/middleware');
const v1Router = require('./src/routes/v1');

const DataConnection = require('./src/db/dataConnection');
const { featureFlags } = require('./src/components/featureFlags');
const dataConnection = new DataConnection();
const { eventStreamService } = require('./src/components/eventStreamService');

const apiRouter = express.Router();
const state = {
connections: {
data: false,
eventStreamService: false,
},
ready: false,
shutdown: false,
};

if (!featureFlags.eventStreamService) delete state.connections.eventStreamService;

let probeId;
const app = express();
app.use(compression());
Expand Down Expand Up @@ -56,7 +63,8 @@ apiRouter.use('/config', (_req, res, next) => {
const frontend = config.get('frontend');
// we will need to pass
const uploads = config.get('files.uploads');
const feConfig = { ...frontend, uploads: uploads };
const features = config.get('features');
const feConfig = { ...frontend, uploads: uploads, features: features };
res.status(200).json(feConfig);
} catch (err) {
next(err);
Expand Down Expand Up @@ -155,6 +163,7 @@ function cleanup() {
log.info('Cleaning up...', { function: 'cleanup' });
clearInterval(probeId);

if (featureFlags.eventStreamService) eventStreamService.closeConnection();
dataConnection.close(() => process.exit());

// Wait 10 seconds max before hard exiting
Expand All @@ -170,6 +179,10 @@ function initializeConnections() {
// Initialize connections and exit if unsuccessful
const tasks = [dataConnection.checkAll()];

if (featureFlags.eventStreamService) {
tasks.push(eventStreamService.checkConnection());
}

Promise.all(tasks)
.then((results) => {
state.connections.data = results[0];
Expand All @@ -178,9 +191,23 @@ function initializeConnections() {
log.info('DataConnection Reachable', {
function: 'initializeConnections',
});

if (featureFlags.eventStreamService) {
state.connections.eventStreamService = results[1];
if (state.connections.eventStreamService) {
log.info('EventStreamService Reachable', {
function: 'initializeConnections',
});
}
} else {
log.info('EventStreamService feature is not enabled.');
}
})
.catch((error) => {
log.error(`Initialization failed: Database OK = ${state.connections.data}`, { function: 'initializeConnections' });
if (featureFlags.eventStreamService)
log.error(`Initialization failed: EventStreamService OK = ${state.connections.eventStreamService}`, { function: 'initializeConnections' });

log.error('Connection initialization failure', error.message, {
function: 'initializeConnections',
});
Expand All @@ -196,7 +223,16 @@ function initializeConnections() {
function: 'initializeConnections',
});
// Start periodic 10 second connection probe check
probeId = setInterval(checkConnections, 10000);
probeId = setInterval(checkConnections, 30000);
} else {
log.error(`Service not ready to accept traffic`, {
function: 'initializeConnections',
});
log.error(`Database connected = ${state.connections.data}`, { function: 'initializeConnections' });
if (featureFlags.eventStreamService) log.error(`EventStreamService connected = ${state.connections.eventStreamService}`, { function: 'initializeConnections' });

process.exitCode = 1;
shutdown();
}
});
}
Expand All @@ -210,16 +246,21 @@ function checkConnections() {
const wasReady = state.ready;
if (!state.shutdown) {
const tasks = [dataConnection.checkConnection()];
if (featureFlags.eventStreamService) tasks.push(eventStreamService.checkConnection());

Promise.all(tasks).then((results) => {
state.connections.data = results[0];
if (featureFlags.eventStreamService) state.connections.eventStreamService = results[1];

state.ready = Object.values(state.connections).every((x) => x);
if (!wasReady && state.ready)
log.info('Service ready to accept traffic', {
function: 'checkConnections',
});
log.verbose(state);
if (!state.ready) {
log.error(`Database connected = ${state.connections.data}`, { function: 'checkConnections' });
if (featureFlags.eventStreamService) log.error(`EventStreamService connected = ${state.connections.eventStreamService}`, { function: 'checkConnections' });
process.exitCode = 1;
shutdown();
}
Expand Down
10 changes: 10 additions & 0 deletions app/config/custom-environment-variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@
"proxy": "SERVER_ENCRYPTION_PROXY"
}
},
"features": {
"eventStreamService": "FEATURES_EVENTSTREAMSERVICE"
},
"eventStreamService": {
"servers": "EVENTSTREAMSERVICE_SERVERS",
"streamName": "EVENTSTREAMSERVICE_STREAMNAME",
"domain": "EVENTSTREAMSERVICE_DOMAIN",
"username": "EVENTSTREAMSERVICE_USERNAME",
"password": "EVENTSTREAMSERVICE_PASSWORD"
},
"serviceClient": {
"commonServices": {
"ches": {
Expand Down
10 changes: 10 additions & 0 deletions app/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@
"proxy": "352f7c24819086bf3df5a38c1a40586045f73e0007440c9d27d59ee8560e3fe7"
}
},
"features": {
"eventStreamService": true
},
"eventStreamService": {
"servers": "localhost:4222,localhost:4223,localhost:4224",
"streamName": "CHEFS",
"domain": "forms",
"username": "chefs",
"password": "password"
},
"serviceClient": {
"commonServices": {
"ches": {
Expand Down
49 changes: 49 additions & 0 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"mime-types": "^2.1.35",
"moment": "^2.29.4",
"multer": "^1.4.5-lts.1",
"nats": "^2.28.0",
"nested-objects-util": "^1.1.2",
"objection": "^3.0.1",
"pg": "^8.10.0",
Expand Down
Loading

0 comments on commit 9d45d5a

Please sign in to comment.