cluster0:power-queue

v0.0.12Published 2 months ago

zcfs:power-queue Build Status

Looking for maintainers - please reach out! This package is to be archived due to inability to find contributors, thanks to everyone who helped make it possible.

If you're looking for an alternative, we highly recommend Meteor-Files by VeliovGroup


PowerQueue is a native Meteor package for memory-backed job queue processing. Features include:

  • async tasks
  • throttling resource usage
  • retrying failed tasks
  • managing sub-queues
  • powered by Meteor's reactive sugar
  • etc.

PowerQueue can use one of two spinal-queue packages, ReactiveList or MicroQueue.

Demos

Check out the cool live queue demo and live sub queue example.

Source code for both can be found in the two branches of the power-queue-example repo.

Kind regards,
Eric(@aldeed) and Morten(@raix)

Happy coding!

API

All getters and setters are reactive.

API Documentation

Helpers / Getters / Setters:

  • PowerQueue.length - Number of tasks in queue
  • PowerQueue.progress - Current progress in percent
  • PowerQueue.usage - Current load in percent
  • PowerQueue.total - Sum of tasks to run in current queue
  • PowerQueue.isPaused - True if queue is paused
  • PowerQueue.isHalted - True if queue is paused or stopped
  • PowerQueue.processing - Number of tasks being processed
  • PowerQueue.errors - Failures where task is passed to the errorHandler
  • PowerQueue.failures - Number of failures in current queue
  • PowerQueue.isRunning - True if queue is active
  • PowerQueue.maxProcessing - Getter + Setter for max tasks to run in parallel
  • PowerQueue.autostart - Getter + Setter for autostart flag - Allow add task to start the queue
  • PowerQueue.maxFailures - Max allowed retries for failing tasks before marked as an error
  • options.queue - Use custom micro-queue compatible queue
  • options.onEnded - Called when queue has ended
  • options.onRelease(remainingTasks) - Called when queue has ended or paused
  • options.onAutostart - Called when queue was autostarted

Methods

  • PowerQueue.add(data) - Add a task to queue
  • PowerQueue.run() - Start the queue
  • PowerQueue.pause() - Pause the queue
  • PowerQueue.resume() - Resume the queue if paused
  • PowerQueue.reset() - Reset the queue
  • PowerQueue.taskHandler(data, next, failures) - Default task handler, where data is a function(done), can be overwritten
  • PowerQueue.errorHandler(data, addTask, failures) - Default error handler, can be overwritten

Example 1

1    var queue = new PowerQueue({
2      isPaused: true
3    });
4
5    queue.add(function(done) {
6      console.log('task 1');
7      done();
8    });
9    queue.add(function(done) {
10      console.log('task 2');
11      done();
12    });
13    queue.add(function(done) {
14      console.log('task 3');
15      done();
16    });
17
18    console.log('Ready to run queue');
19    queue.run();

Example 2

This is a very rough example of how to make custom task handling.

1
2  queue.errorHandler = function(data, addTask) {
3    // This error handler lets the task drop, but we could use addTask to
4    // Put the task into the queue again
5    tasks.update({ _id: data.id }, { $set: { status: 'error'} });
6  };
7
8  queue.taskHandler = function(data, next) {
9
10    // The task is now processed...
11    tasks.update({ _id: data.id }, { $set: { status: 'processing'} });
12
13    Meteor.setTimeout(function() {
14      if (Math.random() > 0.5) {
15        // We random fail the task
16        tasks.update({ _id: data.id }, { $set: { status: 'failed'} });
17        // Returning error to next
18        next('Error: Fail task');
19      } else {
20        // We are done!
21        tasks.update({ _id: data.id }, { $set: { status: 'done'} });
22        // Trigger next task
23        next();
24      }
25      // This async task duration is between 500 - 1000ms
26    }, Math.round(500 + 500 * Math.random()));
27  };
28
29  // Add the task:
30  var taskId = 0;
31  queue.add({ id: tasks.insert({ status: 'added', index: ++taskId }) });

Contribute

Here's the complete API documentation, including private methods.

To update the docs, run npm install docmeteor then docmeteor.

TODO / Wishlist