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

@ededejr/task-tracker

v0.2.0

Published

A small utility for tracking task execution

Downloads

1

Readme

@ededejr/task-tracker

A small utility for tracking task execution.

Installation

npm install @ededejr/task-tracker

Usage

TaskTracker.run

The run method is the easiest and most consistent way to get started using the TaskTracker. It handles a bunch of functionality around running tasks, and utilizes best practices while providing an accessible API.

Let's take a look at a basic example, which will run your task and print out some logs to the console:

import TaskTracker from '@ededejr/task-tracker';

const tracker = new TaskTracker({
  log: (message: string) => console.log(message),
});

async function downloadEmails() {
  return tracker.run(async () => await EmailService.downloadEmails());
}

Tracing

Using the run method keeps a history of all events, which can be useful for tracing (this can be disabled). This history can be accessed with the TaskTracker.history property. In real world use cases it may be more practical to consume the history before items are deleted from memory. This process of deletion is called "reclaiming", and memory is reclaimed when the maxHistorySize is reached.

The following is an example which allows persisting the history on reclaim:

import TaskTracker from '@ededejr/task-tracker';

const tracker = new TaskTracker({
  maxHistorySize: 3, // Persist every 3 entries
  persistHistory: (entries) => {
    for (const entry of entires) {
      appendToFile(JSON.stringify(entry));
    }
  },
});

async function processEmails(emails: Email) {
  for (const email of emails) {
    tracker.run(async () => await EmailService.process(email));
  }
}

Persisting is also possible on insertion, which may be preferred to prevent data loss if the program exits before reclaiming occurs:

import TaskTracker from '@ededejr/task-tracker';

const tracker = new TaskTracker({
  maxHistorySize: 100, // The size of the ledger before being reclaimed
  persistEntry: (entry) => {
    // publish to remote source, or save to disk...
    appendToFile(JSON.stringify(entry));
  },
});

async function processEmails(emails: Email) {
  for (const email of emails) {
    tracker.run(async () => await EmailService.process(email), {
      name: 'processEmail',
    });
  }
}

The above examples will generate a convenient log format:

{
  "data": {
    "id": "214013d1-6f3e-40bc-9b0f-4106d29e46af",
    "signature": "214013d1-6f3e-40bc-9b0f-4106d29e46af::processEmail",
    "taskName": "processEmail",
    "message": "start"
  },
  "index": 190,
  "timestamp": 1674056687784
}

These logs can also include the name of the TaskTracker if supplied:

import TaskTracker from "@ededejr/task-tracker";

const tracker = new TaskTracker({
  name: "Renderer",
  maxHistorySize: 1 00,
  persistEntry: (entry) => {
    appendToFile(JSON.stringify(entry));
  },
});

will generate:

{
  "data": {
    "id": "214013d1-6f3e-40bc-9b0f-4106d29e46af",
    "signature": "Renderer::214013d1-6f3e-40bc-9b0f-4106d29e46af::processEmail",
    "tracker": "Renderer",
    "taskName": "processEmail",
    "message": "start"
  },
  "index": 190,
  "timestamp": 1674056687784
}

The examples folder contains a script which uses the run method to generate a log file using some of the concepts discussed here.

Manually tracking tasks

In some situations you may want to track a task independent of function execution. This could be business logic that requires a number of different operations before being completed.

This is intentionally left open-ended to allow consumers decide what works best for their use case.

const task = tracker.start();
// some stuff later...
const duration = task.stop();

Important: Ensure the stop function is called for your tasks to prevent memory build up.

The TaskTracker can be also be used in combination with other methods to accomplish more functionality. A good example of this is including a logger:

import TaskTracker from '@ededejr/task-tracker';

const tracker = new TaskTracker();

// A convenient wrapper for tasks
function startTask(name: string) {
  log(`start: ${name}`);
  const { stop } = tracker.start();
  return {
    stop: () => {
      log(`stop: ${name} ${stop().toPrecision(2)}`);
    },
  };
}

function main() {
  // create a task
  const readFileTask = startTask('read file');

  // do some work

  // stop the task
  readFileTask.stop();
}