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

timr

v2.0.0

Published

cron like job scheduling library

Downloads

40

Readme

timr Build Status

NPM

NPM

timr offers a scheduling interface for a cron like job execution.

installation

npm install timr

example

var moment = require('moment');

var timr = require('timr');

var scheduler = timr();

////every 15 seconds
scheduler().every(15).seconds().run(function() {
    console.log( 'every 15 seconds' );
});

//start 30 seconds from now, execute every 10 seconds, end in 5 minutes from now
var from = moment().add('seconds', 30);
var to = moment().add('minutes', 5);

scheduler()
    .from(from)
    .to(to)
    .every(10).seconds()
    .run(function() {
        console.log( 'now+30s every 10s until now+5m' );
    });

scheduler

a scheduler holds a collection of tasks. every task is created via the task construction function.

creates a task construction function

var timr = require('timr');

var taskConstructor = timr();

the scheduler object is exposed at the task constructor:

var timr = require('timr');

var taskConstructor = timr();

console.log( taskConstructor.scheduler );

when a attached task is executed, the parent scheduler also emits a execution event

var timr = require('timr');

var taskConstructor = timr();

//...create some tasks...

taskConstructor.scheduler.on('execute', function(name, task) {
    //universal handler for all tasks attached to the scheduler
});

task

modifiers

specify how often and when a task should be performed.

when

via the methods .from(timestamp) and .to(timestamp) the period of time can be specified in which the task should be performed. these methods can be used in every combination or can be omitted completely. (which would case a task to run instantly and indefinitely.

how often

the quantifier .every(n) in combination with a interval modifier like .hour(), .minute() etc defines how often a task gets executed.
the n parameter can be omitted, it defaults to 1. (task.every().minute() means that a task is executed once a minute)

for example the expression task.every(2).minutes() executes the task every 2 minutes.

there are five interval modifiers:
.second()
.minute()
.hour()
.day()
.month()

for every modifier also the plural form is valid. (e.g. .minutes() instead of .minute())

creation

tasks geht automatically attached to the parent scheduler object

var myTask = taskConstructor();

there are multiple ways of invoking a task.

anonymous (one callback)

creates the task, configures it to run every minute and runs the callback assigned in the run handler

taskConstructor().every().minute().run(function() { ... });

anonymous (multiple callbacks)

creates the task, configures it to run every minute and runs each callback.

taskConstructor()
  .every().minute()
  .run(function() { ... })
  .run(function() { ... })
  .run(function() { ... });

named (event handler attached to the task)

creates the task, configures it to run every minute and runs the callback assigned in the run handler.

run has to be called.

var myTask = taskConstructor().every().minute();

myTask.on('execute', function() {
  // ...
});

myTask.on('execute', function() {
  // ...
});

myTask.run();

event handler attached to the scheduler

creates two anonymous tasks, configures to run the first every minute and the second to run every second hour.

than the events emitted on the scheduler are used to consume the task events.

run has to be called.

taskConstructor().every().minute().run();
taskConstructor().every(2).hours().run();

taskConstructor.scheduler.on('execute', function(name, task) {
  // gets invoked every minute and every second hour
});

tests

npm test