Ajax error function is executing with error message “Gateway time-out” when multiple users go through registration process

…衆ロ難τιáo~ 提交于 2019-12-12 06:51:40

问题


I have built an app with a registration process for a magazine company. The app is especially for their subscribers who are paying a monthly subscription through stripe.

The registration process works as follows:

  1. User enters name, email, password (twice) within the application
  2. Ajax is performed with POST and the user details are sent to php page called registration_processing.php on a remote server.

  3. I firstly check if the email address exists already in the app database

  4. If the email doesn't exist then I do an API call to the magazine company's stripe account to get a list of their subscribers email addresses. (Three are about 700 so I use pagination.)

  5. I check the email sent with the ajax post request against the list of subscriber emails received from stripe.

  6. If there is a match then I store the users details into the app database and bring the user to the home page of the app.

This worked perfect when I tested it before launching the app.

However, when the app was launched, multiple users started to go through this registration process and now the following is happening:

Their subscriber email addresses are successfully matching and their details ARE being stored in the app database however the ajax call is executing the error function with error message "Gateway time-out". On testing it takes around 32 seconds before the time-out happens. All other ajax requests to this remote server are working fine. I looked in phpinfo.php and i noticed "max_execution_time" is 120. I'm not sure if that has to do with anything. Do I need to increase this? Thanks for any help.

AJAX code

    //this registration email will need to be checked on the server side with the list of subscribers from stripe
        $.ajax({
        url: app_root_url + 'registration_processing.php',
            data: JSON.stringify(params), 
        type: "POST",
        dataType: "json",
        contentType: "application/json;charset=utf-8",

            success: function(data){

            var result = data;

            var exception_occurred = result.exception_occurred; 
            if(exception_occurred){
                var emailAlreadyExists = result.email_already_exists; 
                if(emailAlreadyExists){
                    displayError('registration_feedback_message', 'This email already exists in the system. If you already have an account please <a class="inline_link normal_link" href="#page-login">login here!</a>');

                }else{
                    var exception_message = 'Error: ' + result.exception_message;
                    displayError('registration_feedback_message', exception_message);
                }
            }else{
                var emailAlreadyExists = result.email_already_exists; 
                if(emailAlreadyExists){
                    //email already exists in our database and therefore the user is already registered so should use the login form
                    //email already exists. if you already have an account then login here.

                        displayError('registration_feedback_message', 'This email already exists in the system. If you already have an account please <a class="inline_link normal_link" href="#page-login">login here!</a>');

                }else{
                    //email does not already exist in our database
                    var userIsSubscriber = result.user_is_subscriber; 

                    if(!userIsSubscriber){
                        //this email address does not exist in the subscribers database

                    }else{
                        //successful registration. 
                        //user is subscriber and therefore has now been registered 

                    }
                }
            }

        }//end success
        ,
            error: function(xhr, status, error) {
            displayError('registration_feedback_message', 'Error message: ' + error);


        }
    });//end ajax

PHP

registration_processing.php (this is the main part of it-after values are validated)

//check email address exists
            $result = checkEmailExists($email, $pdoConnection);
            if($result['email_exists'] == true){
                $data['exception_occurred'] = false;
                $data['email_already_exists'] = true;   

                echo json_encode($data);


            }else{

                //email doesnt exist yet

                //check if the registration email entered, exists on the subscriber's database. currently their subscribers exist in stripe
                $subscribersEmailExists = checkIfUserIsSubscriber($email, $GLOBALS['stripe']); 

                if($subscribersEmailExists == 1 || $subscribersEmailExists == true){
                    //if this returns true therefore this user is indeed a subscriber so now register their details on our
                    //app database including password.

                    $userPrivilegeID = 1;
                    $userHasPassword = 1;

                    $profileImage = "images/profile_images/blank-avatar.png"; //this should come from the html

                    $results = registerUser($password, $email, $isAvatarImage, $profileImage, $userPrivilegeID, $display_name, $userHasPassword, $registration_mob_select, $pdoConnection);

                    if($results['exception_occurred'] == true){
                        $data['exception_occurred'] = true;
                        $data['exception_message'] = $results['exception_message']; 
                        if(strpos($data['exception_message'], 'Duplicate') !== false){

                            //check if the exception is to do with email already existing.                  
                            //SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry for key 'emailAddress'
                            $data['email_already_exists'] = true;   

                        }else{

                            $data['email_already_exists'] = false;
                        }
                        echo json_encode($data);

                    }else{
                        $data['exception_occurred'] = false;
                        if($results['email_already_exists'] == true){
                            //email already exists. user is already registered and therefore has a password
                            //need to show error to user to say they are already registered and should use the login form.

                            $data['email_already_exists'] = true;
                            echo json_encode($data);

                        }else{
                            $data['email_already_exists'] = false;


                            echo json_encode($data);
                        }
                    }

                }else{
                    $data['email_already_exists'] = false; //email does not already exist in our database
                    //this email address does not exist in the subscribers database
                    //therefore this user is not a paying subscriber so needs to be redirected to the website in order to subscribe
                    //$subscribersEmailExists will be false here
                    $data['user_is_subscriber'] = false;

                    echo json_encode($data);
                }
            }

Stripe php functions

function getListOfCustomers($stripe){
    $customers_emails = array();
    \Stripe\Stripe::setApiKey($stripe['secret_key']);

    $customers = \Stripe\Customer::all(array("limit" => 100)); 

    foreach($customers->autoPagingIterator() as $customer){ 
        array_push($customers_emails, $customer['email']); 

    }
    return $customers_emails; 

}
function checkIfUserIsSubscriber($user_email_address, $stripe){
    //initialize a boolean to false. it will be changed to true if customer exists otherwise will remain false.
    $userIsSubscriber = false;
    $array_of_customers = getListOfCustomers($stripe);

    for($x=0; $x < count($array_of_customers); $x++){
        //the users email matches one in the array of customers then assign the boolean to true.
        if($user_email_address == $array_of_customers[$x]){
            $userIsSubscriber = true;

        }
    }
    return $userIsSubscriber; //this value will be true or false depending on whether customers email exists in companies stripe account.

}

来源:https://stackoverflow.com/questions/42899053/ajax-error-function-is-executing-with-error-message-gateway-time-out-when-mult

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!