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

ADD: Stereum Log Backup #2127

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
43 changes: 43 additions & 0 deletions launcher/src/backend/LogFileBackup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { app } from "electron";
import * as fs from "fs";

export class LogFileBackup {
async backupLogFiles() {
let logPath = app.getPath("userData");
let backupPath;
if (process.platform != "darwin") {
logPath += "/logs/";
}
backupPath = logPath + "backups/";

if (!fs.existsSync(backupPath)) {
fs.mkdirSync(backupPath);
}

fs.copyFileSync(`${logPath}main.log`, `${backupPath}main-${Date.now()}.log`);
}

async deleteLogBackups(maxLogs) {
let backupLogs = [];
let logPath = app.getPath("userData");
let backupPath;
if (process.platform != "darwin") {
logPath += "/logs/";
}
backupPath = logPath + "backups/";

fs.readdir(backupPath, (err, files) => {
files.forEach((file) => {
backupLogs.push(file);
});
if (backupLogs.length > maxLogs) {
backupLogs.reverse();
for (let i = maxLogs; i < backupLogs.length; i++) {
fs.unlinkSync(backupPath + backupLogs[i], (err) => {
if (err) throw err;
});
}
}
});
}
}
7 changes: 7 additions & 0 deletions launcher/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ConfigManager } from "./backend/ConfigManager.js";
import { AuthenticationService } from "./backend/AuthenticationService.js";
import { TekuGasLimitConfig } from "./backend/TekuGasLimitConfig.js";
import { SSHService } from "./backend/SSHService.js";
import { LogFileBackup } from "./backend/LogFileBackup.js";
import path from "path";
import { readFileSync } from "fs";
import url from "url";
Expand All @@ -33,6 +34,8 @@ const tekuGasLimitConfig = new TekuGasLimitConfig(nodeConnection);
const sshService = new SSHService();
const { globalShortcut } = require("electron");
const log = require("electron-log");
const logFileBackup = new LogFileBackup();
logFileBackup.backupLogFiles();
const stereumUpdater = new StereumUpdater(log, createWindow, isDevelopment);
stereumUpdater.initUpdater();
log.transports.console.level = process.env.LOG_LEVEL || "info";
Expand Down Expand Up @@ -113,6 +116,10 @@ ipcMain.handle("setIdleTime", async (event, arg) => {
return await monitoring.setIdleTime(arg);
});

ipcMain.handle("deleteLogBackups", async (event, arg) => {
return await logFileBackup.deleteLogBackups(arg);
});

// userData storage
ipcMain.handle("readConfig", async () => {
return storageService.readConfig();
Expand Down
2 changes: 2 additions & 0 deletions launcher/src/components/UI/setting-page/SettingScreen.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import OutputOptions from "./components/OutputOptions.vue";
import LanguageBtn from "./components/LanguageBtn.vue";
import CreditButtons from "./section/CreditButtons.vue";
import IdleTimer from "./components/IdleTimer.vue";
import LogBackups from "./components/LogBackups.vue";
import IdleTimerTime from "./components/IdleTimerTime.vue";
import { ref, computed, onMounted } from "vue";
import CreditBtn from "./components/CreditBtn.vue";
Expand Down Expand Up @@ -105,6 +106,7 @@ const itemConfigurations = computed(() => {
{ title: "Credits", component: CreditButtons },
{ title: "Idle Timeout", component: IdleTimer },
{ title: "Idle Timeout Time (in minutes)", component: IdleTimerTime },
{ title: "Stereum Log Backups", component: LogBackups },
];
} else if (mainBox.value === "audio") {
items = [
Expand Down
75 changes: 75 additions & 0 deletions launcher/src/components/UI/setting-page/components/LogBackups.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<template>
<div class="LogBacksups-parent w-full h-full justify-center items-center flex">
<input
type="number"
:value="footerStore.logBackups"
class="lang-btn-parent w-full h-full bg-[#33393E] rounded-md flex justify-center items-center cursor-pointer border border-[#33393E] uppercase pl-3 pr-3 text-gray-200 text-base"
@change="onChange($event)"
/>
</div>
</template>

<script setup>
import { onMounted } from "vue";
import ControlService from "@/store/ControlService";
import { useFooter } from "@/store/theFooter";

const footerStore = useFooter();

const checkSettings = async () => {
try {
const savedConfig = await ControlService.readConfig();
if (typeof savedConfig.logBackups.value !== "undefined") {
footerStore.logBackups = savedConfig.logBackups.value;
} else {
updateSettings(5);
}
} catch (error) {
console.error("Failed to load saved settings:", error);
}
};

const updateSettings = async (logBackup) => {
try {
const prevConf = await ControlService.readConfig();
const conf = {
...prevConf,
logBackups: { value: logBackup },
};
await ControlService.writeConfig(conf);
useFooter.logBackups = logBackup;
checkSettings();
} catch (error) {
console.error("Failed to update settings:", error);
}
};

const onChange = async (event) => {
if (event.target.value == "" || event.target.value < 3) {
event.target.value = 3;
}
updateSettings(event.target.value);
};

onMounted(() => {
checkSettings();
});
</script>

<style scoped>
input[type="number"] {
text-align: center;
}

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}

.LogBacksups-parent {
display: flex;
align-items: center;
justify-content: center;
}
</style>
5 changes: 5 additions & 0 deletions launcher/src/components/base/BaseLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ const checkSettings = async () => {
await ControlService.idleTimerCheck(false);
}
}
if (typeof savedConfig.logBackups.value !== "undefined") {
await ControlService.deleteLogBackups(savedConfig.logBackups.value);
} else {
await ControlService.deleteLogBackups(3);
}
} catch (error) {
console.error("Failed to load saved settings:", error);
}
Expand Down
4 changes: 4 additions & 0 deletions launcher/src/store/ControlService.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class ControlService extends EventEmitter {
return await this.promiseIpc.send("setIdleTime", args);
}

async deleteLogBackups(args) {
return await this.promiseIpc.send("deleteLogBackups", args);
}

async readConfig() {
return await this.promiseIpc.send("readConfig");
}
Expand Down
1 change: 1 addition & 0 deletions launcher/src/store/theFooter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const useFooter = defineStore("theFooter", {
volState: false,
idleTimer: false,
idleTimerTime: 5,
logBackups: 3,
};
},
getters: {},
Expand Down