local-log-queue
Version:
A file-backed async queue with auto-processing for Node.js
124 lines (90 loc) • 2.85 kB
Markdown
# local-log-queue
A **file-backed asynchronous queue** for Node.js with automatic processing.
Supports **in-memory buffering**, **persistent storage**, and **sequential processing** of queued items.
---
## Features
- Async processing with user-defined callback
- In-memory queue for fast writes
- Persistent log file for crash recovery
- Automatic processing when new items are added
- Sequential processing of queued items
- Node.js built-in module compatible
---
## Installation
```bash
# Using npm
npm install local-log-queue
# Using yarn
yarn add local-log-queue
```
## Usage
### Basic Setup
```js
const LocalQueue = require('local-log-queue');
// Initialize queue with a callback
const queue = new LocalQueue(async (item) => {
console.log('Processing item:', item);
});
// Add items to the queue
queue.enqueue({ task: 'Task 1' });
queue.enqueue({ task: 'Task 2' });
queue.enqueue({ task: 'Task 3' });
```
## Express with localQueue
```js
let express = require('express');
let LocalQueue = require('local-log-queue');
let app = express();
let port = 3000;
const queue = new LocalQueue((item)=>{
setTimeout(() => {
console.log('Processed item:', item);
}, 4000);
})
app.get('/', (req, res) => {
queue.enqueue({id:new Date().toISOString(),data : "Hello World"});
res.send('Hello World!');
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
```
### 1. Print Messages Sequentially
```js
const LocalQueue = require('local-log-queue');
const messageQueue = new LocalQueue(async (item) => {
console.log('Message:', item.text);
});
// Add messages
messageQueue.enqueue({ text: 'Hello World!' });
messageQueue.enqueue({ text: 'Queue is working!' });
messageQueue.enqueue({ text: 'This is easy to understand!' });
```
## 2. Simple Task Queue
```js
const LocalQueue = require('local-log-queue');
const taskQueue = new LocalQueue(async (item) => {
console.log(`Processing Task #${item.id}: ${item.name}`);
});
// Add tasks
taskQueue.enqueue({ id: 1, name: 'Wash dishes' });
taskQueue.enqueue({ id: 2, name: 'Clean room' });
taskQueue.enqueue({ id: 3, name: 'Do homework' });
```
## 3. Number Processing Queue
```js
const LocalQueue = require('local-log-queue');
const numberQueue = new LocalQueue(async (item) => {
console.log(`Number squared: ${item.num * item.num}`);
});
// Add numbers
numberQueue.enqueue({ num: 2 });
numberQueue.enqueue({ num: 5 });
numberQueue.enqueue({ num: 10 });
```
## API
new LocalQueue(callback)
callback (required) – async function called for each queued item.
queue.enqueue(item)
item – object to add to the queue.
Automatically triggers processing if the queue is idle.