Optimisation du refresh cache avec batch processing
Problème : Le refresh cache de tous les projets timeout côté serveur à cause du trop grand nombre de projets à traiter en une seule requête. Solution : Batch processing avec indicateur de progression - Backend : traite 10 projets par batch avec offset/limit - Frontend : fait plusieurs requêtes successives et affiche la progression - Timeout réduit à 60s par batch au lieu de illimité - Bouton désactivé pendant le traitement - Ajout invalidateNotificationsCache() pour vider aussi ce cache Affichage : "15/50 (30%)" pendant le traitement, puis "Terminé (50)" Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
86db1f5a0c
commit
a57b0c203a
2 changed files with 119 additions and 20 deletions
|
|
@ -7,6 +7,7 @@
|
|||
:icon="icon"
|
||||
:title="title"
|
||||
@click="refreshCache()"
|
||||
:disabled="isProcessing"
|
||||
>{{ text }}</k-button
|
||||
>
|
||||
</div>
|
||||
|
|
@ -24,6 +25,8 @@ const { pageUri, pageStatus, lastCacheUpdate } = defineProps({
|
|||
const text = ref("Rafraîchir");
|
||||
const icon = ref("refresh");
|
||||
const theme = ref("aqua-icon");
|
||||
const isProcessing = ref(false);
|
||||
|
||||
const title = computed(() => {
|
||||
return lastCacheUpdate?.length > 0
|
||||
? "Dernière mise à jour : " + lastCacheUpdate
|
||||
|
|
@ -31,25 +34,89 @@ const title = computed(() => {
|
|||
});
|
||||
|
||||
async function refreshCache() {
|
||||
text.value = "En cours…";
|
||||
isProcessing.value = true;
|
||||
icon.value = "loader";
|
||||
theme.value = "orange-icon";
|
||||
|
||||
// Pour les projets multiples (batch processing)
|
||||
if (pageUri === 'projects') {
|
||||
await refreshAllProjects();
|
||||
} else {
|
||||
await refreshSingleProject();
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshAllProjects() {
|
||||
let offset = 0;
|
||||
const limit = 10; // 10 projets par batch
|
||||
let hasMore = true;
|
||||
let total = 0;
|
||||
|
||||
try {
|
||||
while (hasMore) {
|
||||
const init = {
|
||||
method: "POST",
|
||||
"Content-Type": "application/json",
|
||||
body: JSON.stringify({
|
||||
pageUri: 'projects',
|
||||
offset,
|
||||
limit
|
||||
}),
|
||||
};
|
||||
|
||||
const res = await fetch("/refresh-cache.json", init);
|
||||
const json = await res.json();
|
||||
|
||||
if (json.status === "error") {
|
||||
throw new Error(json.message);
|
||||
}
|
||||
|
||||
total = json.total;
|
||||
hasMore = json.hasMore;
|
||||
offset = json.nextOffset;
|
||||
|
||||
// Mise à jour de la progression
|
||||
const progress = Math.round((json.processed / json.total) * 100);
|
||||
text.value = `${json.processed}/${json.total} (${progress}%)`;
|
||||
|
||||
console.log(`Batch terminé : ${json.processed}/${json.total} projets`);
|
||||
}
|
||||
|
||||
// Succès
|
||||
text.value = `Terminé (${total})`;
|
||||
icon.value = "check";
|
||||
theme.value = "green-icon";
|
||||
|
||||
setTimeout(() => {
|
||||
location.href = location.href;
|
||||
}, 1500);
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
text.value = "Erreur";
|
||||
icon.value = "alert";
|
||||
theme.value = "red-icon";
|
||||
isProcessing.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshSingleProject() {
|
||||
text.value = "En cours…";
|
||||
|
||||
const init = {
|
||||
method: "POST",
|
||||
"Content-Type": "application/json",
|
||||
body: JSON.stringify({ pageUri }),
|
||||
};
|
||||
|
||||
const res = await fetch("/refresh-cache.json", init);
|
||||
const json = await res.json();
|
||||
try {
|
||||
const res = await fetch("/refresh-cache.json", init);
|
||||
const json = await res.json();
|
||||
|
||||
if (json.status === "error") {
|
||||
throw new Error(json.message);
|
||||
}
|
||||
|
||||
if (json.status === "error") {
|
||||
console.error(json);
|
||||
text.value = "Erreur";
|
||||
icon.value = "alert";
|
||||
theme.value = "red-icon";
|
||||
} else {
|
||||
console.log(json);
|
||||
text.value = "Terminé";
|
||||
icon.value = "check";
|
||||
|
|
@ -58,6 +125,13 @@ async function refreshCache() {
|
|||
setTimeout(() => {
|
||||
location.href = location.href;
|
||||
}, 1500);
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
text.value = "Erreur";
|
||||
icon.value = "alert";
|
||||
theme.value = "red-icon";
|
||||
isProcessing.value = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue