데이터베이스에서 스레드 (Gmail 대화) ID를 가져온 다음 각 스레드 ID에 대한 모든 데이터를 Google API에 요청하는 기능이 있습니다. 스레드 객체를 받으면 데이터베이스에 저장합니다. 이 메시지는 ~ 1k 개의 메시지가있는받은 편지함에서는 정상적으로 작동합니다. 그러나 100,000 개가 넘는 메시지가있는 계정에서 작동하는지 확신 할 수 없습니다.막대한 양의 콜백이 스크립트를 중단 시키거나 충분한 RAM을 사용할 수있을 때마다 계속할 것인가?
이제 컴퓨터에서 메모리가 부족 해지면 충분한 RAM을 다시 사용할 수있을 때마다 콜백 함수가 계속 실행됩니까? 나는 (일부 지점에서 다시 실행 전체 스크립트를하고 마지막으로 끝난 곳에서 신선한 RAM을 계속?)이 부분 별 역할을 다하기 위해이 코드를 수정해야
function eachThread(auth) {
var gmail = google.gmail('v1');
MongoClient.connect(mongoUrl, function(err, db){
assert.equal(null, err);
var collection = db.collection('threads');
// Find all data in collection and convert it to array
collection.find().toArray(function(err, docs){
assert.equal(null, err);
var threadContents = [];
// For each doc in array...
for (var i = 0; i < docs.length; i++) {
gmail
.users
.threads
.get({auth:auth,'userId':'me', 'id':docs[i].id}, function(err, resp){
assert.equal(null, err);
threadContents.push(resp);
console.log(threadContents.length);
console.log(threadContents[threadContents.length - 1].id);
var anotherCollection = db.collection('threadContents');
anotherCollection.updateOne(
{id: threadContents[threadContents.length - 1].id},
threadContents[threadContents.length - 1],
{upsert:true},
function(err, result){
assert.equal(null, err);
console.log('updated one.');
});
if (threadContents.length === docs.length) {
console.log('Length matches!');
db.close();
}
});//end(callback(threads.get))
}//end(for(docs.length))
});//end(find.toArray)
});//end(callback(mongo.connect))
}//end(func(eachThread))
RAM 때문인지는 모르겠지만 SQL to MongoDB 도구에서 부분적으로 구현하는 것을 기억합니다.파트별로 파트를 테스트하기 전에 파트 별 파트의 버전이 * 2 빠릅니다. – DrakaSAN
@DrakaSAN 나는 똑같이했다. 나는 수백만 개의 행을 가진 mySQL 데이터베이스를 가지고 있었고 SQL에서 mongo로 부분적으로 마이그레이션 한 CRON을 만들었습니다. 그러나 그것은 PHP였습니다. 이 콜백 세계는 시간이 오래 걸리더라도 일회성 실행으로 더 잘 작동 할 수 있다고 생각합니다. 작업을 수행하는 데 필요한 것입니다. – Kunok
당신이 할 수있는 일은'threadContents'를 피하고'resp' 자체를 삽입하는 것입니다. 또한 루프 내에서'anotherCollection'을 생성하는 것은 반복되는 동일한 객체이므로 아무런 의미가 없습니다. 그럼 당신은 확실히 숫양에 어떤 문제가 없을 것입니다. – sed