Giter Site home page Giter Site logo

martinet's Introduction

Martinet

Distributed task management.

Martinet is a database-backed, zeroMQ-based distributed task management system. It is persistent with respect to future and recurring tasks, so if your system goes down, those tasks will be unaffected. Martinet can use any sequelize.js compatible database as its backing database (SQLite is used by default).

Martinet uses a push-pull messaging pattern to ensure efficiency when used in a distributed environment.

Installation

npm install martinet

Usage

This library is divided into two parts: the Martinet object, which handles dispatching and scheduling tasks, and the Worker object which receives said tasks and defines the actions to take upon being given certain tasks.

Martinet

Setup

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');

Creating Tasks

Execute a task immediately
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
//
Execute a task in the future
martinet.schedule('in 20 minutes', {
    worker: 'WORKER_NAME',
    name: 'task_name',
    description: 'Do a thing in 20 minutes'
}, args);
Create a recurring task
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

Setup

var MartinetWorker = require('martinet').Worker;

var WORKER_PORT = 3000;
var worker = new MartinetWorker(WORKER_PORT, {
    martinet_url: '127.0.0.1',
    martinet_port: '8089'
});

Defining Tasks

worker.on('task_name', function(taskId, data, callback) {
    // do a thing.
    
    // if it's successful, callback(),
    // if there's an error, callback(err)

});

Options

Martinet

Port

Custom port for martinet's pull socket to listen on.

var Martinet = require('martinet');

var options = {
    port: 8009
};

var martinet = new Martinet(options);

DB

Connection information to the backing database. Uses sequelize.js options.

default is

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);

but for example to use 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);

Worker

Martinet URL

Connection string to connect to martinet. If worker is on the same machine as martinet, this should be 127.0.0.1

Martinet PORT

The port to connect to martinet on. This should be the same port defined by the martinet object's port option.

martinet's People

Contributors

mathisonian avatar jgable avatar nathasm avatar

Watchers

skyformat99 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.