node-fs-queue
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

node-fs-queue

Usage

import { STATUS } from './Maildir'
import { Channel } from './Channel'
import { Queue } from './Queue'
import path from 'path'

const queue = new Queue({ cwd: path.join(process.cwd(), 'queue') })
const stability = queue.channel('stability')

// cria novos itens
setInterval(async () => {
  // cria prioridade entre 0 e 6
  const priority = Math.floor(Math.random() * 6) + 1
  await stability.push({ now: Date.now(), priority }, priority)
}, 1000)

async function worker (queue: Channel) {
  if (queue.length > 0) {
    const message = await stability.pop()
    const payload = await message.json()

    console.info(payload)

    await message.changeStatus(STATUS.ERR)
  }

  const timeout = (ms: number = 5000) => new Promise(resolve => setTimeout(resolve, ms))

  await (queue.length === 0 ? timeout(5000) : timeout(100))
  await worker(queue)
}

worker(stability)

Readme

Keywords

none

Package Sidebar

Install

npm i node-fs-queue

Weekly Downloads

5

Version

1.0.3

License

proprietary

Unpacked Size

21.9 kB

Total Files

14

Last publish

Collaborators

  • brunocarvalhodearaujo