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

taskdist

v0.1.19

Published

Task distribution framework

Downloads

12

Readme

A framework for setting up a master and worker nodes for distributed tasks.

Synopsis:

import * as Taskdist from 'taskdist';

// Describes the task that the worker can complete.
interface Task {
    // Calculate the n-th Fibonacci number.
    type: 'fibonacci';
    n: number;
}

// The result of the task.
type TaskResult = number;

// Implementation of the work to be done for a task.
class TaskHandler implements Taskdist.TaskHandler<Task, TaskResult> {
    public stop() {
        // Connection closed.
        // Close resources if necessary.
    }

    public async doTask(task: Task) {
        let [a, b] = [0, 1];
        for (let i = 0; i < task.n - 1; i++) {
            [a, b] = [b, a + b];
        }
        return b;
    }
}

(async () => {
    // Set up master node.
    const taskScheduler = new Taskdist.FifoScheduler<Task, TaskResult>();
    const master = new Taskdist.Master(taskScheduler, { listenTimeout: 5000, port: 9000, socketTimeout: 5000, taskTimeout: 30000 });

    // Set up worker node.
    // Can be in the same process (as in this example).
    // Can also be a different process on the same machine, or on another machine altogether.
    const taskHandler = new TaskHandler();
    const worker = new Taskdist.Worker(taskHandler, { connectTimeout: 5000, masterHost: 'localhost', masterPort: 9000, protocol: 'ws' });

    // Start the master and worker.
    master.start();
    worker.start();

    // Put a task onto the queue, and await the result.
    const result = await taskScheduler.put({ type: 'fibonacci', n: 10 });
    // Log the result.
    console.log(`The 10th Fibonacci number is: ${result}.`);

    // When finished, stop the master and worker.
    master.stop();
    worker.stop();
})();

Master

When creating a master:

  • A WebSocket server will be created to listen on the configured port.
  • A FIFO queue is created onto which tasks can be pushed.
  • Workers can connect to the master and pop tasks from the queue.
  • If workers do not complete the task within the timeout, the master will put the task back onto the queue, ensuring "at least once" execution.
  • If the WebSocket server yields an error, the server is re-created indefinitely until the master is stopped.

Worker

When creating a worker:

  • A WebSocket connection is made to the master.
  • The worker will continuously pop tasks from the master and work on those tasks.
  • If the connection is lost, the worker will attempt to re-connect.
  • Stopping the worker will abort work on the current task and disconnect the WebSocket.

Roadmap

  • Option for workers to reject tasks. Worker to send "reject" message to master, and master to put the task back onto the queue.
  • Task filtering on worker side. Allow user to define which tasks can be worked on.