npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

couch-worker

v1.8.4

Published

This module is used to provide shared functionality between CouchDB workers. It manages the connection to CouchDB and listening for changes, as well as posting those changes back to Couch and logging updates.

Downloads

27

Readme

couch-worker

This module is used to provide shared functionality between CouchDB workers. It manages the connection to CouchDB and listening for changes, as well as posting those changes back to Couch and logging updates.

Defining a worker

var createWorker = require('couch-worker').createWorker;

module.exports = createWorker(function (config) {
  return {

    ignored: function (doc) {
      return doc._id[0] === '_';
    },

    migrated: function (doc) {
      return doc.new_property;
    },

    migrate: function (doc, callback) {
      doc.new_property = 'wheee';
      callback(null, doc);
    }

  };
});

ignored(doc)

This should be a predicate which returns true if the document is ignored by the worker, false otherwise. You might want to restrict the worker to operating on a specific document type, and exclude design docs for example.

Important: This function must be self-contained and not use surrounding scope so that it's suitable for converting to a string and sending to couchdb. That means no node-specific code or referencing things outside of the function body.

migrated(doc)

This should be a predicate which returns true if the doc has already been migrated, false otherwise. All documents returned from the migrate() function must pass this predicate.

Important: This function must be self-contained and not use surrounding scope so that it's suitable for converting to a string and sending to couchdb. That means no node-specific code or referencing things outside of the function body.

migrate(doc, callback)

This is the migration function which can cause whatever effects may be required to update the document then passes the updated document back to the callback. You can return multiple documents in an array if you like, but you must return the original document as one of them (modified so that it passes the migrated()predicate).

This function will always be called from Node.js, so you can use surrounding scope in the module and require other Node modules.

Starting a worker

// require the worker definition (see above section)
var myworker = require('myworker');

var config = {
  name: 'My Worker',
  database: 'http://admin:password@localhost:5984/database',
  log_database: 'http://admin:password@localhost:5984/workers',
  concurrency: 4
};

// start the worker
var w = myworker.start(config);

// stop the worker
w.stop(function () {
  console.log('worker stopped');
});

Common configuration options

Your worker can use additional configuration properties as required (for API keys etc), but all workers using couch-worker have the following options available.

  • name (required) - String - The unique name for this worker instance
  • database (required) String - The database URL (with credentials) to migrate documents in
  • log_database (required) - String - The database URL (with credentials) to store worker-related data (error logs, priority queues etc)
  • concurrency - Number - Maximum number of documents to process in parallel
  • timeout - Number Time to wait in milliseconds for migrate() calls to return before causing a timeout error and discarding any future result
  • checkpoint_size - Number - The number of documents to process before recording a checkpoint (the sequence id the worker will resume processing from on a restart)
  • retry_attempts - Number - Number of times to retry a migrate() when an error is returned before recording the error in the log_database and moving onto the next change
  • retry_interval - Number - Number of milliseconds to wait before retrying
  • bucket - Object - An object with start and/or end properties. This causes the worker to hash all document IDs using md5 to put them into fair buckets. The worker will only process the document if the hex digest of the md5 hash is greater than or equal to start and less than end. All other documents will be ignored. This allows you to run multiple instances of the same worker to split up processing of documents. Start and end properties should be Strings in the hex range ('0000...' to 'ffff..'). Omitting the start property means "process everything up until 'end'", omitting the end property means "process everything from 'start' onwards".

Viewing worker progress / logs

You can push the couch-worker-dashboard CouchApp to the log_database db to get a web interface to workers using this module. This includes progress information, error logs and the ability to process specific documents on demand.