@@ -7,7 +7,6 @@ const fs = require('fs');
77const https = require('https');
88const Root = require('../controllers/root').Root;
99const IDM = require('../lib/idm.js').IDM;
10- const async = require('async');
1110const errorhandler = require('errorhandler');
1211
1312config.azf = config.azf || {};
@@ -77,54 +76,20 @@ for (const p in config.public_paths) {
7776
7877app.all('/*', Root.pep);
7978
80- let retries = 0;
81- let idmConnected = false;
82-
83- function retryCheck() {
84- return !idmConnected && retries < 10;
85- }
8679
8780function connectIDM(callback) {
8881 IDM.authenticate(
8982 function(token) {
9083 log.info('Success authenticating PEP proxy. Proxy Auth-token: ', token);
91- idmConnected = true;
9284 callback();
9385 },
9486 function(status, e) {
9587 log.error('Error in IDM communication', e);
96- callback();
88+ callback(e );
9789 }
9890 );
9991}
10092
101- function tryCreateConnection(callback) {
102- const seconds = 5;
103-
104- retries++;
105-
106- if (retries === 1) {
107- log.info('Starting PEP proxy in port ' + port + '. IdM authentication ...');
108- connectIDM(callback);
109- } else {
110- log.info('Waiting %d seconds before attempting again.', seconds);
111- setTimeout(() => {
112- connectIDM(callback);
113- }, seconds * 1000);
114- }
115- }
116-
117- function createConnectionHandler(error) {
118- if (idmConnected) {
119- log.info('Success authenticating PEP proxy.');
120- } else {
121- log.error('Error found after [%d] attempts: %s', retries, error);
122- process.exit(1);
123- }
124- }
125-
126- async.whilst(retryCheck, tryCreateConnection, createConnectionHandler);
127-
12893if (config.https.enabled === true) {
12994 const options = {
13095 key: fs.readFileSync(config.https.key_file),
@@ -139,3 +104,28 @@ if (config.https.enabled === true) {
139104} else {
140105 app.listen(app.get('port'));
141106}
107+
108+ let retry = 20;
109+ function connect() {
110+ const connect_with_retry = () => {
111+ connectIDM((err) => {
112+ if (err) {
113+ retry--;
114+ if (retry === 0) {
115+ log.error('Error found after [%d] attempts: %s', 20, error);
116+ process.exit(1);
117+ } else {
118+ console.log('retry after 5 seconds.');
119+ //eslint-disable-next-line snakecase/snakecase
120+ setTimeout(connect_with_retry, 5000);
121+ }
122+ } else {
123+ log.info('Success authenticating PEP proxy.');
124+ }
125+ });
126+ };
127+ connect_with_retry();
128+ }
129+
130+ log.info('Starting PEP proxy in port ' + port + '. IdM authentication ...');
131+ connect();
0 commit comments