fix: remove duplicate jobs script block
This commit is contained in:
1 parent
a8464f08dd
commit
fc96558724
1 file changed
-205
-205
@@ -2839,211 +2839,6 @@ def jobs():
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
''')
|
||||
<script>
|
||||
// Track previous job statuses to detect completion
|
||||
let previousJobStatuses = {};
|
||||
|
||||
// Collapsible section functions
|
||||
function toggleZipDetails(element) {
|
||||
const details = element.nextElementSibling;
|
||||
const isVisible = details.style.display === 'block';
|
||||
details.style.display = isVisible ? 'none' : 'block';
|
||||
element.innerHTML = element.innerHTML.replace(isVisible ? ' v' : ' >', isVisible ? ' >' : ' v');
|
||||
}
|
||||
|
||||
function toggleSkippedDetails(element) {
|
||||
const details = element.nextElementSibling;
|
||||
const isVisible = details.style.display === 'block';
|
||||
details.style.display = isVisible ? 'none' : 'block';
|
||||
element.innerHTML = element.innerHTML.replace(isVisible ? ' v' : ' >', isVisible ? ' >' : ' v');
|
||||
}
|
||||
|
||||
function updateJobs() {
|
||||
console.log('[updateJobs] Starting update...');
|
||||
fetch('/api/jobs', { credentials: 'same-origin' })
|
||||
.then(response => {
|
||||
console.log('[updateJobs] Response received:', response.status);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
console.log('[updateJobs] Job data received:', data);
|
||||
const jobs = Object.entries(data);
|
||||
console.log('[updateJobs] Number of jobs:', jobs.length);
|
||||
|
||||
// Check for job completions
|
||||
let hasNewCompletion = false;
|
||||
if (jobs.length > 0) {
|
||||
jobs.forEach(([jobId, job]) => {
|
||||
console.log(`[updateJobs] Job ${jobId}: status=${job.status}, progress=${job.progress || 'none'}`);
|
||||
|
||||
// Check if job just completed
|
||||
if (job.status === 'completed' && previousJobStatuses[jobId] === 'processing') {
|
||||
hasNewCompletion = true;
|
||||
console.log(`[updateJobs] Job ${jobId} just completed!`);
|
||||
}
|
||||
|
||||
// Update previous status
|
||||
previousJobStatuses[jobId] = job.status;
|
||||
});
|
||||
}
|
||||
|
||||
// Redirect to completed jobs if a job just finished
|
||||
if (hasNewCompletion) {
|
||||
console.log('[updateJobs] Redirecting to completed jobs page...');
|
||||
setTimeout(() => {
|
||||
window.location.href = '/completed-jobs';
|
||||
}, 500); // Small delay to show completion briefly
|
||||
return; // Stop processing to avoid UI updates during redirect
|
||||
}
|
||||
|
||||
console.log('[updateJobs] Starting DOM manipulation...');
|
||||
// Find the container where jobs are rendered
|
||||
const body = document.body;
|
||||
const existingJobs = body.querySelectorAll('.job');
|
||||
console.log(`[updateJobs] Found ${existingJobs.length} existing job elements`);
|
||||
|
||||
// Remove existing job elements
|
||||
existingJobs.forEach(job => {
|
||||
console.log('[updateJobs] Removing job element');
|
||||
job.remove();
|
||||
});
|
||||
|
||||
// Remove existing "No jobs yet" messages
|
||||
const noJobsMsgs = body.querySelectorAll('p');
|
||||
noJobsMsgs.forEach(p => {
|
||||
if (p.textContent.includes('No jobs yet')) {
|
||||
console.log('[updateJobs] Removing "no jobs" message');
|
||||
p.remove();
|
||||
}
|
||||
});
|
||||
|
||||
// Find where to insert new jobs (after nav-links div)
|
||||
const navLinks = body.querySelector('.nav-links');
|
||||
const scriptTag = body.querySelector('script');
|
||||
console.log('[updateJobs] navLinks found:', !!navLinks, 'scriptTag found:', !!scriptTag);
|
||||
|
||||
// Add updated job elements
|
||||
const jobEntries = Object.entries(data);
|
||||
console.log(`[updateJobs] Rendering ${jobEntries.length} jobs`);
|
||||
|
||||
if (jobEntries.length > 0) {
|
||||
jobEntries.forEach(([jobId, job]) => {
|
||||
console.log(`[updateJobs] Creating element for job ${jobId}`);
|
||||
try {
|
||||
const jobDiv = document.createElement('div');
|
||||
jobDiv.className = 'job';
|
||||
|
||||
// Build the HTML step by step to avoid syntax errors
|
||||
let jobHTML = `
|
||||
<h3>Job ${jobId.substring(0, 8)}</h3>
|
||||
<p><strong>Status:</strong> <span class="status ${job.status}">${job.status.charAt(0).toUpperCase() + job.status.slice(1)}</span></p>`;
|
||||
|
||||
// Add progress if processing
|
||||
if (job.status === 'processing' && job.progress) {
|
||||
jobHTML += `<p><strong>Progress:</strong> <span class="progress-text">${job.progress}</span></p>`;
|
||||
}
|
||||
|
||||
jobHTML += `<p><strong>Files:</strong> ${job.files ? job.files.length : 0}</p>`;
|
||||
|
||||
// Add ZIP contents if available
|
||||
if (job.zip_contents) {
|
||||
jobHTML += `<p><strong>ZIP Contents:</strong></p>`;
|
||||
Object.entries(job.zip_contents).forEach(([zipName, pdfFiles]) => {
|
||||
jobHTML += `
|
||||
<div class="zip-content">
|
||||
<div class="zip-toggle" onclick="toggleZipDetails(this)">
|
||||
<strong>${zipName}</strong> (${pdfFiles.length} files) >
|
||||
</div>
|
||||
<div class="zip-details">
|
||||
<ul>
|
||||
${pdfFiles.map(pdfFile => `<li>${pdfFile}</li>`).join('')}
|
||||
</ul>
|
||||
</div>
|
||||
</div>`;
|
||||
});
|
||||
}
|
||||
|
||||
// Add download/delete buttons if completed
|
||||
if (job.status === 'completed') {
|
||||
if (job.zip_name) {
|
||||
jobHTML += `
|
||||
<p><a href="/download/${job.zip_name}" class="download-btn">Download ZIP</a>
|
||||
<form method="post" action="/delete/${jobId}" style="display: inline;">
|
||||
<button type="submit" class="delete-btn" onclick="return confirm('Are you sure you want to delete this job?')">Delete Job</button>
|
||||
</form></p>`;
|
||||
} else {
|
||||
jobHTML += `
|
||||
<p><em>No files were successfully processed to create a ZIP archive.</em>
|
||||
<form method="post" action="/delete/${jobId}" style="display: inline;">
|
||||
<button type="submit" class="delete-btn" onclick="return confirm('Are you sure you want to delete this job?')">Delete Job</button>
|
||||
</form></p>`;
|
||||
}
|
||||
|
||||
// Add skipped files section if any
|
||||
if (job.skipped && job.skipped.length > 0) {
|
||||
jobHTML += `
|
||||
<div class="skipped-section">
|
||||
<div class="skipped-toggle" onclick="toggleSkippedDetails(this)">
|
||||
Skipped Files (${job.skipped.length}) >
|
||||
</div>
|
||||
<div class="skipped-details">
|
||||
<ul>
|
||||
${job.skipped.map(skipInfo => `<li><strong>${skipInfo[0]}</strong>: ${skipInfo[1]}</li>`).join('')}
|
||||
</ul>
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
}
|
||||
|
||||
jobDiv.innerHTML = jobHTML;
|
||||
console.log(`[updateJobs] HTML created for job ${jobId}`);
|
||||
|
||||
// Insert job before the script tag
|
||||
if (scriptTag) {
|
||||
body.insertBefore(jobDiv, scriptTag);
|
||||
} else {
|
||||
body.appendChild(jobDiv);
|
||||
}
|
||||
console.log(`[updateJobs] Job ${jobId} element inserted into DOM`);
|
||||
} catch (err) {
|
||||
console.error(`[updateJobs] Error creating job element for ${jobId}:`, err);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.log('[updateJobs] No jobs to render, showing "no jobs" message');
|
||||
const noJobsMsg = document.createElement('p');
|
||||
noJobsMsg.innerHTML = 'No jobs yet. <a href="/">Upload some files</a> to get started.';
|
||||
if (scriptTag) {
|
||||
body.insertBefore(noJobsMsg, scriptTag);
|
||||
} else {
|
||||
body.appendChild(noJobsMsg);
|
||||
}
|
||||
}
|
||||
console.log('[updateJobs] Update complete!');
|
||||
})
|
||||
.catch(function(error) {
|
||||
console.error('[updateJobs] Error updating jobs:', error);
|
||||
if (error.stack) {
|
||||
console.error('[updateJobs] Error stack:', error.stack);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
console.log('[Init] Setting up job update interval...');
|
||||
// Update jobs every 1 second
|
||||
const updateInterval = setInterval(updateJobs, 1000);
|
||||
console.log('[Init] Update interval set, ID:', updateInterval);
|
||||
|
||||
// Initialize previous job statuses
|
||||
console.log('[Init] Running initial job update...');
|
||||
updateJobs(); // Run once immediately to populate initial statuses
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
''', jobs=job_queue)
|
||||
|
||||
@app.route('/api/jobs')
|
||||
|
||||
Reference in new issue
Block a user