Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gateway backups #568

Merged
merged 2 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/config/scheduler-jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { EVENTS } = require('../utils/constants');
const jobs = [
{
name: 'backup-gateway',
frequencyInSeconds: 24 * 60 * 60,
frequencyInSeconds: 2 * 60 * 60, // we check every 2 hour if needed, but it will backup only once a day
event: EVENTS.GATEWAY.CREATE_BACKUP,
},
{
Expand Down
33 changes: 33 additions & 0 deletions server/lib/gateway/gateway.checkIfBackupNeeded.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const logger = require('../../utils/logger');
/**
* @description Check if a backup is needed
* @example
* checkIfBackupNeeded();
*/
async function checkIfBackupNeeded() {
if (!this.connected) {
logger.info(`Instance not connected to Gladys Gateway, not backing up.`);
return;
}
const backups = await this.getBackups();
let shouldBackup = false;
if (backups.length === 0) {
shouldBackup = true;
} else {
const lastBackupTimestamp = new Date(backups[0].created_at).getTime();
const yesterday = new Date().getTime() - 24 * 60 * 60 * 1000;
if (lastBackupTimestamp <= yesterday) {
shouldBackup = true;
}
}
if (shouldBackup) {
logger.info(`Trying to backup instance to Gladys Gateway`);
await this.backup();
} else {
logger.info(`Not backing up instance to Gladys Gateway, last backup is recent.`);
}
}

module.exports = {
checkIfBackupNeeded,
};
29 changes: 3 additions & 26 deletions server/lib/gateway/gateway.init.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const logger = require('../../utils/logger');
const { EVENTS } = require('../../utils/constants');

/**
* @description Init Gladys Gateway.
Expand All @@ -19,6 +20,8 @@ async function init() {
this.handleNewMessage.bind(this),
);
this.connected = true;
// try to backup, if needed
this.event.emit(EVENTS.GATEWAY.CREATE_BACKUP);
}
} catch (e) {
logger.debug(e);
Expand All @@ -31,32 +34,6 @@ async function init() {
} catch (e) {
logger.debug(e);
}

if (!this.connected) {
return;
}

try {
const backups = await this.getBackups();
let shouldBackup = false;
if (backups.length === 0) {
shouldBackup = true;
} else {
const lastBackupTimestamp = new Date(backups[0].created_at).getTime();
const yesterday = new Date().getTime() - 24 * 60 * 60 * 1000;
if (lastBackupTimestamp <= yesterday) {
shouldBackup = true;
}
}
if (shouldBackup) {
logger.debug(`Trying to backup instance to Gladys Gateway`);
await this.backup();
} else {
logger.debug(`Not backing up instance to Gladys Gateway, last backup is recent.`);
}
} catch (e) {
logger.debug(e);
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion server/lib/gateway/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const serverUrl = getConfig().gladysGatewayServerUrl;
const cryptoLib = new WebCrypto();

const { backup } = require('./gateway.backup');
const { checkIfBackupNeeded } = require('./gateway.checkIfBackupNeeded');
const { handleNewMessage } = require('./gateway.handleNewMessage');
const { login } = require('./gateway.login');
const { loginTwoFactor } = require('./gateway.loginTwoFactor');
Expand Down Expand Up @@ -36,14 +37,15 @@ const Gateway = function Gateway(variable, event, system, sequelize, config, use
this.restoreInProgress = false;
this.GladysGatewayClient = GladysGatewayClient;
this.gladysGatewayClient = new GladysGatewayClient({ cryptoLib, serverUrl, logger });
this.event.on(EVENTS.GATEWAY.CREATE_BACKUP, eventFunctionWrapper(this.backup.bind(this)));
this.event.on(EVENTS.GATEWAY.CREATE_BACKUP, eventFunctionWrapper(this.checkIfBackupNeeded.bind(this)));
this.event.on(EVENTS.GATEWAY.RESTORE_BACKUP, eventFunctionWrapper(this.restoreBackupEvent.bind(this)));
this.event.on(EVENTS.SYSTEM.CHECK_UPGRADE, eventFunctionWrapper(this.getLatestGladysVersion.bind(this)));
this.event.on(EVENTS.WEBSOCKET.SEND_ALL, eventFunctionWrapper(this.forwardWebsockets.bind(this)));
this.event.on(EVENTS.WEBSOCKET.SEND, eventFunctionWrapper(this.forwardWebsockets.bind(this)));
};

Gateway.prototype.backup = backup;
Gateway.prototype.checkIfBackupNeeded = checkIfBackupNeeded;
Gateway.prototype.handleNewMessage = handleNewMessage;
Gateway.prototype.login = login;
Gateway.prototype.loginTwoFactor = loginTwoFactor;
Expand Down