Skip to content

Commit

Permalink
Merge pull request #1 from bigbluebutton/f-ftos
Browse files Browse the repository at this point in the history
feat: fault tolerance on start
  • Loading branch information
pedrobmarin authored Feb 11, 2022
2 parents 45bf8bf + 5c051f7 commit 004290d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
50 changes: 43 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,46 @@ const Logger = require('./lib/utils/logger');

const logger = new Logger('bbb-pads');

api.call('checkToken').then(() => {
subscriber.start();
server.start();
monitor.start();
}).catch(() => {
logger.fatal('api key', 'mismatch');
});
const RETRY = 10;

let retries = 0;

const fibonacci = (index) => {
if (index === 1) return 0;

if (index === 2) return 1;

return fibonacci(index - 1) + fibonacci(index - 2);
};

const abort = (error) => {
logger.fatal('abort', error);

process.exit(1);
};

const start = () => {
api.check().then(() => {
api.call('checkToken').then(() => {
subscriber.start();
server.start();
monitor.start();
}).catch(() => abort('key-mismatch'));
}).catch((error) => {
logger.warn('starting', error);

if (retries < RETRY) {
retries++;

setTimeout(() => {
logger.info('start', `retry ${retries} of ${RETRY}`);

start();
}, 1000 * fibonacci(retries));
} else {
abort('retry-exhausted');
}
});
};

start();
13 changes: 12 additions & 1 deletion lib/etherpad/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const logger = new Logger('api');

const { etherpad: settings } = config;

const url = `${settings.scheme}://${settings.host}:${settings.port}/api/${settings.api.version}`;
const baseURL = `${settings.scheme}://${settings.host}:${settings.port}/api`;

const url = `${baseURL}/${settings.api.version}`;

const buildURL = (method, params) => {
let query = `apikey=${settings.api.key}`;
Expand Down Expand Up @@ -53,6 +55,15 @@ const call = (method, params = {}) => {
});
};

const check = () => {
return axios({
method: 'get',
url: baseURL,
responseType: 'json'
});
};

module.exports = {
call,
check,
};

0 comments on commit 004290d

Please sign in to comment.