Skip to content

Commit

Permalink
chore(#9585): teach Sentinel sign language (#9658)
Browse files Browse the repository at this point in the history
No longer start and stop sentinel to stop/start transitions and run scheduled tasks. Instead make it listen to two kill signals.
Kill the test process if API is down in the before hook. This way we might get logs when the process ends up hanging.

#9585
  • Loading branch information
dianabarsan authored Nov 25, 2024
1 parent 5157ee9 commit 4090a4b
Show file tree
Hide file tree
Showing 25 changed files with 346 additions and 294 deletions.
34 changes: 20 additions & 14 deletions sentinel/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,26 @@ const waitForApi = () => new Promise(resolve => {
});

logger.info('Running server checks...');
serverChecks
.check(environment.couchUrl)
.then(waitForApi)
.then(() => {
// Even requiring this boots translations, so has to be required after
// api has booted

(async () => {
try {
await serverChecks.check(environment.couchUrl);
await waitForApi();

const config = require('./src/config');
return config.init().then(() => {
require('./src/schedule').init();
logger.info('startup complete.');
});
})
.catch(err => {
logger.error('Fatal error intialising sentinel');
await config.init();

const schedule = require('./src/schedule');
schedule.init();

logger.info('startup complete.');

const processHooks = require('./src/process-hooks');
processHooks.init();

} catch (err) {
logger.error('Fatal error initialising sentinel');
logger.error('%o', err);
process.exit(1);
});
}
})();
44 changes: 23 additions & 21 deletions sentinel/src/lib/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const MAX_QUEUE_SIZE = 100;

let request;
let processed = 0;
let processing;

const enqueue = change => changeQueue.push(change);

Expand Down Expand Up @@ -100,33 +101,34 @@ changeQueue.drain(() => {
resumeProcessing();
});

/**
* Start listening from the last processed seq. Will restart
* automatically on error.
*/
const listen = () => {
logger.info('Starting transition processing');
processing = true;
changeQueue.resume();
return resumeProcessing();
};

module.exports = {
/**
* Stops listening for changes. Must be restarted manually
* by calling listen.
*/
const cancel = () => {
logger.info('Suspending transition processing');
processing = false;
changeQueue.pause();
if (request) {
request.cancel && request.cancel();
request = null;
}
};

/**
* Start listening from the last processed seq. Will restart
* automatically on error.
*/
module.exports = {
listen,
cancel,

/**
* Stops listening for changes. Must be restarted manually
* by calling listen.
*/
cancel: () => {
changeQueue.pause();
if (request) {
request.cancel && request.cancel();
request = null;
}
},

// exposed for testing
_changeQueue: changeQueue,
_transitionsLib: transitionsLib,
_enqueue: enqueue,
toggle: () => processing ? cancel() : listen()
};
13 changes: 13 additions & 0 deletions sentinel/src/process-hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const feed = require('./lib/feed');
const schedule = require('./schedule');

module.exports = {
init: () => {
process.on('SIGUSR1', () => {
feed.toggle();
});
process.on('SIGUSR2', () => {
schedule.runTasks();
});
}
};
1 change: 1 addition & 0 deletions sentinel/src/schedule/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,4 @@ exports.init = () => {
runTasks();
interval = setInterval(runTasks, RUN_EVERY_MS);
};
exports.runTasks = runTasks;
5 changes: 1 addition & 4 deletions sentinel/src/transitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,5 @@ module.exports = {
/**
* Loads the transitions and starts watching for db changes.
*/
loadTransitions: loadTransitions,

// exposed for testing
_transitionsLib: transitionsLib,
loadTransitions,
};
Loading

0 comments on commit 4090a4b

Please sign in to comment.