Skip to content

Commit 150b537

Browse files
fetchSystemData changed with fetchSystemDataWithRetries
1 parent a0075a6 commit 150b537

3 files changed

Lines changed: 60 additions & 7 deletions

File tree

src/routes/error_handlers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ def method_not_allowed(e):
2424
"""Handle 405 Method Not Allowed error."""
2525
return "Method not allowed", 405
2626

27+
@app.errorhandler(429)
28+
def ratelimit_handler(e):
29+
return "Too many requests, please try again later.", 429
30+
2731
@app.errorhandler(500)
2832
def internal_server_error(e):
2933
"""Handle 500 Internal Server Error."""

src/static/js/refreshCardData.js

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,66 @@ async function postRefreshInterval(newInterval) {
4848
}
4949

5050
// Fetch system data from a given API endpoint
51-
async function fetchSystemData(apiEndpoint) {
51+
// Note: depcrecated in favor of fetchSystemDataWithRetries
52+
53+
// async function fetchSystemData(apiEndpoint) {
54+
// try {
55+
// const response = await fetch(apiEndpoint);
56+
// return await response.json();
57+
// } catch (error) {
58+
// console.error(`Error fetching ${apiEndpoint}:`, error);
59+
// return null;
60+
// }
61+
// }
62+
63+
async function fetchSystemDataWithRetries(apiEndpoint, retries = 3, delay = 2000) {
64+
for (let i = 0; i < retries; i++) {
65+
try {
66+
const response = await fetch(apiEndpoint);
67+
if (response.ok) {
68+
return await response.json();
69+
} else {
70+
const errorText = await response.text();
71+
console.error(`Error fetching ${apiEndpoint} - Status: ${response.status}, Response: ${errorText}`);
72+
}
73+
} catch (error) {
74+
console.error(`Fetch attempt ${i + 1} failed: ${error.message}`);
75+
}
76+
if (i < retries - 1) {
77+
await new Promise(res => setTimeout(res, delay));
78+
}
79+
}
80+
return null; // Return null after exhausting retries
81+
}
82+
83+
84+
let requestQueue = [];
85+
let isRequestInProgress = false;
86+
87+
async function processQueue() {
88+
if (isRequestInProgress || requestQueue.length === 0) return;
89+
isRequestInProgress = true;
90+
91+
const { apiEndpoint, resolve, reject } = requestQueue.shift();
5292
try {
53-
const response = await fetch(apiEndpoint);
54-
return await response.json();
93+
const data = await fetchSystemDataWithRetries(apiEndpoint);
94+
resolve(data);
5595
} catch (error) {
56-
console.error(`Error fetching ${apiEndpoint}:`, error);
57-
return null;
96+
reject(error);
5897
}
98+
99+
isRequestInProgress = false;
100+
processQueue(); // Continue processing the next request
59101
}
60102

103+
function queueRequest(apiEndpoint) {
104+
return new Promise((resolve, reject) => {
105+
requestQueue.push({ apiEndpoint, resolve, reject });
106+
processQueue(); // Start processing if not already in progress
107+
});
108+
}
109+
110+
61111
// Update card content and bar width based on fetched data
62112
function updateCard(cardSelector, dataKey, data, unit = '', barSelector = null, maxDataKey = null) {
63113
const cardElement = document.querySelector(cardSelector);
@@ -109,7 +159,7 @@ async function refreshCardText(cardSelector, dataKey, data) {
109159

110160
// Refresh all card data
111161
async function refreshData() {
112-
const data = await fetchSystemData('/api/system-info');
162+
const data = await queueRequest('/api/system-info');
113163
if (!data) return;
114164

115165
updateCard('.bg-disk', 'disk_percent', data, '%', '.disk-bar');

src/utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,6 @@ def _get_system_info():
470470
memory_available = get_cached_value("memory_available", get_memory_available)
471471
# Gathering fresh system information
472472
battery_data = check_battery_status()
473-
print("Battery Data: ", battery_data)
474473
memory_info = psutil.virtual_memory()
475474
disk_info = psutil.disk_usage('/')
476475
network_sent, network_received = get_network_io()

0 commit comments

Comments
 (0)