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

que-it

v1.0.0

Published

Background job processing inspired by laravel.

Downloads

5

Readme

QUE-IT

Background job processing inspired by laravel.

build status Coverage Status node version NPM version David deps

USAGE

Sending contact mail example.

You need to add jobs directory in your projects package.json file.

{
  ...
  "queit": {
    "jobsDir": "path/test/jobs"
  }
}

Then create a job class ContactMail.js in decalred directory.

Note: File name should match the class name.

const { Job } = require('que-it');

class ContactMail extends Job {
  constructor({ to }) {
    this.to = to;
    /*
    bull JobOptions
    this.$opts = {
      priority: number;
      delay: number;
      attempts: number;

      repeat: {
        tz?: string,
        endDate?: Date | string | number
      }
    };
    */
  }

  async run() {
    // do some fun
    // SomeMailingPackage.to(this.to).send('Hi!');
  }
}

module.exports = ContactMail;

In your controller

const { dispatch } = require('que-it');
const ContactMail = require('path/test/jobs');

// do some code

dispatch(new ContactMail({ to: '[email protected]' }));

dispatch method will push your job on bull queue.

In order to start processing queue, you need to start worker as background process with forever/pm2 or what ever process manager you like.

Worker file example.

const { worker, config } = require('que-it');

// add queue names used in project to worker
config.addQueue('default');

// passing options
// config.addQueue('default', {redis: {port: 6379, host: '127.0.0.1', password: 'foobared'}});

// will return the object of queues created by worker
// Single process:
const queues = worker.start();

// You can use concurrency as well:
// const queues = worker.start({ concurrency : 5 });
// and name your processor
// const queues = worker.start({ name: 'myProcessor', concurrency : 5 });

queues['default'].on('completed', () => {
  // bull queue event
});

Worker will create logs directory in current working directory to save job processing logs.

Adding multiple queues

config.addQueue('default');
config.addQueue('encoder');
config.addQueue('mailer');

Dispatching job on specific queue

dispatch(new ContactMail({ to: '[email protected]' }).on('mailer'));

Process job without queueing.

const { dispatch } = require('que-it');
const ContactMail = require('path/test/jobs');

// do some code

dispatch(new ContactMail({ to: '[email protected]' }).now());

Configure

Changing default queue drivers.

const { config } = require('que-it');
// every job will be processed immediately instead of queue in case of sync drivers
config.set('driver', 'sync');

// queueable driver
// config.set('driver', 'bull');

Note: Producer, Worker and Processor each runs as seperate process, on fly config in one does not effect another, considure using file based config.

Creating config file

config.set('driver', 'sync');
config.setJobsDir('./app/jobs');
config.save();

Adding jobs from different directories

config.addJob('PasswordReset', './modules/auth/PasswordReset.js');

Que-it considers job as independent module. In case of using database, you have to write a boot file for that.

Example with mongodb:

// boot.js
// configure env variables
require('dotenv').config();
const mongoose = require('mongoose');
// db connection
mongoose.connect(process.env.MONGODB, { useNewUrlParser: true }, (err) => {
  if (err) {
    console.error(err);
  }
});

You need to register boot file in package.json of your project.

{
  ...
  "queit": {
    "jobsDir": "path/test/jobs",
    "booter": "./boot.js"
  }
}

By using this boot file, you will be able to use mongoose models in you jobs. Make sure to require used models (populate models also) in job files otherwise mongoose will throw "MissingSchemaError: Schema hasn't been registered for model" this error.

For more configuration, read Bull documentation.