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

supertask-cluster

v0.1.5-alpha

Published

A cluster superset of Supertask

Downloads

8

Readme

supertask-cluster

Build Status Test Coverage

Create, compile and run tasks across a cluster with minimal setup.

Installation

Note that this module is a superset of Supertask and requires ES6 & NodeJS 4.x+.

npm install supertask-cluster

Features

  • Automatic Cluster & Worker creation
  • Cluster Monitoring, Worker resurrection
  • Bidirectional argument passing and results from Master to Worker and vice versa.
  • Processing & Compilation of function or JS source on Workers with minimal effort. (uses NodeJS VM no interpreters or parsers)
  • Buffer upload mechanism and Buffer references as arguments
  • Entirely written in JS and fully based on NodeJS core modules where possible

Usage

Create a new shared task with a unique name and distribute it to all available Workers (one per core). Note that a unique name is required for every task.

var SupertaskCluster = require('supertask-cluster');
var cluster = new SupertaskCluster();
// Deploy Cluster across all CPU cores
cluster.deploy();

TaskManager.addShared('taskname', function power(n, x, callback) {
    // n^x function
    callback(null, Math.pow(n,x));
}, function callback(error, task) {
    // You'll need to distribute the task to Workers
    // before executing the latest code/context
    task.distribute(function(error, stats){
        console.log("Task was distributed to", stats[0], "cores of", stats[1], "total");
    });
});

Run task. You can pass arguments after name and before callback. This will automatically run the task on a free Worker based on the internal load.

TaskManager.do('taskname', 2, 4, function callback(error, result) {
    console.log("2^4 is equal to", result);
});

Note that the usual Supertask methods work as this module is merely a superset.

API

Buffers

Buffers allow you to pre-upload a StorageType (currently only NodeJS's internal Buffer object is supported) to a Worker in order to save upload time. This is inspired by GPU's parallelization mechanism whereby data is uploaded to the GPU in order to save bandwidth and data transfer time that will otherwise make parallelization on GPUs slow. This same concept applies to a Cluster and if large Buffers are passed as arguments to every function every time you'll spend most of your processing time uploading data. This is where Buffers come in. Buffers are automatically chunked & uploaded (like a stream) to avoid congestion that would otherwise make passing of a 2GB Buffer to a Worker impossible.

Here is how to use Buffers:

Create a new task:

// Create a large 20MB buffer on Worker ID 0
var buffer = new Buffer(20000000);
// Fill buffer with 'c's
buffer.fill('c');
// Create new Buffer on Worker 0. Note that the Buffer will take some time to upload.
// (approximately 20s per 1GB or 400ms for 20MB)
TaskManager.createBufferOnWorker('0', 'largeBuffer', buffer, 'utf8', false, true, function(error){
    if(error) throw error;
    // Here we create a reference to the Buffer
    var ref = cluster.workerBufferReference('largeBuffer');
    // Create a new task to process some of the Buffer
    TaskManager.addShared('bufferProcessor', function (buf, callback) {
        if(!buf) return callback(new Error('Buffer did not exist in the cluster'));
        // Convert 10 chars to String starting from 2kb (results in 10 'c's in a row)
        var str = buf.toString('utf8', 2000, 2010);
        // Pass str to master
        callback(null, str);
    }, function (error, task) {
        // Here we set the permissions to Minimal in order to make the Buffer object available to the task
        task.permission(SupertaskCluster.ST_MINIMAL);
        // Distribute the task to all Workers
        task.distribute(function(){
            // Run task with ref as an argument
            task.call(ref, function(error, rstr){
                // Processed string
                console.log(rstr);
                // Output: cccccccccc
            });
        });
    });
});

Note that the Cluster automatically chooses the best candidate that has the Buffer reference available. If the Buffer reference is not available in any of the workers it will be set to undefined.

A Buffer can be immutable. Read API documentation for more info. More documentation and methods coming soon. Check out the test functions for more information in the mean time.

Disclaimer

This module is not yet ready to be used in a production environment. While Supertask has reasonably good stability with over 40 tests it does not fully expose all methods and capabilities and may not function as intended. Supertask-cluster is equally missing some important cluster monitoring methods to keep the cluster alive and well in a production environment. Use it at your own risk.

License

MIT © Schahriar SaffarShargh [email protected]