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

@map-colonies/cleanup-registry

v1.1.0

Published

This is a package for safe service cleanup

Downloads

97

Readme

cleanup-registry

a package for calling async functions to be resolved in a configured loop

Install

npm install @map-colonies/cleanup-registry

Usage

  1. import cleanup-registry:
import { CleanupRegistry } from '@map-colonies/cleanup-registry';
  1. create your registry with wished options:
const registry = new CleanupRegistry({
    overallTimeout: 3000,
    preCleanupHook: async (): Promise<void> => Promise.resolve(),
  });
  1. register your async functions:
registry.register({
    func: myAsyncFunc,
    id: 'funcId',
    timeout: 100,
    timeoutAfterFailure: 300
});
  1. optionally listen to your wanted events:
registry.on('itemCompleted', (id) => console.info({ msg: 'item completed', id }));
registry.on('itemFailed', (id, error, message) => console.error({ err: error, msg: message, id }));
registry.on('started', () => console.info('started'));
registry.on('finished', (status) =>
    status === 'success' ?
    console.info('finished successfully') :
    console.error({ msg: 'finished with bad status', status })
);
  1. trigger the registered functions
await registry.trigger();

How it works

The cleanup process as a whole:

flowchart LR
    A[Triggered] -->B{already triggered?}
    B -->|yes| C[AlreadyTriggeredError]
    B -->|no| D((Started))
    D -->E{PreCleanupHook?}
    E -->|yes| F[PreHook]
    F -->|resolves| G
    F -->|rejects| H{ignorePreError?}
    H -->|yes| G[Registry Cleanup]
    H -->|no| I((preFailed))
    E -->|no| G
    G -->J{overallExpired?}
    J -->|yes| K((timeout))
    J -->|no| L{PostCleanupHook?}
    L -->|yes| M[PostHook]
    L -->|no| P
    M -->|resolves| P((success))
    M -->|rejects| N{ignorePostError?}
    N -->|no| O((postFailed))
    N -->|yes| P

Cleaning up the registry will result in each item independent cleanup loop:

flowchart TB
    A{Overall Expired?}
    A -->|yes| B((timeout))
    A -->|no| C[callFunction]
    C -->|resolves| D((itemCompleted))
    C -->|rejests| E((itemFailed))
    E -->F{TimeoutError?}
    F -->|yes| A
    F -->|no| G[delay]
    G --> A

Configuration

RegistryOptions

  • overallTimeout: the duration in ms for all the registered functions to complete. when expired new function completion attempts will cease and a finished event with timeout status will be emitted. defaults to 10000.
  • preCleanupHook: an async function to be called before any of the registered functions will be called.
  • postCleanupHook: an async function to be called after all registered functions have completed.

ItemOptions

  • func: an async function for registration
  • id: the id to be attached to the item on itemCompleted, itemFailed events and remove function, could be a string or a symbol, if no id is provided a random uuid will be set.
  • timeout: the duration in ms for the function to be resolved in a single attempt, when expired itemFailed event will be emitted with a TimeoutError as error. defaults to 1000.
  • timeoutAfterFailure: the duration in ms for the function to be delayed after inner function rejection (not due to timeout), only when expired a new attempt will be made. defaults to 500.

TriggerOptions

  • ignorePreError: should ignore rejection by preCleanupHook, when falsy and preCleanupHook rejects a finished event with preFailed will be emitted and the cleanup process will stop. defaults to true.
  • ignorePostError: should ignore rejection by postCleanupHook, if falsy and postCleanupHook rejects a finished event with postFailed will be emitted. defaults to true.

Events

optionally listen to the following events:

  • started: the registry had started the cleanup process.
  • itemCompleted: a registered function promise has been resolved, the item id is provided.
  • itemFailed: a registered function promise had rejected, this could be due to inner function rejection or the item timed out. the item id, the rejecting error and possibly a message are provided.
  • finished: the cleanup process has completed, the finish status is provided. finish status is one of the following statuses:
    • success: all registered functions had completed successfully
    • timeout: the registry timeout had expired before all registered functions have completed
    • preFailed: the preCleanupHook had rejected
    • postFailed: the postCleanupHook had rejected

Tests

npm test