Skip to content

Commit

Permalink
test: download backup file
Browse files Browse the repository at this point in the history
  • Loading branch information
alihm committed Jan 9, 2025
1 parent 800be06 commit ca0809b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
51 changes: 50 additions & 1 deletion ClusterOperator/Operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
const timer = require('timers/promises');
const fs = require('fs');
const net = require('net');
const https = require('https');
const { io } = require('socket.io-client');
const missingQueryBuffer = require('memory-cache');
const BackLog = require('./Backlog');
Expand Down Expand Up @@ -68,6 +69,8 @@ class Operator {

static ghosted = false;

static downloadingBackup = false;

static masterQueue = [];

static ticket = 0;
Expand Down Expand Up @@ -398,6 +401,49 @@ class Operator {
}
}

/**
* [downloadBackup]
*
*/
static async downloadBackup(filename, filesize) {
return new Promise((resolve, reject) => {
const fileUrl = `http://${this.masterNode}:${config.debugUIPort}/${filename}/${filesize}`;
const filePath = `./dumps/${filename}.sql`;
log.info(`downloading backup from ${fileUrl}`);
const file = fs.createWriteStream(filePath);
const request = https.get(fileUrl, (response) => {
if (response.statusCode !== 200) {
log.error(`Failed to download file. Status code: ${response.statusCode}`);
fs.unlink(filePath, (err) => {
if (err) {
log.error(`Error deleting partially created file: ${err}`);
}
});
reject(new Error(`Failed to download file. Status code: ${response.statusCode}`));
return;
}

response.pipe(file);

file.on('finish', () => {
log.info('download finished.');
file.close();
resolve(); // Resolve the promise after successful download
});
});

request.on('error', (error) => {
log.error(`Error downloading file: ${error.message}`);
fs.unlink(filePath, (err) => {
if (err) {
log.error(`Error deleting partially created file: ${err}`);
}
});
reject(error); // Reject the promise with the error
});
});
}

/**
* [comperssBacklog]
*
Expand All @@ -412,7 +458,10 @@ class Operator {
let tries = 0;
while (!fs.existsSync(`./dumps/${backupFilename}.sql`)) {
// reset master if file is not being replicated.
if (tries > 20) {
if (tries === 20) {
this.downloadBackup(backupFilename, filesize);
}
if (tries > 30) {
if (this.masterWSConn) {
try {
this.masterWSConn.removeAllListeners();
Expand Down
14 changes: 14 additions & 0 deletions ClusterOperator/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,20 @@ function startUI() {
res.status(403).send('Bad Request');
}
});
app.get('/getbackupfile2/:filename/:filesize', async (req, res) => {
const { filename } = req.params;
const { filesize } = req.params;
if (fs.existsSync(`./dumps/${sanitize(filename)}.sql`) && fs.statSync(`./dumps/${sanitize(filename)}.sql`).size === sanitize(filesize)) {
res.download(path.join(__dirname, `../dumps/${sanitize(filename)}.sql`), `${sanitize(filename)}.sql`, (err) => {
if (err) {
// Handle errors, such as file not found
res.status(404).send('File not found');
}
});
} else {
res.status(403).send('Bad Request');
}
});
app.post('/upload-sql', async (req, res) => {
if (authUser(req)) {
if (!req.files || !req.files.sqlFile) {
Expand Down

0 comments on commit ca0809b

Please sign in to comment.