Skip to content
Snippets Groups Projects
Commit a75659ea authored by Ferenc Schulcz's avatar Ferenc Schulcz
Browse files

Make initialization more resilient

parent 10f94c52
No related branches found
No related tags found
No related merge requests found
const https = require('https');
const express = require('express');
// Initialize
// --------------------------------------------------------
const app = express();
const { error } = require('console');
const config = require('./config');
config.init();
const db = require('./database');
db.init();
const knotdns = require('./knotdns');
// Sync records from master
// --------------------------------------------------------
// number of times to try syncing DNS records from master before giving up
const retryMax = 5;
console.log("Syncing records from " + config.config().master.address + "/ ...");
// Synchronizes current DNS records from master
function syncRecords() {
return new Promise((resolve, reject) => {
https.get(config.config().master.address + '/dyndns-list?id=' + config.config().master.myId + '&key=' + config.config().master.key, (res) => {
if (res.statusCode > 299) {
process.exit(2);
const errormsg = 'Bad HTTP status code from ' + config.config().master.address + ' : ' + res.statusCode;
reject(errormsg);
return;
}
......@@ -48,29 +45,34 @@ https.get(config.config().master.address + '/dyndns-list?id=' + config.config().
await knotdns.updateRecord(domain, ip);
console.log("Registering domain " + domain + " to " + ip);
}
};
}
resolve();
return;
}
loadRecords(records);
} catch (error) {
console.error("Error parsing records from master: ", error);
process.exit(1);
const errormsg = "Error parsing records from master: " + error;
reject(errormsg);
return;
}
})
})
});
}).on('error', (error) => {
console.error("Could not sync records from master: ", error);
process.exit(1);
const errormsg = "Could not sync records from master: " + error;
reject(errormsg);
return;
});
})
}
// Register and serve endpoints
// --------------------------------------------------------
// Start HTTP server and process record updates
function handleUpdates() {
return new Promise((resolve, reject) => {
const registerMW = require('./middleware/registerMW');
const replyMW = require('./middleware/replyMW');
const updateMW = require('./middleware/updateMW');
const { error } = require('console');
const knotdns = require('./knotdns');
// register a new dyndns record
app.get('/register', registerMW(), replyMW());
......@@ -82,4 +84,44 @@ const server_port = config.config().listenPort;
const server_domain = config.config().listenAddress;
app.listen(server_port, server_domain, function () {
console.log(`This server is listening on http://${server_domain}:${server_port}`);
resolve();
});
})
}
// Try syncing from master a few times and then serve DNS update requests
function syncAndRun(retries) {
console.log("Syncing records from " + config.config().master.address + "/ ...");
syncRecords().then(() => {
handleUpdates();
})
.catch((errormsg) => {
console.error(errormsg);
if (retries < retryMax) {
console.error("Retrying in 1 min.");
setTimeout(() => {
syncAndRun(retries + 1);
}, 60000);
} else {
console.error("Giving up.");
process.exit(1);
}
})
}
// Initialize
// --------------------------------------------------------
const app = express();
config.init();
db.init();
// Sync records from master then serve requests
// --------------------------------------------------------
var retries = 0;
syncAndRun(retries);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment