Skip to content

Commit

Permalink
feat(api): Allow ordering queue listing
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Jan 15, 2024
1 parent 85f2aa7 commit 41017ff
Show file tree
Hide file tree
Showing 5 changed files with 698 additions and 652 deletions.
48 changes: 46 additions & 2 deletions lib/api-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,54 @@ class APIServer {
}

let type = (req.params.type || '').toString().toLowerCase().trim();

type = ['deferred', 'queued'].includes(type) ? type : 'queued';

this.queue.listQueued(req.params.zone, type, 1000, (err, list) => {
let sort;
let sortKey = (req.query.sort || '').toString().trim().toLowerCase();
let sortOrder;

let start = Number((req.query.start || '').toString().trim()) || 0;
let pageSize = Number((req.query.pageSize || '').toString().trim()) || 1000;

if (start < 0) {
start = 0;
}

if (start > 1000000000) {
start = 1000000000;
}

if (pageSize < 0) {
pageSize = 0;
}

if (pageSize > 1000) {
pageSize = 1000;
}

switch ((req.query.order || '').toString().trim().toLowerCase()) {
case 'asc':
sortOrder = 1;
break;
case 'desc':
default:
sortOrder = -1;
break;
}

switch (sortKey) {
case 'created':
case 'queued':
sort = {};
sort[sortKey] = sortOrder;
break;
case 'id':
default:
sort = { _id: sortOrder };
break;
}

this.queue.listQueued(req.params.zone, type, sort, start, pageSize, (err, list) => {
if (err) {
res.json(500, {
error: err.message
Expand Down
23 changes: 17 additions & 6 deletions lib/mail-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,13 @@ class MailQueue {
this.garbageTimer = null;
}

listQueued(zone, type, maxItems, callback) {
listQueued(zone, type, sort, start, maxItems, callback) {
sort = sort || {
_id: 1
};

start = start || 0;

let query = {
sendingZone: zone,
queued: {
Expand All @@ -1105,11 +1111,13 @@ class MailQueue {
.project({
id: 1,
sendingZone: 1,
recipient: 1
})
.sort({
_id: 1
recipient: 1,
queued: 1,
created: 1,
'_deferred.count': 1
})
.sort(sort)
.skip(start)
.limit(maxItems)
.toArray((err, entries) => {
if (err) {
Expand All @@ -1125,7 +1133,10 @@ class MailQueue {
entries.map(entry => ({
id: entry.id,
zone: entry.sendingZone,
recipient: entry.recipient
recipient: entry.recipient,
created: entry.created.toISOString(),
queued: entry.queued.toISOString(),
deferred: (entry._deferred && entry._deferred.count) || 0
}))
);
});
Expand Down
7 changes: 0 additions & 7 deletions lib/queue-locker.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ class QueueLocker {

lock(key, zone, domain, lockOwner, maxConnections, ttl) {
if (this.lockExists(key)) {
console.log('LOCK EXISTS', key);
return false;
}
console.log('SETTING LOCK', key);

/*
if (domain && this.domainIsSkipped(zone, domain)) {
Expand Down Expand Up @@ -115,17 +113,13 @@ class QueueLocker {

release(key) {
if (!key) {
console.log('NOT LOCKED 1', key);
return false;
}

if (!this.locks.has(key)) {
console.log('NOT LOCKED 2', key);
return false;
}

console.log('RELEASING LOCK', key);

let lock = this.locks.get(key);
this.locks.delete(key);

Expand Down Expand Up @@ -166,7 +160,6 @@ class QueueLocker {

releaseLockOwner(lockOwner) {
if (!this.lockOwners.has(lockOwner)) {
console.log('RELEASE OWNER', lockOwner);
return false;
}
let keys = [];
Expand Down
Loading

0 comments on commit 41017ff

Please sign in to comment.