Skip to content

Commit

Permalink
fix: éviter les exécutions simultanées de la tâche de récupération en…
Browse files Browse the repository at this point in the history
… arrière-plan + récupération des nouvelles informations 1 par 1 + quelques optimisations
  • Loading branch information
Kgeek33 committed Jan 22, 2025
1 parent c99ab28 commit 462a90a
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions src/background/BackgroundTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BackgroundFetchResult } from "expo-background-fetch";
import { expoGoWrapper } from "@/utils/native/expoGoAlert";

import { fetchNews } from "./data/News";
import { log, error, warn, info } from "@/utils/logger/logger";
import { log, error } from "@/utils/logger/logger";
import { getAccounts, getSwitchToFunction } from "./utils/accounts";
import { fetchHomeworks } from "./data/Homeworks";
import { fetchGrade } from "./data/Grades";
Expand Down Expand Up @@ -39,19 +39,22 @@ notifee.onBackgroundEvent(async ({ type, detail }) => {
}
});

/**
* Background fetch function that fetches all the data
* @warning This task should not last more than 30 seconds
* @returns BackgroundFetchResult.NewData
*/
const backgroundFetch = () => {
let isBackgroundFetchRunning = false;

const backgroundFetch = async () => {
if (isBackgroundFetchRunning) {
log("⚠️ Background fetch already running. Skipping...", "BackgroundEvent");
return BackgroundFetchResult.NoData;
}

isBackgroundFetchRunning = true;
log("Running background fetch", "BackgroundEvent");

try {
const accounts = getAccounts();
const switchTo = getSwitchToFunction();

accounts.forEach(async (account) => {
for (const account of accounts) {
await switchTo(account);

await fetchNews();
Expand All @@ -60,44 +63,42 @@ const backgroundFetch = () => {
await fetchLessons();
await fetchAttendance();
await fetchEvaluation();
});
}

log("✅ Finish background fetch", "BackgroundEvent");
return BackgroundFetchResult.NewData;
} catch (ERRfatal) {
error(`❌ Task failed: ${ERRfatal}`, "BackgroundEvent");
return BackgroundFetchResult.Failed;
} finally {
isBackgroundFetchRunning = false;
}
};

TaskManager.defineTask("background-fetch", backgroundFetch);

const registerBackgroundTasks = async () => {
await TaskManager.isTaskRegisteredAsync("background-fetch").then(
async (isRegistered) => {
if (!isRegistered) {
expoGoWrapper(async () => {
await BackgroundFetch.registerTaskAsync("background-fetch", {
minimumInterval: 60 * 15, // 15 minutes
stopOnTerminate: false, // Maintenir après fermeture (Android)
startOnBoot: true, // Redémarrer au démarrage (Android)
});

log("✅ Background task registered", "BackgroundEvent");
});
} else {
warn("⚠️ Background task already registered, unregister task...", "BackgroundEvent");
await BackgroundFetch.unregisterTaskAsync("background-fetch");
info("🔁 Re-register background task...", "BackgroundEvent");
registerBackgroundTasks();
}
}
);
};

const unsetBackgroundFetch = async () => {
await BackgroundFetch.unregisterTaskAsync("background-fetch");
log("✅ Background task unregistered", "BackgroundEvent");
};

const registerBackgroundTasks = async () => {
const isRegistered = await TaskManager.isTaskRegisteredAsync("background-fetch");

if (isRegistered) {
log("⚠️ Background task already registered. Unregister background task.", "BackgroundEvent");
await unsetBackgroundFetch();
}

expoGoWrapper(async () => {
await BackgroundFetch.registerTaskAsync("background-fetch", {
minimumInterval: 60 * 15,
stopOnTerminate: false,
startOnBoot: true,
});

log("✅ Background task registered", "BackgroundEvent");
});
};

export { registerBackgroundTasks, unsetBackgroundFetch };

0 comments on commit 462a90a

Please sign in to comment.