GithubHelp home page GithubHelp logo

emilianobruni / minionjs-backend-mongoose Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 311 KB

A Mongoose backend for minion.js

License: GNU General Public License v3.0

TypeScript 44.56% JavaScript 55.44%

minionjs-backend-mongoose's Introduction

Mongoose backend for minion.js

npm package Build workflow Coverage Status Last Commit Dependencies Downloads

A Mongoose Backend written in Typescript/ES6 for minion.js, a high performance job queue for Node.js

Installation

npm i minionjs-backend-mongoose -s

Usage

import Minion from '@minionjs/core';
import MongooseBackend from 'minionjs-backend-mongoose';

// Use the high performance MongoDB backend
const uri = 'mongodb://user:password@localhost:27017/database?authSource=admin'
const minion = new Minion({uri: uri}, {backendClass: MongooseBackend}));
// or
// await mongoose.connect(uri);
// const minion = new Minion(mongoose, { backendClass: MongooseBackend });

// Add tasks
minion.addTask('somethingSlow', async (job, ...args) => {
  console.log('This is a background worker process.');
});

// Add hook before job started
minion.addJobHook('job:before', (minion, job) => {
  minion.app.log.trace(`Performing job "${job.id}" with task "${job.task}"`);
});

// Enqueue jobs
await minion.enqueue('somethingSlow', ['foo', 'bar']);
await minion.enqueue('somethingSlow', [1, 2, 3], {priority: 5});

// Perform jobs for testing
await minion.enqueue('somethingSlow', ['foo', 'bar']);
await minion.performJobs();

// Start a worker to perform up to 12 jobs concurrently
const worker = minion.worker();
worker.status.jobs = 12;
await worker.start();

console.log("Worker started and is waiting for other jobs");

process.on('SIGINT', async () => {
  console.log("Stopping worker...");
  await worker.stop();
  console.log("Shutdown minion");
  await minion.end();
})

Or using mojo.js framework and typescript

// node script.js minion-worker -j 12
// to start a worker to perform up to 12 jobs concurrently

import { minionPlugin } from '@minionjs/core';
import mojo, { MojoApp } from '@mojojs/core';
import MongooseBackend from 'minionjs-backend-mongoose';

const uri = 'mongodb://user:password@localhost:27017/database?authSource=admin'

export const app: MojoApp = mojo();

app.plugin(minionPlugin, { config: { uri: uri }, backendClass: MongooseBackend });

const minion = app.models.minion;

// Add tasks
minion.addTask('somethingSlow', async (job, ...args) => {
    console.log('This is a background worker process with id %s and args %s', job.id, args);
});

// Add hook before job started
minion.addJobHook('job:before', (minion, job) => {
    minion.app.log.trace(`Performing job "${job.id}" with task "${job.task}"`);
});

// Enqueue jobs
await minion.enqueue('somethingSlow', ['foo', 'bar']);
await minion.enqueue('somethingSlow', [1, 2, 3], { priority: 5 });

// Perform jobs for testing
await minion.enqueue('somethingSlow', ['foo', 'bar']);
await minion.performJobs();

app.start();

console.log("App is started and is waiting for other jobs");

process.on('SIGINT', async () => {
    console.log("Shutdown minion");
    await minion.end();
})

Now send some other jobs to these workers.

import Minion from '@minionjs/core';
import MongooseBackend from 'minionjs-backend-mongoose';
import mongoose from 'mongoose';

const uri = 'mongodb://user:password@localhost:27017/database?authSource=admin'

await mongoose.connect(uri);
const minion = new Minion(mongoose, {backendClass: MongooseBackend});

const id = await minion.enqueue('somethingSlow', ['a', 'b', 'c']);
console.log("Enqueued job with id", id);

await minion.end();

and look to worker output to see that the job has been processed.

See Minion.js documentation for other examples.

Bugs / Help / Feature Requests / Contributing

Author

Emiliano Bruni - [email protected]

License

Licensed under GNU GPLv3

minionjs-backend-mongoose's People

Contributors

emilianobruni avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.