background-process-js
Version:
A set of util tools for create background process.
58 lines (57 loc) • 2.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeCacheProvider = void 0;
class NodeCacheProvider {
constructor(nodeCacheConfig) {
this.nodeCacheConfig = nodeCacheConfig;
this.MAX_NUMBER_OF_MESSAGES_BY_CHUNK = 10;
this.nodeCacheConfig = {
...nodeCacheConfig,
MaxNumberOfMessagesByChunk: this.nodeCacheConfig.MaxNumberOfMessagesByChunk ??
this.MAX_NUMBER_OF_MESSAGES_BY_CHUNK,
};
if (this.nodeCacheConfig.MaxNumberOfMessagesByChunk >
this.MAX_NUMBER_OF_MESSAGES_BY_CHUNK) {
throw Error("Exceeded the maximum number of messages by request (10)");
}
else if (this.nodeCacheConfig.MaxNumberOfMessagesByChunk < 1) {
throw Error("The minimum number of messages by request is 1");
}
}
hasDeadQueue() {
return !!this.nodeCacheConfig.deadQueueName;
}
getMaxNumberOfMessagesByChunk() {
return this.nodeCacheConfig.MaxNumberOfMessagesByChunk;
}
async getMessages() {
const messages = [];
for (let index = 0; index < this.nodeCacheConfig.MaxNumberOfMessagesByChunk; index++) {
const id = this.nodeCacheConfig.idGenerator.dequeueID(this.nodeCacheConfig.mainQueueName);
if (!id)
break;
const body = this.nodeCacheConfig.client.get(id);
messages.push({ id, body });
}
return messages;
}
async deleteMessages(messages) {
if (messages.length === 0)
return;
messages.forEach((message) => {
this.nodeCacheConfig.client.del(message.id);
});
}
async markAsDeadMessages(messages) {
if (messages.length === 0)
return;
if (!this.hasDeadQueue()) {
throw Error("Need to pass 'deadQueueUrl' in Node Cache config");
}
for (const message of messages) {
const id = this.nodeCacheConfig.idGenerator.enqueueID(this.nodeCacheConfig.deadQueueName);
this.nodeCacheConfig.client.set(id, message.body);
}
}
}
exports.NodeCacheProvider = NodeCacheProvider;