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

resolve #56 #61

Open
wants to merge 3 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
.vscode
node_modules
dumps/
2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"axios": "^1.4.0",
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"cron": "^3.1.7",
"cross-spawn": "^7.0.3",
"date-fns": "^2.30.0",
"dotenv": "^16.0.3",
"fastify": "^4.17.0",
Expand Down
19 changes: 19 additions & 0 deletions backend/src/cronjob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { CronJob } from "cron";
import { dumpDatabase, removeOldDumps } from "@/utils/database";


export const dumpDatabaseJob = CronJob.from({
start: false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pourquoi pas le start directement 🤔 ?

cronTime: "0 0 4 * * *",
onComplete: null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Du coup y'a pas besoin de cette propriété, si ?

onTick: dumpDatabase,
timeZone: "Europe/Paris"
});

export const deleteOldDumpJob = CronJob.from({
start: false,
cronTime: "0 30 4 * * *",
onComplete: null,
onTick: removeOldDumps,
timeZone: "Europe/Paris"
});
6 changes: 6 additions & 0 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import APIHandler from "@/api";
import StaticHandler from "@/static";
import UploadHandler from "@/upload";
import PageHandler from "@/page";
import * as cron from "@/cronjob";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Si tu les start directement, tu peux juste import le fichier

import "@/cronjob";



//import NotFoundHandler from "@/notFound";

async function init() {
Expand Down Expand Up @@ -50,3 +53,6 @@ process.on("SIGINT", async function() {
await closeDatabase();
process.exit(1);
});

cron.dumpDatabaseJob.start();
cron.deleteOldDumpJob.start();
28 changes: 28 additions & 0 deletions backend/src/utils/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { promises as pfs } from "node:fs";
import * as cs from "cross-spawn";

const BACKUP_RETENTION_DAYS = 30;

export async function dumpDatabase() {
const dumpDate = new Date().toLocaleDateString("fr-fr",{ day: "2-digit", month: "2-digit", year: "numeric" }).replaceAll("/", "-");
const folderName = `../dumps/${ dumpDate }`;
await pfs.mkdir(folderName, { recursive: true });

const child = cs.spawn("mongodump", ["--db", "sgnw", "--out", `../dumps/${ dumpDate }`], { stdio: "inherit" });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ça serait peut être pas mal de mettre le path vers mongodump dans le .env, je suis pas sûr qu'on veuille forcément le mettre dans le Path 🤔

}

export async function removeOldDumps() {
const currentDate = new Date();
const folderName = "../dumps/";

const dirs = await pfs.readdir(folderName,{ encoding: "utf-8" });
dirs.forEach(async (dir) => {
const [day, month, year] = dir.split("-").map(Number);
const dumpDate = new Date(year, month - 1, day);
const Difference_In_Days = Math.floor((currentDate.getTime() - dumpDate.getTime()) / (1000 * 3600 * 24));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Il y a le package date-fns qui est installé, je sais pas si c'est worth, mais jette un oeil 🤔

https://date-fns.org/v4.1.0/docs/differenceInDays


if (Difference_In_Days > BACKUP_RETENTION_DAYS) {
pfs.rm(`${ folderName }/${ dir }`, { recursive: true });
}
});
}
2 changes: 1 addition & 1 deletion backend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
"exclude": [
"node_modules"
]
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pourquoi y'a cette modif 🤔 ? L'eslint devrait garder les lignes vide à la fin normalement, non ?

Copy link
Contributor Author

@Kotsudes Kotsudes Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'ai enfin compris. J'ai ajouté ces lignes à mon .vscode dans website

"eslint.workingDirectories": [
        {
            "mode": "auto"
        }
    ]

25 changes: 25 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.