-
-
Save anonymous/84740ccb1db39ab03c883de4c7028cce to your computer and use it in GitHub Desktop.
I can has memleak?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const xmlStream = require('xml-stream') | |
const _ = require('lodash') | |
const config = require('config') | |
var fs = require('fs-extra') | |
var { | |
db, | |
mongoose | |
} = require('../common/db') | |
const _path = require('path') | |
const logger = require('../common/logger') | |
// var kue = require('kue') | |
let badKey = /\$/ | |
let sanitizeKeys = function (object) { | |
return _.reduce(object, (object, value, key) => { | |
if (badKey.test(key)) { | |
key = key.replace('$', '_') | |
} | |
if (_.isObject(value)) { | |
value = sanitizeKeys(value) | |
} | |
object[key] = value | |
return object | |
}, {}) | |
} | |
let counter = 0 | |
var xml = null | |
let savedCallBack = function (err) { | |
if (err) { | |
logger.error(err.toString(), err) | |
} | |
} | |
fs.readdir(config.discogsDumpsPath, (err, discogsDumps) => { | |
if (err) { | |
return logger.error(err.toString(), err) | |
} | |
_.each(discogsDumps, (discogsDump) => { | |
if (discogsDump.indexOf('labels') == -1) { | |
return true | |
} | |
let dumpStream = fs.createReadStream(`${config.discogsDumpsPath}${discogsDump}`) | |
xml = new xmlStream(dumpStream) | |
let dumpType = _.last(discogsDump.split('_')).replace(/s\..*$/, '') | |
let skeleton = null | |
let model = null | |
xml.once(`updateElement: ${dumpType}s>${dumpType}`, (item) => { | |
skeleton = _.reduce(item, (skeleton, value, key) => { | |
skeleton[key] = { | |
type: global[_.capitalize(typeof value)] | |
} | |
if (key == 'id') { | |
skeleton[key].type = Number | |
skeleton[key].index = true | |
} | |
return skeleton | |
}, {}) | |
let schema = mongoose.Schema(skeleton) | |
model = mongoose.model(`discogs${_.capitalize(dumpType)}`, schema) | |
}) | |
xml.on(`updateElement: ${dumpType}s>${dumpType}`, (item) => { | |
item = sanitizeKeys(item) | |
console.log(item) | |
// let whatever = model(item).save(savedCallBack) // <-- memory leak here | |
return true | |
}) | |
return true | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment