Skip to content

Commit 277d1ac

Browse files
limit scrupt
1 parent 42e1a10 commit 277d1ac

3 files changed

Lines changed: 29 additions & 14 deletions

File tree

lib/models/mongo/schemas/context-version.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,18 @@ ContextVersionSchema.index({
186186
'appCodeVersions.lowerRepo':1,
187187
'appCodeVersions.commit':1
188188
});
189+
ContextVersionSchema.index({
190+
'build.completed': 1,
191+
'build.hash': 1,
192+
'build._id': 1,
193+
'build.started': 1
194+
});
195+
ContextVersionSchema.index({
196+
'build.completed': 1,
197+
'build.hash': 1,
198+
'build._id': 1,
199+
'build.started': -1
200+
});
189201

190202
ContextVersionSchema.set('toJSON', { virtuals: true });
191203
// ContextVersionSchema.post('init', function (doc) {

lib/models/mongo/schemas/infra-code-version.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ var path = require('path');
1111
var FileSchema = new Schema({
1212
Key: {
1313
type: String,
14-
required: true
14+
required: true,
15+
index: true
1516
},
1617
ETag: {
1718
type: String,

scripts/add-hash-to-files.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@ var debug = require('debug')('script');
66
var mongoose = require('mongoose');
77
mongoose.connect(process.env.MONGO);
88
var async = require('async');
9-
var createCount = require('callback-count');
109

1110
async.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

2321
function getAllInfra (cb) {
@@ -45,22 +43,27 @@ function hashString(data, cb) {
4543

4644
function 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

Comments
 (0)