Beispiel #1
0
			function(callback){
				flow.parallel([
					function(callback){//查询单个文档
						query.exec(function(error,result){
							if (error) return console.error(error);

							doc=result;
							callback();
						});
					},
					function(callback){//热门文章
						models.Article.find({},function(error,result){
							if (error) return console.error(error);

							hotPosts=result;
							callback();
						}).limit(5).sort({views:-1});
					},
					function(callback){//查询评论
						models.Comment.find({articleId:id,through:true},function(error,result){
							if (error) return console.error(error);

							comments=result;
							callback();
						}).limit(10).sort({time:-1});
					}

				],callback);
			},
        function(cback) {
            nimble.parallel([
                function (cback) {
                	writeFile(originalFilePath, originalFileSaved, function() {
                    	cback();
					});
                }
                ,
                function(cback) {

                	generateThumbnails(originalFilePath, originalFileSavedFileName , saveDir, sizeList, function(orgiImagesAndThumbnails) {
                		
                		if (orgiImagesAndThumbnails) {
                			for (var i = 0; i < orgiImagesAndThumbnails.length; i++) {
                				orgiImagesAndThumbnails[i] = convertFilePathToUrl(staticHost, approot, channel, orgiImagesAndThumbnails[i]);
                			} 

                			result.images = orgiImagesAndThumbnails;
                		}

                    	cback();
                	});
                }
            ], cback);
        },
			    function(callback) {
			        nimble.parallel([
			            function(callback){
			                getUserInfo('qmjjr', brokerId, function(err, result) {
			                	userinfo = result;
			                	callback();
			                });
			            }, function(callback) {
			            	if (type == 'card') {
								download(oldUrl, newPath, function(err, rtnResult){
				                	if (err) {
				                		is404 = true;
				                	}
									callback();
								});
							}

							if (type == 'cert') {
								download(oldUrl[0], newPath[0], function(err, rtnResult){
				                	if (err) {
				                		is404 = true;
				                		callback();
				                	} else {
				                		download(oldUrl[1], newPath[1], function(err, rtnResult){
						                	if (err) is404 = true;
											callback();
										});
				                	}									
								});

								
							}	                
			            }
			        ], callback);
			    },
 function (callback) {
   flow.parallel([ 
     function (callback) {
       console.log('Downloading Node v0.4.6...');
       downloadNodeVersion('0.4.6', '/tmp', callback);
     },
     function (callback) {
       console.log('Downloading Node v0.4.7...');
       downloadNodeVersion('0.4.7', '/tmp', callback);
     }
   ], callback);
 },
Beispiel #5
0
	function (callback) {
		flow.parallel([
			function (callback) {
				console.log('Downing 1 img...');
				downloadImg('http://img1.gamersky.com/image2014/11/20141101ge_5/image010.jpg', '/tmp', callback);
			},
			function (callback) {
				console.log('Downing 2 img...');
				downloadImg('http://img1.gamersky.com/image2014/10/20141030ge_4/gamersky_004origin_007_2014103017588B8.jpg', '/tmp', callback);
			}
			], callback);
	},
        function(cback) {
            nimble.parallel([
                function (cback) {
					sizeOf(originalFilePath, function(err, dimensions) {
						orgiWidth 	= dimensions.width;
						orgiHeight 	= dimensions.height;
						cback();
					});
                },
                function(cback) {	                	
					generateBackgroundImg(width, height, function(result) {
						bgNew = result;
						cback();
					});
                }
            ], cback);
        },
	        function(cback) {
	            nimble.parallel([
	                function (cback) {
						writeEncryptImage(user, originalFilePath, encryptFileSaved, function () {
  							cback();
  						});
	                },
	                function(cback) {	                	
						writeSpreadImageAndThumbnails(staticHost, originalFilePath, originalFileSaved, saveDir, approot, channel, sizeList, function (rtnResult) {
							result.spread 	= rtnResult.spread;
							result.original = rtnResult.spread;		// 模糊处理后的图片作为原图
							result.images 	= rtnResult.images;

							cback();
						});
	                }
	            ], cback);
	        },
    function(callback) {
        nimble.parallel([
            function(callback){
    //             getAccessToken('18500183080', '111111', targetProject.channel, '3', function(token) {	
				// 	currentToken 	= token;
				// 	callback();
				// });

				callback();
            }, function(callback) {
                getConnection(targetProject, function(connection) {
					query(connection, querySql, function(err, result) {
						queryReqult 	= result;
						callback();
					});
				});
            }
        ], callback);
    },
    function(callback) {
        console.log('Creating archive of downloaded files...');
        exec(
            'tar cvf node_distros.tar ./tmp/0.4.6.tqz ./tmp/0.4.7.tqz',
            function (error, stdout, stderr) {
                console.log('All done!');
                callback();
            }
        );
        flow.parallel([
            function(callback) {

                downloadNodeVersion('0.4.6', './tmp', callback);
            },
            function(callback) {
                console.log('Downloading Node v0.4.7...');
                downloadNodeVersion('0.4.7', './tmp', callback);
            }
        ], callback);
    }
