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

clusterizer

v0.4.0

Published

Instant clusterization of an array of module paths, a directory of modules, or an array of npm module names.

Downloads

86

Readme

Clusterizer

Instant clusterization of an array of module paths, a directory of modules, or an array of npm module names.

Modules don't need to be performing the same task, as is usually the case with Node.js clusters. Clusterizer excels at offloading long-running operations into another process and scheduling them intelligently.

Clusterizer includes built-in sleep-type scheduling as well as advanced scheduling through Agenda.

Installation

$ npm install clusterizer

Features

  • automatic process forking
  • events to/from each module
  • log/error aggregation to master process
  • built-in sleep-type scheduling
  • integration with Agenda for advanced scheduling
  • graceful shutdown

Usage

See test_modules/module1.coffee for an example module. Modules need to inherit from Clusterized and optionally implement a process(callback) function. The class name (or constructor function name if in js) is irrelevant as long as it is a module-level export as shown below.

{ Clusterized } = require 'clusterizer'

class Worker extends Clusterized
  process: (callback) ->
    # do something
    @send "result", 3.141592653589793
    callback(err)

module.exports = Worker

Events are another way to interact with your worker:

{ Clusterized } = require 'clusterizer'

class Worker extends Clusterized
  constructor: ->
    @on 'start-process-function', (msg) ->
      @myProcessFunction

  myProcessFunction: ->
    # do the magic
    @send "magic", "result"

module.exports = Worker

Then instantiate a Clusterizer in your code with an options object. See the example main function in clusterizer.coffee. Use .isMaster as a check to prevent your other code from running in every process.

{ Clusterizer } = require 'clusterizer'

clusterizer = new Clusterizer
  logging: true
  dir: ["../test_modules"]

if clusterizer.isMaster

  # example log handler
  clusterizer.on 'log', (msg, module) ->
    console.log "LOG : #{module} : #{msg}"

  # example error handler, err object contains fields
  # similar to Error, i.e. name, message, stack
  clusterizer.on 'error', (err, module) ->
    console.error "ERROR : #{module} : #{err.stack}"

  # example user-defined message handler
  clusterizer.on 'echo', (msg, module) ->
    console.log "\nGot #{msg} from #{module}\n"

  # modify sleep backoff time for all
  clusterizer.setSleep 500

  # modify sleep backoff for specific module
  clusterizer.setSleep 500, 'module2'

  # set agenda for all
  clusterizer.setAgenda 'localhost:27017/test', '3 seconds'

  # set agenda for one
  clusterizer.setAgenda 'localhost:27017/test', '3 seconds', 'module1'

  # start all (uses Agenda mode for any module which has an agenda set)
  clusterizer.start()

  # broadcast event to all modules
  clusterizer.send "echo", "test broadcasted message"
  # or
  clusterizer.broadcast "echo", "test broadcasted message"

  # event with message to single module
  clusterizer.send "echo", "call me back", "module2"

  # event with no message to single modules
  clusterizer.send "echo", null, "module2"

  # stops module1
  clusterizer.stop 'module1'

  # stops all
  clusterizer.stop()

  # restart all
  clusterizer.start()

  # kill all
  clusterizer.kill()

  # ... your code ...

Other forms of specifying worker modules in the Clusterizer options:

file: ["../test_modules/module1.coffee", "../test_modules/module2.coffee"]
npm: ["clusterizer-test-module1", "clusterizer-test-module1"]

Note

  • the file:, dir:, and npm: options can be used simultaneously
  • duplicate modules are currently not supported
  • the 'error' event is emitted, so it needs to have a listener or an unspecified error will be thrown
  • any errors thrown in clusterized modules will be emitted by the clusterizer with a full stack trace

Advanced Scheduling

setAgenda(database, every, name)

Use setAgenda to define a fuzzy execution frequency. Clusterizer uses Agenda behind the scenes so the database and every parameters are what Agenda expects. For example, something like

clusterizer.setAgenda 'localhost:27017/test', '3 seconds', 'module1'

If the name parameter isn't specified, Clusterizer will apply the agenda to all clusterized modules.

Calling start() once an Agenda has been defined for a module will always use the agenda, not the sleep period.

A module can call setAgenda() on itself if the db address is fixed or handed in using a message. This allows each module to specify its preferred schedule.

License

MIT