Giter VIP home page Giter VIP logo

node-sqlite-queue's Introduction

node-sqlite-queue

  • SQLite-backed job queue for Node.js.

SQLite version of monq, Reference monq

Install

npm i node-sqlite-queue --save

Usage

producer

const sqliteQ = require('node-sqlite-queue');
const client = sqliteQ('./db_job.db');

const queueSign = 'foo'; // 队列唯一标识
const queue = client.queue(queueSign);

const queueHandleMethodName = 'testMethod'; // 队列消息消费逻辑方法名
const msg = { text: 'test enqueue' };

const msgOptions = { 
    attempts: { count: 3, delay: 3000, strategy: 'exponential' }, 
    timeout: 5000, 
    priority: 3 
};

queue.enqueue(queueHandleMethodName, msg, msgOptions, function (err, job) {
    if (err) {
        console.log('error:', err);
    } else {
        console.log('success:', job.data);
        process.exit();
    }
});

consumer

const sqliteQ = require('node-sqlite-queue');
const client = sqliteQ('./db_job.db');

const queueSign = 'foo'; // 队列唯一标识
const worker = client.worker([queueSign]);

worker.register({
    /**
     * 
     * @param {*} params 消息对象
     * @param {*} callback 
     */
    testMethod: function (params, callback) {
        try {
            console.log('处理消息', params);
            callback(null, params);
        } catch (err) {
            callback(err); // 触发失败
        }
    }
});

worker.on('dequeued', function (data) {
    console.log('消费', data);
});
worker.on('failed', function (data) {
    console.log('消费失败', data);
});
worker.on('complete', function (data) {
    console.log('消费完成', data);
});
worker.on('error', function (err) {
    console.log('消费出错', err);
    worker.stop();
});

worker.start();

node-sqlite-queue's People

Contributors

sinkhaha avatar

Stargazers

Matt Ma avatar  avatar

Watchers

 avatar

node-sqlite-queue's Issues

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.