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

@1999/scheduler

v0.0.9

Published

Node.js library for periodical tasks

Downloads

40

Readme

Scheduler

Build Status Dependency Status DevDependency Status

Node.js library for periodical tasks written in Typescript.

How to install

npm install @1999/scheduler --save

API

The only concept of scheduler is a Task which represents a periodically run task. It should be a callback which should return a Promise.

Simple tasks

Use scheduler.addTask(task: Task, period: number) function to set up task period.

import {
  default as Scheduler,
  Task,
} from '@1999/scheduler';

const task: Task = () => Promise.resolve(2);
const scheduler = new Scheduler();
scheduler.addTask(task, 1000);

scheduler.start();

Named tasks

In this case you can pass task groups in scheduler constructor. Then use scheduler.addTask(task: Task, periodId: string) function to assign task to task group.

import {
  default as Scheduler,
  Task,
} from '@1999/scheduler';

const task1: Task = () => got('https://api.facebook/id/1');
const task2: Task = () => got('https://api.facebook/id/2');

const scheduler = new Scheduler({ api: 1000 });
scheduler.addTask(task1, 'api');
scheduler.addTask(task2, 'api');

scheduler.start();

When period should depend on task execution

Sometimes it's not enough to have execution periodicity for tasks. For instance when you have an API which allows you to make requests once per N seconds: in this case it can be safer to send next request only N seconds after you get the response from the previous request. For this purpose you can use Marker callback which is the only argument for Task:

import {
  default as Scheduler,
  Marker,
  Task,
} from '@1999/scheduler';

const task: Task = (marker: Marker) => {
  return got('https://api.facebook/id/1').then(() => {
    // you can run marker function anywhere inside your task
    // and the period pause will be started from this moment
    marker();
  });
};

const scheduler = new Scheduler();
scheduler.addTask(task);
scheduler.start();

Events

Scheduler instance extends node.js EventEmitter. You can use it to subscribe to events happening inside Scheduler instance:

  • taskCompleted - emits when task is successfully finished. Also emits an object { name: string, execTime: number } where runTime is the task execution time in milliseconds.
  • taskFailed - emits when task execution fails. Also emits an object { err: Error, execTime: number, name: string }