All Projects → kessler → Tempus Fugit

kessler / Tempus Fugit

A scheduling and time utilities module that doesn't waste your time

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Tempus Fugit

The Nodejs Master Class
Code samples for the Node.js Master Class
Stars: ✭ 1,037 (+1381.43%)
Mutual labels:  node-js
Cli Error Notifier
Sends native desktop notifications if CLI apps fail
Stars: ✭ 61 (-12.86%)
Mutual labels:  node-js
Clojure News Feed
evaluating various technologies by implementing a news feed micro-service
Stars: ✭ 65 (-7.14%)
Mutual labels:  node-js
Arpx
Automate and relate multiple processes.
Stars: ✭ 49 (-30%)
Mutual labels:  scheduling
Springboot
SpringBoot 整合各类框架和应用
Stars: ✭ 54 (-22.86%)
Mutual labels:  scheduling
Fhir.js
Node.JS library for serializing/deserializing FHIR resources between JS/JSON and XML using various node.js XML libraries
Stars: ✭ 61 (-12.86%)
Mutual labels:  node-js
Offline invoicing
Desktop invoicing app built with electron. Create Quotes and Invoices. Download PDF or Email directly to your customers.
Stars: ✭ 47 (-32.86%)
Mutual labels:  node-js
Tor Router
A SOCKS, HTTP and DNS proxy for distributing traffic across multiple instances of Tor
Stars: ✭ 69 (-1.43%)
Mutual labels:  node-js
Asciichart
Nice-looking lightweight console ASCII line charts ╭┈╯ for NodeJS, browsers and terminal, no dependencies
Stars: ✭ 1,107 (+1481.43%)
Mutual labels:  node-js
Waiter
Runs, manages, and autoscales web services on Mesos and Kubernetes
Stars: ✭ 65 (-7.14%)
Mutual labels:  scheduling
Influx Crypto Trader
Node js trading bot, let you create trading strategy and run it (backtest/simulation/live)
Stars: ✭ 49 (-30%)
Mutual labels:  node-js
Node Typescript Boilerplate
Minimalistic project template to jump start a Node.js back-end application in TypeScript. ESLint, Jest and type definitions included.
Stars: ✭ 1,061 (+1415.71%)
Mutual labels:  node-js
Traceshark
This is a tool for Linux kernel ftrace and perf events visualization
Stars: ✭ 63 (-10%)
Mutual labels:  scheduling
Bugsnag Node
[DEPRECATED] Please upgrade to our Universal JS notifier "@bugsnag/js" • https://github.com/bugsnag/bugsnag-js
Stars: ✭ 48 (-31.43%)
Mutual labels:  node-js
Komada
Komada: Croatian for `pieces`, is a modular bot system including reloading modules and easy to use custom commands.
Stars: ✭ 67 (-4.29%)
Mutual labels:  node-js
Grafana Prometheus Node Js Example
Step-by-step tutorial on creating beautiful dashboards for your Node JS application
Stars: ✭ 47 (-32.86%)
Mutual labels:  node-js
Documentbuilder
ONLYOFFICE Document Builder is powerful text, spreadsheet, presentation and PDF generating tool
Stars: ✭ 61 (-12.86%)
Mutual labels:  node-js
Node Julius
Node.js module for voice recognition using Julius
Stars: ✭ 69 (-1.43%)
Mutual labels:  node-js
Rhine
Haskell Functional Reactive Programming framework with type-level clocks
Stars: ✭ 69 (-1.43%)
Mutual labels:  scheduling
Frame Scheduling
Asynchronous non-blocking running many tasks in JavaScript. Demo https://codesandbox.io/s/admiring-ride-jdoq0
Stars: ✭ 64 (-8.57%)
Mutual labels:  scheduling

Tempus Fugit Build Status Join the chat at https://gitter.im/kessler/tempus-fugit

Tempus fugit is a Latin expression meaning "time flees", more commonly translated as "time flies". It is frequently used as an inscription on clocks.

This module contains high level api for scheduling jobs and also exposes utilities and classes to help build other more custom / complex scheduling code.

Install

	npm install tempus-fugit

Usage

Scheduling api

The scheduling api can be used to schedule single time or repeating jobs. Repeating jobs schedule is defined using the interval object (see below).

schedule a one time job in the future:
var schedule = require('tempus-fugit').schedule;

var futureDate = new Date(....);
function task() {}

var job = schedule(futureDate, task);

// can cancel
job.cancel();

job = schedule(1000, task); // schedule in 1 second from now
schedule a repeating / recurring job:
var schedule = require('tempus-fugit').schedule;

var interval = { hour: 1, minute: 5 }; // every hour and 5 minutes

