New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

level-queue

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

level-queue

queue plugin for leveldb.

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

level-queue

queue plugin for leveldb.

Deprecated

Use level-trigger instead.

Stability

Deprecated: Expect no more changes. Avoid using this module.

Example

This module can be used to add job queues to leveldb, and is especially useful if used with level-hooks

var levelup = require('levelup')

levelup(file, function(err, db) {

  //adds .queue to db.
  require('level-queue')()(db)

  //add a worker, for a given job name.
  db.queue.add('job', function (value, done) {

    setTimeout(function () {
      console.log(value)
      //call done() to delete this job from the database.
      done()
    }, Math.random() * 1000)

  })

  db.queue('job', 'todo - may be any string or buffer')
  
})

If the process crashes before the job is completed, it will be restarted when the process restarts.

The job must be idempotent.

Example with hooks

make a twitter like news feed, where messages are posted to friend's feeds.

var levelup = require('levelup')
var map     = require('map-stream')

levelup(file, function(err, db) {


  //adds .queue to db.
  require('level-hooks')()(db)
  require('level-queue')()(db)

  //add a worker, for a given job name.
  db.queue.add('job', function (value, done) {

    setTimeout(function () {
      console.log(value)
      //call done() to delete this job from the database.
      done()
    }, Math.random() * 1000)

  })

  /**

    calling db.queue(name, value, false)

    will not PUT the queue into the database, but will instead return the insert
    so that it can be included into an atomic batch operation.

  **/

  db.hooks.pre(function (batch) {
    //if any of the inserts is a user 
    
    for(var i in batch) {
      var row = batch[i]
      if(isUserMessage(row.key))
        batch.push(db.queue('postToFriends', row.value, false))
    }
  })

  db.queue.add('postToFriends', function (value, done) {
    var message = JSON.parse(''+value)

    //retrive all friends of a poster.
    db.readStream({
      start: 'friends:'+message.author, 
      end: 'friends:'+message.author + '~'
    })
    //attach the message to all follower's feeds.
    .pipe(map(function (val, next) {
      db.put('feed:'+val+':'
        +message.timestamp()+':'
        +message.author, value, next)
      //note: since we take the timestamp from the message,
      //if the job accidentially gets run twice,
      //it will just overwrite the same message.
      //the job is idempotent!
    })
    //mark the job DONE!
    .on('end', done)
  })
  
})

License

MIT

FAQs

Package last updated on 08 Mar 2013

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc