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

repetitive-task

v1.1.2

Published

Base layer for building time-based repetitive tasks

Downloads

6

Readme

RepetitiveTask Build Status

RepetitiveTask provides a base layer for building time-based repetitive tasks, such as background jobs that run forever.

Example

Simple task that always runs

const RepetitiveTask = require('repetitive-task')

class CpuMonitor extends RepetitiveTask {
  _process (task) {
    console.log(process.cpuUsage())
  }
}

const monitor = new CpuMonitor(60 * 1000)
monitor.start()

Condition based task

const irrigation = require('./irrigation')
const RepetitiveTask = require('repetitive-task')

class Gardener extends RepetitiveTask {
  _getTask () {
    return irrigation.getSoilMoisture()
      .then((moistureLevel) => {
        // Only water the garden if the moisture level is below 20%
        if (moistureLevel < 20) {
          return moistureLevel
        }

        return null
      })
  }

  _process (moistureLevel) {
    return irrigation.turnOnWater()
      .then(() => {
        return new Promise((resolve) => {
          setTimeout(resolve, 5 * 60 * 1000)
        })
      })
      .then(() => irrigation.turnOffWater())
  }
}

const gardener = new Gardener(12 * 60 * 60 * 1000)
gardener.start()

API - Usage

new RepetitiveTask(interval)

Creates a new RepetitiveTask instance.

  • interval (Number): The interval (in seconds) for the repetitive task.

NOTE: Tasks don't run on a strict interval. The specified interval is actually the amount of time between the end of one run and the beginning of the next run. This ensures that a long running task will never result in multiple tasks running simultaneously.

RepetitiveTask#start()

Starts the repetitive task.

RepetitiveTask#stop() Returns: Promise

Stops the repetitive task. Stopping the task may be asynchronous since a task may be running at the time of the call. The promise will resolve when the task has completed.

API - Extending

In order for RepetitiveTask to be useful, you must extend it with your actual task. The following methods are available to customize the behavior of the task.

RepetitiveTask#_createLogger(name) Returns: Object

Creates a logger for the repetitive task.

The built-in logger writes JSON messages to stdout, but can be replaced with any logger that supports the following API:

  • info([data], message)
  • error([data], message)

The optional data parameter is an object with additional properties to include in the log.

RepetitiveTask#_getTask() Returns: Promise

Gets a task to work on. Depending on the type of task being performed, you may need to determine if there is a task to run at any given time. For example, you may need to pull an item off of a queue and process it. In this case, _getTask() should read from the queue and return the item, if any.

Returning a falsy value indicates that there is no task to perform right now.

RepetitiveTask#_getLogData(task) Returns: Object

Gets additional data to log about the task. The object returned from _getLogData() will be passed to the logger for the "processing task" message.

  • task (Mixed): The task object returned from _getTask().

RepetitiveTask#_process(task) Returns: Promise

Processes the task. This is the main function for the repetitive task and is the only method that must be implemented for the task to run.

  • task (Mixed): The task object returned from _getTask().

License

Copyright RepetitiveTask contributors. Released under the terms of the ISC license.