// job.done() is not required when overlappingExecutions is true
function task(job) { 
	// this.done() also works
	// also job.callback() can be used to create a callback function instead, e.g fs.readFile('foo', job.callback())
	job.done(); 
}

var job = schedule(interval, task /*, {.. options ..} */);

// can cancel
job.cancel();
scheduling options:

unref: [boolean] (default false) setting this to true will issue automatic unref() on timers, which will allow the node process to exit when a task is run.

overlappingExecutions: [boolean] (default false) setting this to true will cause tasks to overlap if they dont finish before interval time elapses.

createOnly: [boolean] (default false) if set to true execute() will not be called, this means you will have to call job.execute() after shceduling.schedule(...)

the interval object:
var interval = {
	millisecond: 1,
	second: 2,
	minute: 3,
	hour: 4,
	day: 5,
	start: Date.now() + 10000 || new Date('some date in the future') //optional
}

The interval object supports all the time units displayed above, those can also be used in combination to create more complex intervals (e.g day + hour + second). When scheduling a task using an interval object, tempus-fugit will sync the execution cycle to the next round occurance of the interval.

For example, look at the following code:

schedule({ hour: 1 }, function task (job) { job.done() })

If initially run at 20:31, will execute task at 21:00, then 22:00, then 23:00 etc...

If we want to start the cycle right away we can use the optional start property:

schedule({ hour: 1, start: Date.now() }, function task (job) { job.done() })

If initially run at 20:31, will execute task at 20:31, then 21:31, 22:31 etc...

Creating new job "classes"
	var AbstractJob = require('tempus-fugit').AbstractJob;
	var $u = require('util');

	$u.inherits(MyJob, AbstractJob);
	function MyJob(task, options) {
		AbstractJob.call(this, task, options)
	}

	// must implement
	MyJob.prototype._executeImpl = function () {
		return setInterval(this._task, 500);
	};

	// must implement
	MyJob.prototype._cancelImpl = function(token) {
		return clearInterval(token);
	};

	// optionally implement, if so, do no pass task argument in constructor
	MyJob.prototype._task = function () {
		console.log('foo!');
	};


Interval util

tu.intervalObjectToMillis():
var tu = require('tempus-fugit').temporalUtil;

var interval = { millisecond: 500, second: 2 };

console.log(tu.intervalObjectToMillis(interval));

will print:

2500

tu.normalizeIntervalObject:
var tu = require('tempus-fugit').tu;

var interval = { millisecond: 1502, second: 2 };

console.log(tu.normalizeIntervalObject(interval));

will print:

{ millisecond: 502, second: 3 }

note: this will modify the original interval object

tu.intervalCountSinceEpoch:
var tu = require('tempus-fugit').tu;

var interval = { day: 1 };

var n = Date.UTC(2000, 0);

var millis = tu.intervalObjectToMillis(interval);

console.log(tu.intervalCountSinceEpoch(millis, n));

will print:

10957

which is 30 years * 365 day + 7(.5) days from leap years

note: the n argument is optional, if omitted the function will use Date.now() internally

tu.nextIntervalEvent:
var tu = require('tempus-fugit').tu;

var interval = { day: 1 };

var n = Date.UTC(2000, 0, 1, 0, 30); // Sat Jan 01 2000 00:30:00 GMT

var millis = tu.intervalObjectToMillis(interval);

var nextInterval = tu.nextIntervalEvent(millis, n);

console.log(new Date(nextInterval).toUTCString());

will print:

Sun, 02 Jan 2000 00:00:00 GMT

note: the n argument is optional, if omitted the function will use Date.now() internally

Date related util

tu.nextSecond(date);

tu.nextMinute(date);

tu.nextHour(date);

tu.nextDate(date);

tu.nextMonth(date);

tu.nextYear(date);

example
	var tf = require('tempus-fugit');

	var now = new Date(2013, 11, 25, 23, 23, 59, 123);
	var actual = tf.tu.nextSecond(now);  // tf.tu === tf.temporalUtil

	console.log('closest second:');
	console.log(now);
	console.log(actual);

will print:

Wed Dec 25 2013 23:23:59 GMT+0200 (Jerusalem Standard Time)

Wed Dec 25 2013 23:24:00 GMT+0200 (Jerusalem Standard Time)

A Pitfall

I should probably find a solution for this, but for now, if you run a SeriallyRepeatingJob like this one:

schedule({ /*some interval data*/ }, function (job) {})

Calling job.done() will created a timer. But in this case we don't, if there are no other pending tasks in the event loop the process will exit.

TODO


  • support month and year intervals, calculated correctly
  • throw exception from jobs if error event is not handled or ignore errors flag is not set
  • add more events to job
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].