refactors to use redis & celery
This commit is contained in:
@@ -12,6 +12,7 @@ export class ScraperUtils {
|
||||
|
||||
this.serverTime = null;
|
||||
this.endTime = null;
|
||||
this.pollInterval = null; // Add this line
|
||||
|
||||
this.init();
|
||||
}
|
||||
@@ -20,7 +21,6 @@ export class ScraperUtils {
|
||||
this.showLoadingIndicator();
|
||||
|
||||
try {
|
||||
// Ensure each function runs only once
|
||||
await Promise.all([
|
||||
this.updateServerTime(),
|
||||
this.checkScrapingStatus()
|
||||
@@ -29,31 +29,41 @@ export class ScraperUtils {
|
||||
console.error("Error during initialization:", error);
|
||||
}
|
||||
|
||||
// Ensure end time is fetched only if scraping is active
|
||||
if (this.endTime === null) {
|
||||
try {
|
||||
await this.fetchEndTime();
|
||||
} catch (error) {
|
||||
console.error("Error fetching end time:", error);
|
||||
// Start polling for status updates
|
||||
this.startPolling();
|
||||
|
||||
// Only start the clock and wait for end time if scraping is active
|
||||
if (this.activityIndicator.textContent === 'Active') {
|
||||
if (!this.endTime) {
|
||||
try {
|
||||
await this.fetchEndTime();
|
||||
} catch (error) {
|
||||
console.error("Error fetching end time:", error);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.serverTime && this.endTime) {
|
||||
this.startClock();
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure UI is only updated once everything is ready
|
||||
if (this.serverTime && this.endTime) {
|
||||
this.startClock();
|
||||
this.hideLoadingIndicator();
|
||||
} else {
|
||||
console.warn("Delaying hiding the loading indicator due to missing data...");
|
||||
const checkDataInterval = setInterval(() => {
|
||||
if (this.serverTime && this.endTime) {
|
||||
clearInterval(checkDataInterval);
|
||||
this.startClock();
|
||||
this.hideLoadingIndicator();
|
||||
}
|
||||
}, 500);
|
||||
|
||||
// Hide loading indicator regardless of scraping status
|
||||
this.hideLoadingIndicator();
|
||||
}
|
||||
|
||||
startPolling() {
|
||||
// Poll every 2 seconds
|
||||
this.pollInterval = setInterval(async () => {
|
||||
await this.checkScrapingStatus();
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
stopPolling() {
|
||||
if (this.pollInterval) {
|
||||
clearInterval(this.pollInterval);
|
||||
this.pollInterval = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
showLoadingIndicator() {
|
||||
this.statusContainer.classList.remove('d-none');
|
||||
@@ -79,9 +89,7 @@ export class ScraperUtils {
|
||||
this.activityIndicator.classList.add('text-bg-success');
|
||||
this.activityIndicator.textContent = 'Active';
|
||||
|
||||
console.log(`Scraping is active until ${data.end_time} TCT`);
|
||||
|
||||
// Only call fetchEndTime() if endTime is not already set
|
||||
// Fetch end time if we don't have it yet
|
||||
if (!this.endTime) {
|
||||
await this.fetchEndTime();
|
||||
}
|
||||
@@ -98,6 +106,9 @@ export class ScraperUtils {
|
||||
|
||||
this.endTimeElement.classList.add('d-none');
|
||||
this.timeLeftElement.classList.add('d-none');
|
||||
|
||||
// Reset end time when inactive
|
||||
this.endTime = null;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error checking scraping status:', error);
|
||||
@@ -177,4 +188,16 @@ export class ScraperUtils {
|
||||
const seconds = totalSeconds % 60;
|
||||
return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
// Add cleanup method
|
||||
cleanup() {
|
||||
this.stopPolling();
|
||||
}
|
||||
}
|
||||
|
||||
// Add event listener for page unload
|
||||
window.addEventListener('unload', () => {
|
||||
if (window.scraperUtils) {
|
||||
window.scraperUtils.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user