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

schedward

v0.3.1-hotfix

Published

Smart scheduled tasks manager for Node

Downloads

109

Readme

Schedward

A simple to use scheduled task manager for highloaded Node apps.

Preamble

High load on back-end applications and setTimeout do not mix quite well, especially when it comes to scheduled processing of large amounts of data within frequent user requests, and here's why:

  • Whenever setTimeout callback uses reference to an object defined outside its execution scope, it keeps this object from being garbage collected during whole wait time for a scheduled callback to execute. This leads to data being stored in heap for much longer than it is supposed to, and every time a garbage collection triggers, it will have harder time iterating through needless references across multiple runs. Combine that with a fact that V8's GC halts a runtime until it finishes it's job, and you'll have your application throttling.

  • Async is not threads. setTimeout queues a callback execution in event loop's task queue. Event loop is processed every time a current synchronous code execution completes, thus making queued calls consecutively synchronous. Frequent setTimeout calls triggered by user request in a high loaded application cause event loop pollution.

  • If a callback is passed as a closure defined straight away instead of a single callback function definition somewhere outside setTimeout call, this might also pollute the heap and block execution during JIT compilation of a closure.

All of these problems can be handled with a proper approach to scheduled operations implementation, of course. Sometimes, though not necessarily, it also implies using additional boilerplate code as a solution.

This package offers rather universal solution to reduce that boilerplate, avoid mistakes and optimize scheduled tasks queue processing.

Features

  • Distribution of scheduled tasks across a process pool, allowing to scale up a number of event loops and garbage collectors operating with task queues in parallel and balance the load

  • Segregation of scheduled tasks queues from application main process, allowing to unload it from continuous, excessive and intensive event loop processing and garbage collection

  • Dereferencing of objects passed as callback parameters from outside callback execution scope by making their deep copies and storing them in a pooled process heap instead

  • Failover hooks for storing and redistributing task registries across runners pool upon application restart

  • Separation of tasks according to scheduled operation logical purpose

  • Stateless, isolated scheduled task callbacks

Installation

npm

npm i -S schedward

Basic usage


import { TaskManager } from 'schedward'

const manager = new TaskManager(),
    concurrency = 1 // number of task runners in a pool

// launch a pool of task runners for an instance of task manager
manager.launch(concurrency, () => {

    manager
        // pull a task schedule
        .task('mytask')
        // add a callback for all tasks triggered within a schedule
        .addListener('timeout', ({ task_uid }) => {

            // use task_uid as a unique identifier
            // can also represent a parameters set composed into a string
            console.log('Task', task_uid, 'executed')

            // stop runners pool upon last task execution
            if(task_uid == 'anothertask') manager.stop()

        })

    manager.task('mytask').timeout('newtask', 1000)
    manager.task('mytask').timeout('anothertask', 2000)

})

// (after 1000ms passed): 
// -> Task newtask executed

// (after another 1000ms passed): 
// -> Task anothertask executed

Documentation

For extended usage examples and instructions please refer to full documentation page on GitHub.

Important note

This package is not meant to handle actual scheduled operation code execution using separate process pool's resources. It only allows to optimize queued tasks processing in an event loop for monolith back-end applications.

This may or may not be a subject to change in a future though ;)

If you need to improve your computing power utilization for compute-intensive operations, consider referring to Node's built-in cluster module to develop your own solution for that.

You might also need to look in a direction of horizontal system scaling solutions for your application, e.g. microservice architecture, or delegate your compute-intensive code to native addons.

TBD

Future versions of this package are planned to provide additional features:

  • Handle crash events and errors in a task runners pool
  • Benchmarking and GC tracing
  • TaskRunner and process pooling documentation