Mysql crash after foreach create
See original GitHub issueHi, i try to create posts inside foreach, but i think something wrong, bcs my mysql crash after that, can you tell me, how i can fix this? Maybe something like delay?
result.forEach(function(item){
wp.song().search( item['artist']+' - '+item['song'] ).then(function( posts ) {
if(!posts[0]){
var myRequests = [];
myRequests.push(rp({uri: "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist="+encodeURIComponent(item['artist'])+"&api_key=[key]&format=json", json: true}));
myRequests.push(rp({uri: "https://www.googleapis.com/youtube/v3/search?part=snippet&order=viewCount&q="+encodeURIComponent(item['artist']+'+'+item['song'])+"&type=video&key=[key]", json: true}));
Promise.all(myRequests)
.then((arrayOfHtml) => {
console.log(arrayOfHtml[0]['artist']['image'][2]['#text']);
console.log(arrayOfHtml[1]['items'][0]['id'].videoId);
wp.posts().create({
title: item['artist']+' - '+item['song'],
content: 'Your post content',
status: 'publish'
}).then(function( response ) {
console.log( response.id );
})
})
.catch(/* handle error */);
}
});
})
Issue Analytics
- State:
- Created 7 years ago
- Comments:12
Top Results From Across the Web
PHP: how to foreach large data from mysql or array without ...
There has a large data that is from mysql or a array, such as over 40000. I need loop them and then do...
Read more >B.3.3.3 What to Do If MySQL Keeps Crashing
Many unexpected server exits are caused by corrupted data files or index files. MySQL updates the files on disk with the write() system...
Read more >10 Most Common Mistakes That PHP Developers Make - Toptal
Common Mistake #1: Leaving dangling array references after foreach loops. Not sure how to use foreach loops in PHP? Using references in foreach...
Read more >How to fix InnoDB corruption cases for the MySQL databases ...
[ERROR] InnoDB: Cannot create log files because data files are corrupt or the database was not shut down cleanly after creating the data ......
Read more >{foreach},{foreachelse} - Smarty Template Engine
{foreachelse} is executed when there are no values in the array variable. ... $smarty = new Smarty; $dsn = 'mysql:host=localhost;dbname=test'; ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@nezzard Apologies for the delayed response, I have been away from Github for the holidays.
The most likely issue you’re running into is that too many requests are being made at once—if
result
is large, the forEach executes in a few milliseconds, so all the requests are dispatched at once and that overwhelms MySQL. The best way around this is to dispatch the requests in sequence, instead of in parallel, by chaining each new request onto the promise from the prior request: this is easily accomplished using reduce, and we can write a small helper to assist with batching.Note: I’ve removed your API keys from your post, and they are represented below by the
keys
object. I recommend not sharing your own API keys in github posts, just so that other developers aren’t able to use your account in their own applications.The reducer function at the end could easily be adapted to batch the requests, making only 5 or so at once, which WP should be able to handle; that’ll get you through a large set faster than going one by one. You can use _.chunk or a similar method of your own to split a large array into many arrays of five items:
Let me know if the above works, hope this is helpful!
@nezzard I wanted to check in to see if you were still having this issue – if so, please share the code you are using to import or require
_
. I’m going to close this out again because it seems the issue is around importing lodash, not about the functionality of this library, but if you’re still having trouble I’ll do my best to help.