I need every day update ~ 10.000 items in my MySql. I have upload a CSV file and try to update my database data. I am forking with two field. My problem is that works but in some time I get 504 Gateway Time-out and my upload is not able to finish the process.
here is my code
if(is_uploaded_file($_FILES["filename"]["tmp_name"])){
//
move_uploaded_file($_FILES["filename"]["tmp_name"], "".$_SERVER["DOCUMENT_ROOT"]."/upload/".$_FILES["filename"]["name"]);
$file_path="".$_SERVER["DOCUMENT_ROOT"]."/upload/".$_FILES["filename"]["name"]."";
$file=file_get_contents("".$file_path."");
$file=iconv("windows-1251", "utf-8",$file);
file_put_contents("".$file_path."",$file);
if(!setlocale(LC_ALL, 'ru_RU.utf8')) setlocale(LC_ALL, 'en_US.utf8'); if(setlocale(LC_ALL, 0) == 'C') die(' (ru_RU.utf8, en_US.utf8)');
if (($handle_f = fopen($file_path, "r")) !== FALSE) {
// csv
while (($data_f = fgetcsv($handle_f,99999,";"))!== FALSE) {
// ean13
$sql="SELECT id_product FROM ps_product WHERE reference = '".$data_f[0]."'";
$id_product = Db::getInstance()->getValue($sql,0);
// ,
if ($id_product) {
$sql=mysql_query("UPDATE `ps_product` SET `quantity` ='".$data_f[1]."' WHERE `reference`='".$data_f[0]."'");
echo "<p style='color:green'>Items<b>".$data_f[0]."</b> updated</p>";
} else{
echo "<p style='color:red '>Items<b>".$data_f[0]."</b> not found</p>";
}
}
echo "<b>Update is complited</b>";
}else{
echo "Can`t open imported file";
}
}else{
echo '
<h2>Quantity update:</h2>
<form action="'.$_SERVER["PHP_SELF"].'" method="post" enctype="multipart/form-data">
<input type="file" name="filename"><br>
<input type="submit" value="Load"><br>
</form>
';
}
Now I split in excel for many files with ~ 1000 per fail and update database. It takes some time. Can you give me an idea, or maybe I have some error in my code? Update using ajax as I think will not help.
504 Bad Gateway Timeoutis when your gateway/proxy server did not get atimely responsefrom your processing server. In such cases, even if the processing server does finish all the updates, since it did not send any messages to your proxy to let it know that processing was still going on, the proxy closes the connection.Using ajax for updates is not a very good idea in this case. However there is an
ajax design patterncalled theheartbeat patternwhich can help you out. The basic idea is this:This
continuous message request and responseis called aheartbeat. It helps in keeping theconnection alivewhile your server processing takes place. Once the server processing is over and you get a confirmation, you can kill the timer and close the connection as needed.Of course on the processing server side, you can do additional things like
increase timeout lenght,add database indexesetc to make sure that aphp process timeoutdoes not take place.Note however that the process timeout is different from a Gateway timeout.