3 Star 0 Fork 0

Gitee 极速下载 / neuron-js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/flatiron/neuron
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Neuron Build Status

The simplest possible event driven job manager, FIFO queue, and "task based cache" in node.js

Usage

Neuron is a simple job queue with support for granular concurrency and persistent worker storage. It provides a way to manage jobs as they are created and completed in an async, event-driven manner. Heuristics for parallelization, ordering, and pooling are simple right now and jobs are processed in a FIFO order.

Managing Jobs

Managing jobs in neuron is easy. Neuron doesn't assume anything about the internal structure of the properties for each of your jobs except that they have a function called work(). The concurrency property is also useful but optional. If it isn't specified, neuron defaults to running 50 concurrent jobs.

Here's a quick sample of managing a single job called listDir with neuron.

  var util = require('util'),
      neuron = require('neuron');

  //
  // Create the manager and set the job.
  //
  var manager = new neuron.JobManager();
  manager.addJob('listDir', {
    dirname: __dirname,
    concurrency: 25,
    work: function (dirname) {
      var self = this;
      exec('ls -la ' + dirname || this.dirname, function (error, stdout, stderr) {
        if (error) self.error = error;
        else self.stdout = stdout;

        //
        // Finish the job, this will notify the manager.
        //
        self.finished = true;
      });
    }
  });

Working with and Finishing Job instances

A JobManager will create a worker for the specified Job associated (i.e. add it to the job queue) each time the enqueue() method is called. All parameters passed to the enqueue method are passed on to the Job work() function.

A Job function is 'finished' when it sets this.finished = true. This raises an event which is handled by the manager and re-emitted for the programmer. So when a worker completes, the JobManager raises the finish event:

  //
  // Start a worker and listen for finish
  //
  manager.on('finish', function (job, worker) {
    //
    // Log the result from the worker (the directory listing for '/')
    //
    console.dir(worker.stdout);
  });

  //
  // All arguments passed to the enqueue() function after the job name
  // are consumed by the work() function passed to the job.
  //
  manager.enqueue('listDir', '/');

Using the Persistent WorkerCache

Neuron has a built-in WorkerCache that stores the ordering and arguments to your workers for single instance durability. You don't have to worry about all the cache consistency nonsense though, just include the cache property when creating a JobManager.

  var manager = new neuron.JobManager({
    cache: {
      host: 'localhost',
      port: 6379
    }
  });

  manager.addJob('delayAdd', {
    work: function (a, b, c) {
      var self = this;
      setTimeout(function () {
        self.result = a + b + c;
        self.finished = true;
      }, 1000);
    }
  });

If there are any workers stored in your Redis server, you can load them by calling manager.load(). The manager will emit the load event when it is finished. Make sure that you have already added your jobs to your neuron JobManager before calling load or they will not be placed in the queue for that job.

  manager.on('finish', function (job, worker) {
    //
    // Log the output from the delayed addition
    //
    console.log(worker.result);
  });

  manager.on('load', function () {
    console.log('This will be called before any `finish` events');
  })

  manager.load();

Installing neuron

  $ [sudo] npm install neuron --save

Authors: Donovan Buck, Charlie Robbins

Copyright (c) 2010 Donovan Buck, Charlie Robbins & the Contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

neuron 是一款 Node.js 中的事件驱动的,使用FIFO队列,”基于缓存任务“ 的作业管理器 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/mirrors/neuron-js.git
git@gitee.com:mirrors/neuron-js.git
mirrors
neuron-js
neuron-js
master

搜索帮助