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

yocto-daemon

v1.2.0

Published

Async tool to emulate an app like a system daemon

Downloads

13

Readme

NPM

alt text Code Climate Test Coverage Issue Count Build Status

Overview

This module is a part of yocto node modules for NodeJS.

Please see our NPM repository for complete list of available tools (completed day after day).

This module is an async tool to emulate an app like a system daemon without setTimeout or setInterval method for the core of process.

Motivation

After some analysis of logs and memory usage on a program who works like a system daemon we decided to forget setInterval and setTimeout usage because memory leak it's important during huge action.

So we decided to create an app that use a queue system based on a populate & execution methods.

How it works

This module start a queue process based on given delay (default 10 seconds).

It use a given populate function to retrieve data and a given execute function to process extra things (update, etc ...).

When queue will processed the app will be sleep during given delay.

How to use

var logger      = require('yocto-logger');
var daemon      = require('yocto-daemon')(logger);
var _           = require('lodash');
var Q           = require('q');

// Create a populate function
function pfn() {

  // create an async process here
  var deferred = Q.defer();

  // Do your get data here
  // And when all is ok
  if (foo) {
    deferred.resolve(bar);
  } else {
    deferred.reject(bar);  
  }

  // return promise is required
  return deferred.promise;
}
// Create an execution method 
function efn(data) {
  // create an async process here
  var deferred = Q.defer();

  // Do your extra process here
  // And when all is ok
  if (foo) {
    deferred.resolve(bar);
  } else {
    deferred.reject(bar);  
  }

  // return promise is required
  return deferred.promise;
}

// set your populate function on your daemon system
daemon.use(pfn);
// set your execution function on your daemon system
daemon.use(efn, true);
// set your nb allowed retry 
daemon.retry(10);
// set your delay between each populate
daemon.delay(10);

// check is your daemon is ready
if (daemon.isReady(true)) {

  // start your daemon
  daemon.start();
}

How to pass context on populate and execute method

Your can also set context of your populate and execute method. For this You must set the third parameters of use method.

// set your populate function on your daemon system
daemon.use(pfn, false, CONTEXT_HERE);
// set your execution function on your daemon system
daemon.use(efn, true, CONTEXT_HERE);

How to use priority on queue

By default pour populate method can return anything in what structure you want.

If you want use priority your populate method must return an object like :

{
  data      : 'YOUR_DATA_TO_EXECUTE_HERE', // WHAT YOU WANT HERE OBJ, ARRAY, ETC
  priority  : 'YOUR_PRIORITY_HERE', // MUST BE AN INTEGER
  callback  : function () {
    // A CALLBACK METHOD => PREFER USE YOUR OWN CALLBACK BY DEFAULT WE USE AN INTERNAL CALLBACK METHOD
  }
}

Available methods

  • start() : start daemon
  • pause() : pause current queue WARNING : In this function we use setTimeout, this will be remove in a future version
  • resume() : resume current queue
  • clean() : clean current queue
  • use(Function, Boolean) : to set populate and execute function on daemon. the second param must to set at true to set execute method
  • lessWorkers() : add less workers on queue to manage concurrency limit
  • moreWorkers(Integer) : add more workers on queue to manage concurrency limit. if integer is not defined default value (10) must be use

Logging in tool

By Default this module include yocto-logger for logging. It's possible to inject in your router instance your current logger instance if is another yocto-logger instance.

Changelog

All history is here