Beispiel #10
0
			function(callback){
				flow.parallel([//并行
					function(callback){//分页
						models.Article.find({},function(error,result){
							if (error) return console.error(error);							

							docs=result;
							callback();
							
						}).skip(skipNum).limit(pageSize).sort({_id:-1});
					},
					function(callback){//热门文章
						models.Article.find({},function(error,result){
							if (error) return console.error(error);

							hotPosts=result;
							callback();
						}).limit(5).sort({views:-1});
					}
				],callback);
			},
Beispiel #11
0
var _ = require('nimble')

var a,b

_.parallel([
    function (callback) {
        setTimeout(function () {
            console.log('one');
            a=3
        }, 25);
    },
    function (callback) {
        setTimeout(function () {
            console.log('two');
            b=4
        }, 0);
    }
],()=>console.log('end'));
Beispiel #12
0
	}
])

/*
 * 使用parallel并行控制流程:callback回调参数不可省略
 */
flow.parallel([
	function (callback){
		setTimeout(function(){
			console.log('parallel 1 .');
			callback();
		},1000);
	},
	function (callback){
		setTimeout(function(){
			console.log('parallel 2 . ');
			callback();
		},1000);
	},
	function (callback){
		setTimeout(function(){
			console.log('parallel 3 . ');
			callback();
		},1);
	}
])

/*
 * each迭代对象
 */
flow.each([1,2,3],function(val){
	console.log(val);
Beispiel #13
0
                 findMultifeedByNameOrId(res, multifeedNameOrId, 'querySpec', function(multifeed) {
                    // inflate the querySpec
                    multifeed.querySpec = JSON.parse(multifeed.querySpec);

                    // build functions to find the feeds described by each item in the querySpec.  We'll execute them
                    // in parallel, combining the results which we'll then use to build the call to the datastore.
                    var queryFunctions = [];
                    var feedsAndChannels = [];
                    var errors = [];
                    multifeed.querySpec.forEach(function(specItem) {
                       queryFunctions.push(
                             function(done) {
                                var sqlWhere = {
                                   where : "((isPublic=1) AND (" + specItem.feeds.where + "))",
                                   values : specItem.feeds.values
                                };

                                FeedModel.findBySqlWhere(sqlWhere,
                                                         {
                                                            fields : ['id', 'userId'],
                                                            orderBy : ['id']
                                                         },
                                                         false,   // false here to ensure the SQL doesn't include a LIMIT clause
                                                         function(err, feeds) {
                                                            if (err) {
                                                               log.error(JSON.stringify(err, null, 3));
                                                               errors.push(err);
                                                            }
                                                            else {
                                                               if (feeds && feeds.length > 0) {
                                                                  feedsAndChannels.push({
                                                                                           feeds : feeds,
                                                                                           channels : specItem.channels
                                                                                        });
                                                               }
                                                            }
                                                            done();
                                                         });
                             });
                    });

                    flow.parallel(queryFunctions,
                                  function() {
                                     if (errors.length > 0) {
                                        // just report the first error
                                        var err = errors[0];

                                        // See if the error contains a JSend data object.  If so, pass it on through.
                                        if (typeof err.data !== 'undefined' &&
                                            typeof err.data.code !== 'undefined' &&
                                            typeof err.data.status !== 'undefined') {
                                           return res.jsendPassThrough(err.data);
                                        }
                                        return res.jsendServerError("Failed to get feeds", null);
                                     }
                                     else {
                                        FeedModel.getTiles(feedsAndChannels, level, offset, function(err, eventEmitter) {
                                           if (err) {
                                              if (err.data && err.data.code == httpStatus.UNPROCESSABLE_ENTITY) {
                                                 return res.jsendPassThrough(err.data)
                                              }

                                              log.error("Failed to get tiles for multifeed: " + JSON.stringify(err, null, 3));
                                              return res.jsendServerError("Failed to get tiles for multifeed", null);
                                           }

                                           // set the status code and set the connection to close
                                           res
                                                 .status(httpStatus.OK)
                                                 .set("Content-Type", "application/json")
                                                 .set("Connection", "close");

                                           // I don't really understand why, but we must have a function (even an empty
                                           // one!) listening on stderr, or else sometimes I get no data on stdout.
                                           eventEmitter.stderr.on('data', function(data) {
                                              // log.error(data);
                                           });

                                           eventEmitter.on('error', function(e) {
                                              log.error("Error event from EventEmitter while getting tiles: " + JSON.stringify(e, null, 3));
                                           });

                                           // pipe the eventEmitter to the response
                                           return eventEmitter.stdout.pipe(res);
                                        });
                                     }
                                  });
                 });