Code Monkey home page Code Monkey logo

node-celery's Introduction

Celery client for Node.js

NPM Version Downloads

Celery is an asynchronous task/job queue based on distributed message passing. node-celery allows to queue tasks from Node.js. If you are new to Celery check out http://celeryproject.org/

Usage

Simple example, included as examples/hello-world.js:

var celery = require('node-celery'),
	client = celery.createClient({
		CELERY_BROKER_URL: 'amqp://guest:guest@localhost:5672//',
		CELERY_RESULT_BACKEND: 'amqp'
	});

client.on('error', function(err) {
	console.log(err);
});

client.on('connect', function() {
	client.call('tasks.echo', ['Hello World!'], function(result) {
		console.log(result);
		client.end();
	});
});

Note: When using AMQP as result backend with celery prior to version 3.1.7 the result queue needs to be non durable or it will fail with a: Queue.declare: (406) PRECONDITION_FAILED.

var celery = require('node-celery'),
	client = celery.createClient({
		CELERY_TASK_RESULT_DURABLE: false
	});

For RabbitMQ backends, the entire broker options can be passed as an object that is handed off to AMQP. This allows you to specify parameters such as SSL keyfiles, vhost, and connection timeout among others.

var celery = require('node-celery'),
	client = celery.createClient({
		CELERY_BROKER_OPTIONS: {
			host: 'localhost',
			port: '5672',
			login: 'guest',
			password: 'guest',
			authMechanism: 'AMQPLAIN',
			vhost: '/',
			ssl: {
				enabled: true,
				keyFile: '/path/to/keyFile.pem',
				certFile: '/path/to/certFile.pem',
				caFile: '/path/to/caFile.pem'
			}
		},
		CELERY_RESULT_BACKEND: 'amqp'
	});

ETA

The ETA (estimated time of arrival) lets you set a specific date and time that is the earliest time at which your task will be executed:

var celery = require('node-celery'),
	client = celery.createClient({
		CELERY_BROKER_URL: 'amqp://guest:guest@localhost:5672//',
	});

client.on('connect', function() {
	client.call('send-email', {
		to: '[email protected]',
		title: 'sample email'
	}, {
		eta: new Date(Date.now() + 60 * 60 * 1000) // an hour later
	});
});

Expiration

The expires argument defines an optional expiry time, a specific date and time using Date:

var celery = require('node-celery'),
	client = celery.createClient({
		CELERY_BROKER_URL: 'amqp://guest:guest@localhost:5672//',
	});

client.on('connect', function() {
	client.call('tasks.sleep', [2 * 60 * 60], null, {
		expires: new Date(Date.now() + 60 * 60 * 1000) // expires in an hour
	});
});

Backends

The backend is used to store task results. Currently AMQP (RabbitMQ) and Redis backends are supported.

var celery = require('node-celery'),
	client = celery.createClient({
		CELERY_BROKER_URL: 'amqp://guest:guest@localhost:5672//',
		CELERY_RESULT_BACKEND: 'redis://localhost/0'
	});

client.on('connect', function() {
	var result = client.call('tasks.add', [1, 2]);
	setTimeout(function() {
		result.get(function(data) {
			console.log(data); // data will be null if the task is not finished
		});
	}, 2000);
});

AMQP backend allows to subscribe to the task result and get it immediately, without polling:

var celery = require('node-celery'),
	client = celery.createClient({
		CELERY_BROKER_URL: 'amqp://guest:guest@localhost:5672//',
		CELERY_RESULT_BACKEND: 'amqp'
	});

client.on('connect', function() {
	var result = client.call('tasks.add', [1, 2]);
	result.on('ready', function(data) {
		console.log(data);
	});
});

Routing

The simplest way to route tasks to different queues is using CELERY_ROUTES configuration option:

var celery = require('node-celery'),
	client = celery.createClient({
		CELERY_BROKER_URL: 'amqp://guest:guest@localhost:5672//',
		CELERY_ROUTES: {
			'tasks.send_mail': {
				queue: 'mail'
			}
		}
	}),
	send_mail = client.createTask('tasks.send_mail'),
	calculate_rating = client.createTask('tasks.calculate_rating');

client.on('error', function(err) {
	console.log(err);
});

client.on('connect', function() {
	send_mail.call([], {
		to: '[email protected]',
		title: 'hi'
	}); // sends a task to the mail queue
	calculate_rating.call([], {
		item: 1345
	}); // sends a task to the default queue
});

node-celery's People

Contributors

bitdeli-chef avatar carlhoerberg avatar cjhubert avatar debel avatar esco avatar hidetaka-f-matsumoto avatar hmit avatar jaysoffian avatar jluyau avatar ksachdeva avatar mher avatar shrinath-shenoy avatar

Watchers

 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.