Node.js 分布式作业队列:Martinet
jopen
10年前
Martinet 是分布式任务管理工具。
Martinet 数据库后端,基于 zeroMQ 的分布式任务管理系统。Martinet 可以使用任意的 sequelize.js 兼容的数据库(默认是 SQLite)。
Martinet 使用 push-pull 管理模式来确保在分布式的环境下高效的运行。
安装
npm install martinet
使用
Martinet 包括两个部分: Martinet
对象,处理调度和调度任务; Worker
对象,接受任务和定义。
Martinet
安装
var Martinet = require('martinet'); var martinet = new Martinet(); // Martinet allows you to create multiple workers // so that you can keep worker code in separate // logical modules.martinet.addWorker('WORKER_NAME_1', 'WORKER_PORT_1'); martinet.addWorker('WORKER_NAME_2', 'WORKER_PORT_2');
创建任务
martinet.execute({ worker: 'WORKER_NAME', name: 'task_name', description: 'Do a thing' // Used in the backend so it's easier to lookup tasks later }, args); // args JSON object of named arguments, so like // { // thing_id: 1 // } // // this object gets serialized and passed to the Worker //
martinet.schedule('in 20 minutes', { worker: 'WORKER_NAME', name: 'task_name', description: 'Do a thing in 20 minutes'}, args);
martinet.every('30 minutes', { worker: 'WORKER_NAME', name: 'task_name', description: 'Do a thing every half hour', run_at: 'midnight' // optional time to start the recurring task}, args);
Workers
安装
var MartinetWorker = require('martinet').Worker;var WORKER_PORT = 3000;var worker = new MartinetWorker(WORKER_PORT, { martinet_url: '127.0.0.1', martinet_port: '8089'});
定义任务
worker.on('task_name', function(taskId, data, callback) { // do a thing. // if it's successful, callback(), // if there's an error, callback(err)});
端口
Martinet 的 pull socket 监听端口:
var Martinet = require('martinet');var options = { port: 8009};var martinet = new Martinet(options);
DB
后端数据库的连接信息,使用 sequelize.js 选项。
默认:
var Martinet = require('martinet');var options = { db: { database: 'martinet-db', username: process.env.USER, password: null, options: { dialect: 'sqlite', storage: 'martinet.db', logging: false, omitNull: true }, sync: true }};var martinet = new Martinet(options);
使用 postgres:
var Martinet = require('martinet');var options = { db: { database: 'martinet-db', username: process.env.USER, password: null, options: { dialect: 'postgres', port: 5432, host: 'database.host' logging: false, omitNull: true }, sync: true }};var martinet = new Martinet(options);