New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

minionjs-backend-mongoose

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

minionjs-backend-mongoose

A Mongoose backend for minion.js

  • 0.1.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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 - info@ebruni.it

License

Licensed under GNU GPLv3

Keywords

FAQs

Package last updated on 07 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc