Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
storage-based-queue
Advanced tools
Storage based queue processing mechanism. Today, many backend technology is a simple derivative of the queuing systems used in the browser environment.
You can run jobs over the channels as asynchronous that saved regularly.
This library just a solution method for some use cases. Today, there are different technologies that fulfill the similar process.
Data regularly store (local storage for now) added to queue pool. Storing queue data is also inspired by the JSON-RPC method. When the queue is started, the queues start to be processed sequentially in the specified range according to the sorting algorithm.
If any exceptions occur while the worker classes are processing, the current queue is reprocessed to try again. The task is frozen when it reaches the defined retry value.
You need to create at least one channel. One channel can be created as many channels as desired. Channels run independently of each other. The areas where each channel will store tasks are also separate. The area where tasks are stored is named with the channel name and prefix.
The important thing to remember here is that each newly created channel is actually a new copy of the Queue class. So a new instance is formed, but the dependencies of the channels are still alive as singletons.
Example; You created two channels. Their names are channelA and channelB. If you make a setting in the channelA instance, this change will also be reflected in channelB and all other channels.
Worker classes should return boolean
(true / false)
data with the Promise class as the return value. The return Promise / resolve (true)
must be true if a task is successfully completed and you want to pass the next task. A possible exception should also be tried again: Promise / resolve (false)
. If we do not want the task to be retried and we want to pass the next task: Promise / reject ('any value')
$ npm install storage-based-queue --save
import:
import * as Queue from 'storage-based-queue';
import via script tag:
<script type="text/javascript" src="node_modules/storage-based-queue/dist/queue.js">
Create instance:
new Queue;
Worker class:
class SendEmail {
retry = 2;
handle(args) {
try {
return new Promise((resolve, reject) => {
resolve(true);
});
} catch(e) {
reject('rejected')
}
}
}
Note: The return value of the worker classes must be promise.
Worker Register:
Queue.register([
{ handler: SendEmail }
]);
Create channel and run queue listener:
const queue = new Queue();
const channelA = queue.create('send-email');
channelA.start();
Add a job to channelA listener:
channelA.add({
handler: 'SendEmail',
args: {email: 'johndoe@example.com', fullname: 'John Doe'}
});
That's it!
The queue will start automatically because we have already started the start() method
Name | Type | Description |
---|---|---|
prefix | string | Storage key name prefix for jobs. Default: sq_jobs |
timeout | integer | Worker delay time of between two taks. Default: 1000 |
limit | integer | Runnable task limit. Default: -1 (limitless) |
Name | Description |
---|---|
FIFO | First In First Out (Default) |
LIFO | Last In First Out |
Detail information: FIFO and LIFO
Example:
const queue = new Queue({prefix: 'my-Queue', timeout: 1500, limit: 50, principle: Queue.FIFO})
Other ways config the queue (runtime):
const queue = new Queue;
queue.setTimeout(15000);
queue.setLimit(50);
queue.setPrefix('my-Queue');
queue.setPrinciple(Queue.LIFO);
Below the detailed worker class usage.
class SendEmail {
retry = 2;
handle(args, dep1, dep2) {
try {
return new Promise((resolve, reject) => {
resolve(true);
});
} catch(e) {
reject('rejected')
}
}
before(args) {
//
}
after(args) {
//
}
}
Note: The worker classes has two events. before and after
Register:
const user = new User;
const order = new Order;
Queue.register([
{ handler: SendEmail, deps: { user, order } }
]);
All methods will explain in this section with examples.
Create new task and return it's queue id.
Name | Type | Description |
---|---|---|
handler | string | Worker class name. |
args | string | Worker parameters. |
priority | string | Queue priority value. Default: 0 |
tag | string | Task idenitity tag. |
Example:
queue.add({
handler: 'SendEmail',
tag: 'email-sender',
args: {email: 'johndoe@example.com', fullname: 'John Doe'},
priority: 2
});
Start the queue listener. If has any tasks waiting the run, starts the process of these tasks. Next when adding tasks will run automaticly.
Example:
const queue = new Queue;
queue.start();
Stop the queue listener after current tasks is done.
queue.stop();
Stop the queue listener including current task.
queue.forceStop();
Create a new channel.
const channelA = qeueu.create('channel-a');
channelA.add({
handler: 'SendEmail',
args: {email: 'johndoe@example.com', fullname: 'John Doe'}
});
channelA.start();
Checks the channel repository has any task.
channel.isEmpty()
Get the number of tasks.
channel.count();
Get the number of tasks by a specific tag.
channel.countByTag('send-email');
Clear all tasks.
channel.clear();
Clear all tasks by a specific tag.
channel.clearByTag('send-email');
Checks a task by queue id.
const id = channel.add({
handler: 'SendEmail',
args: {email: 'johndoe@example.com', fullname: 'John Doe'}
});
channel.has(id);
Checks a task by tag.
channel.hasByTag('email-sender');
MIT license
FAQs
Simple client queue mechanism. Create it and run asynchronously with worker class in browser.
The npm package storage-based-queue receives a total of 48 weekly downloads. As such, storage-based-queue popularity was classified as not popular.
We found that storage-based-queue demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.