Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added queue cleaning #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

// Store for all of the jobs in progress
let jobs = {};

Expand All @@ -12,14 +12,19 @@ async function addJob() {

// Fetch updates for each job
async function updateJobs() {
let newJobs = {};
for (let id of Object.keys(jobs)) {
let res = await fetch(`/job/${id}`);
let result = await res.json();
if (!!jobs[id]) {
jobs[id] = result;
try {
let result = await res.json();
if (!!jobs[id]) {
newJobs[id] = result;
}
} catch (e) {
}
render();
}
jobs = newJobs;
render();
}

// Delete all stored jobs
Expand Down Expand Up @@ -53,7 +58,7 @@ function renderJob(job) {
color = "bg-dark-red";
progress = 100;
}

return document.querySelector('#job-template')
.innerHTML
.replace('{{id}}', job.id)
Expand Down
15 changes: 14 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,20 @@ app.get('/job/:id', async (req, res) => {

// You can listen to global events to get notified when jobs are processed
workQueue.on('global:completed', (jobId, result) => {
console.log(`Job completed with result ${result}`);
console.log(`Job ${jobId} completed`);
});

// Message cleaning
workQueue.on('cleaned', function(jobs, type) {
console.log('Cleaned %s %s jobs', jobs.length, type);
});

// Clear queue every minute of processed and failed jobs.
setInterval(function() {
// Cleans all jobs that completed over 10 seconds ago,
// and cleans all jobs that failed over 2 minutes ago.
workQueue.clean(10000);
workQueue.clean(120000, 'failed');
}, 60000);

app.listen(PORT, () => console.log("Server started!"));