• Hi everyone!

    I created a costum page that, given the link of a csv file uploaded to the media, updates me a meta data of the users. I used jquery (ajax) and php.
    I thought of inserting the ajax post call within the loop of the csv lines to progressively update the metadata per user. I do not think it is the correct way to proceed, however, because the console gives me error 502 and 429. What am I doing wrong?
    Previously I had managed the loading of the csv and the update of the users only through php but with a large number of users it becomes a very heavy operation and gives me a 502 error. I was looking for a less cumbersome solution

    Jquery

    Query(document).ready(function( $ ){
    
     $( "#test-click" ).click(function() {
      
        $.ajax({
            type: "GET",
            url: 'FILE-CSV.csv',
            dataType: "text",
            success: function(data) {processData(data);}
         });
    
     
    function processData(allText) {
        var allTextLines = allText.split(/\r\n|\n/);
        var headers = allTextLines[0].split(';');
        //var lines = [];
    
        for (var i=1; i<allTextLines.length; i++) {
            var data = allTextLines[i].split(';');
            if (data.length == headers.length) {
    
                var tarr = [];
                for (var j=0; j<headers.length; j++) {
                    //tarr.push(headers[j]+":"+data[j]); 
                  tarr[headers[j]] = data[j];
                  //console.log(tarr['n']);
          $.ajax({
                url:"/wp-json/bluenext/updatelesson",
                method: "POST",
                //data: post_id,
                data: {id : tarr['id'], id_lezione : tarr['id_lezione']},
                success: function(response){
                
               console.log(response);
    
                    }
                }) 
                }
                //lines.push(tarr);
            }
        }
         //console.log(lines);
    }
      
      });
    });

    PHP

    function updatelesson(){
      $user_id = json_decode($_POST['id']);
      $idlesson = json_decode($_POST['id_lezione']);
      llms_mark_complete( $user_id, $idlesson, 'lesson');
      return $user_id;
    }
Viewing 1 replies (of 1 total)
  • The processing of requests is time limited by web server services. For larger import files I would rather suggest another way that does not lead to a timeout, but at least to a safe import:

    Just save the CSV file at the server. Do not process it directly after the upload. Then add them to a list for processing. Then create a WP Cron event that checks every X minutes if a file is in the processing list. And from here there are 2 options:

    a) Since WP Cron normally runs request-based only, this automatic import could also lead to a timeout. So you would have to split the import file and import e.g. only 50 records at a time. For this you need another processing list in which you store the records that are still to be processed.
    b) You control the import via system-cron. This runs completely independent of requests via web, therefore there is no timeout. WP CLI would certainly also be an interesting approach.

Viewing 1 replies (of 1 total)
  • The topic ‘Usermeta update with AJAX’ is closed to new replies.