Thursday, 10 October 2019

Running out of memory with nested promises?

Howdy! I've got a question that's been bogging me down for awhile and I'd like some input from yall...To get an idea of why I'm nesting promises, here's a small function of something similar that I'm trying to accomplish. All while this runs, my memory increases at pretty quick rate until the program crashes. I'm looking for suggestions on how I can avoid this or how I can tell the GC it's time to take out the trash. Any help/advice would be greatly appreciated!``` /** * Let's say we have a baseball game we have to parse stats for, transform the data, * and then insert all of this data into a multiple tables... * * Steps: * * 1. Fetch the scraped data and return an array of 1500-ish boxscores * 2. Loop through and divide data into sections to be transformed (sections based on tables) * 3. For each "section", push transformed data to DB and return a promise * 4. Watch memory increase and blow up. */function start() { // Make a request to some URL to get an array of boxscores const boxscores = await getAllBoxscores();// Loop through array of boxscore objects to transform the data the way we want it // and then insert/update the correct table. Using async.eachLimit to prevent bombarding external // website with scraping calls async.eachLimit( boxscores, 5, (boxscore, callback) => { // First thing we do is set an object of general data that'll be used for most other functions const identifyingData = setIdentifyingDataForBoxscore(boxscore); // Second thing we do is split this boxscore up into sections based on the corresponding // database table. const boxscoreDetails = boxscore.details; // game_id, location, weather, ballpark, etc... const boxscoreRosters = boxscore.rosters; // [homeroster, awayroster] game_id, player_name, player_number, hits, walks, etc... const boxscoreStars = boxscore.stars; // game_id, star_number, name, etc... const boxscoreInningStats = boxscore.innings; // game_id, inning_num, top_bottom, hits, runs, pitches, walks, etc... const boxscorePitches = boxscore.pitches; // game_id, at_bat_num, hitter, pitcher, pitch_type, ball_strike_hit, count, etc... const boxscorePitchers = boxscore.pitchers; // game_id, name, number, inning_start, inning_end, result, pitch_count, balls, strikes, etc... // Transform each one of these "sections" and insert into the db await parseAndInsertBoxscoreDetails(identifyingData, boxscoreDetails); // table: boxscore_details await parseAndInsertBoxscoreRosters(identifyingData, boxscoreRosters); // table: boxscore_player_stats (each player gets an entry for the game) await parseAndInsertBoxscoreStars(identifyingData, boxscoreStars); // table: boxscore_stars (each star player gets an entry for the game) await parseAndInsertBoxscoreInningStats(identifyingData, boxscoreInningStats); // boxscore_innings (usually 9 entries here) await parseAndInsertBoxscorePitches(identifyingData, boxscorePitches); // boxscore_plate_appearances (each pitch is it's own row here = lots of inserts) await parseAndInsertBoxscorePitchers(identifyingData, boxscorePitchers); // boxscore_pitchers (summary for each pitcher) callback(); } ) } ```

Submitted October 10, 2019 at 02:46PM by strictly_rin

No comments:

Post a Comment