Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added many.js example

  • Loading branch information...
commit 984687856013437a3bf6975d5da374d836bd5fe9 1 parent 307d016
@tj tj authored
Showing with 159 additions and 0 deletions.
  1. +51 −0 examples/many.js
  2. +108 −0 test.js
View
51 examples/many.js
@@ -0,0 +1,51 @@
+
+var kue = require('../')
+ , express = require('express');
+
+// create our job queue
+
+var jobs = kue.createQueue();
+
+function create() {
+ var name = ['tobi', 'loki', 'jane', 'manny'][Math.random() * 4 | 0];
+ jobs.create('video conversion', {
+ title: 'converting ' + name + '\'s to avi'
+ , user: 1
+ , frames: 200
+ }).save();
+ setTimeout(create, Math.random() * 3000 | 0);
+}
+
+create();
+
+function create2() {
+ var name = ['tobi', 'loki', 'jane', 'manny'][Math.random() * 4 | 0];
+ jobs.create('email', {
+ title: 'emailing ' + name + ''
+ , body: 'hello'
+ }).save();
+ setTimeout(create2, Math.random() * 1000 | 0);
+}
+
+create2();
+
+// process video conversion jobs, 3 at a time.
+
+jobs.process('video conversion', 2, function(job, done){
+ console.log('video');
+ setTimeout(done, Math.random() * 5000);
+});
+
+// process 10 emails at a time
+
+jobs.process('email', 10, function(job, done){
+ console.log('email');
+ setTimeout(done, Math.random() * 2000);
+});
+
+// start the UI
+var app = express.createServer();
+app.use(express.basicAuth('foo', 'bar'));
+app.use(kue.app);
+app.listen(3000);
+console.log('UI started on port 3000');
View
108 test.js
@@ -0,0 +1,108 @@
+
+var kue = require('./');
+
+ var jobs = kue.createQueue();
+
+ jobs.process('ping', 100, function(job, done){
+ done();
+ })
+
+ // create packet
+ var length = 64;
+ var packet = '', n = 0;
+ for (var i = 0; i < length; i++) packet += 'a';
+
+ // start ping
+ setInterval(function(){
+
+ // closure on 'n'
+ -function(n) {
+
+ var start = new Date;
+ var job = jobs.create('ping', packet).save();
+
+ job.on('complete', function(){
+ var ms = new Date - start;
+ console.log('%d bytes from kue: icmp_seq=%d time=%d ms', packet.length, n, ms);
+ });
+
+ }(n++)
+
+
+ }, 100)
+
+// cluster = cluster()
+// .use(cluster.debug())
+// .set('workers', 2)
+// .start();
+//
+// if (cluster.isMaster) {
+// // start the UI
+// kue.app.listen(3000);
+// console.log('UI started on port 3000');
+// } else {
+//
+// // create our job queue
+//
+// var jobs = kue.createQueue();
+//
+// // start redis with $ redis-server
+//
+// // create some jobs at random,
+// // usually you would create these
+// // in your http processes upon
+// // user input etc.
+//
+// function create() {
+// var name = ['tobi', 'loki', 'jane', 'manny'][Math.random() * 4 | 0];
+// var job = jobs.create('video conversion', {
+// title: 'converting ' + name + '\'s to avi'
+// , user: 1
+// , frames: 200
+// });
+//
+// job.on('complete', function(){
+// console.log(" Job complete");
+// }).on('failed', function(){
+// console.log(" Job failed");
+// }).on('progress', function(progress){
+// process.stdout.write('\r job #' + job.id + ' ' + progress + '% complete');
+// });
+//
+// job.save();
+//
+// setTimeout(create, Math.random() * 200 | 0);
+// }
+//
+// create();
+//
+// // process video conversion jobs, 1 at a time.
+//
+// jobs.process('video conversion', 1, function(job, done){
+// var frames = job.data.frames;
+//
+// function next(i) {
+// // pretend we are doing some work
+// convertFrame(i, function(err){
+// if (err) return done(err);
+// // report progress, i/frames complete
+// job.progress(i, frames);
+// if (i >= frames) done()
+// else next(i + Math.random() * 10);
+// });
+// }
+//
+// next(0);
+// });
+//
+// function convertFrame(i, fn) {
+// if (Math.random() > .99) {
+// fn(new Error('fail'));
+// fn();
+// } else {
+// setTimeout(fn, Math.random() * 50);
+// }
+// }
+// }
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.