@@ -6,18 +6,16 @@ var debug = require('debug')('script');
66var mongoose = require ( 'mongoose' ) ;
77mongoose . connect ( process . env . MONGO ) ;
88var async = require ( 'async' ) ;
9- var createCount = require ( 'callback-count' ) ;
109
1110async . waterfall ( [
1211 getAllInfra ,
1312 eachInfra
1413 ] , function ( err ) {
1514 if ( err ) {
16- console . log ( 'ERROR' , err . stack ) ;
17- process . exit ( 1 ) ;
15+ return console . log ( 'ERROR' , err . stack ) ;
1816 }
1917 console . log ( 'done everything went well' ) ;
20- process . exit ( 0 ) ;
18+ mongoose . disconnect ( ) ;
2119} ) ;
2220
2321function getAllInfra ( cb ) {
@@ -45,22 +43,27 @@ function hashString(data, cb) {
4543
4644function eachInfra ( infras , cb ) {
4745 debug ( 'eachInfra' ) ;
48- var count = createCount ( 1 , cb ) ;
46+ if ( ! infras || infras . length === 0 ) {
47+ return cb ( ) ;
48+ }
4949 // get all infracodes
50- infras . forEach ( function ( infra ) {
50+ async . eachLimit ( infras , 1000 , function ( infra , cb ) {
5151 debug ( 'eachInfra:infra' , infra . _id ) ;
5252 // for each file
53- infra . files . forEach ( function ( file ) {
54- if ( file . isDir || file . hash ) { return ; }
55- count . inc ( ) ;
53+
54+ async . each ( infra . files , function ( file , cb ) {
55+ if ( file . isDir || file . hash ) { return cb ( ) ; }
56+
5657 debug ( 'eachInfra:infra:file' , infra . _id , file . _id ) ;
5758 var filePath = file . Key . substr ( file . Key . indexOf ( '/source' ) + 7 ) ;
5859 // get contance of file
5960 infra . bucket ( ) . getFile ( filePath , file . VersionId , file . ETag , function ( err , data ) {
6061 if ( err ) { return cb ( err ) ; }
62+
6163 // create hash of file
6264 hashString ( data . Body . toString ( ) , function ( err , hash ) {
6365 if ( err ) { return cb ( err ) ; }
66+
6467 file . hash = hash ;
6568 debug ( 'eachInfra:infra:file:hash' , infra . _id , file . _id , file . hash ) ;
6669 // update mongo of file with hash
@@ -71,10 +74,9 @@ function eachInfra (infras, cb) {
7174 $set : {
7275 'files.$' : file
7376 }
74- } , count . next ) ;
77+ } , cb ) ;
7578 } ) ;
7679 } ) ;
77- } ) ;
78- } ) ;
79- count . next ( ) ;
80+ } , cb ) ;
81+ } , cb ) ;
8082}
0 commit comments