How to optimize process in php or run it in the background?

0

I have a file in php that is responsible for closing the session but before it must change the status in several different databases, this process takes a bit, so the browser stays waiting and until it ends redirects to index .php This is the code to close the session ...

      <?php
      include 'HistorialController.php';
      include_once 'clases/Sistemas.php';
      session_start();

      $_SESSION['login'] = false;
      $_SESSION['recordar'] = 'no';         

      $liberar = new Sistemas('');
      $liberar->libera();

      unset($_SESSION['login']);
      header('Location: index.php');    
      ?>

The release code is the following ...

       /**
       * Metodo que Libera las imagenes ocupadas por el usuario al salir de la session
       **/
       public function libera(){
           $id_user = $_SESSION['id_usuario'];
           $actualiza = "UPDATE Imagen SET estatuspm_web=0 WHERE id_usuariopm_web=" . $id_user." and estatuspm_web=1";
           for ($i = 0; $i < count($this->listaSistemas); $i++){ //RECORRIDO DE LOS SISTEMAS 
                $sistemaActual = $this->listaSistemas[$i];                 
                $baseConn = new BaseConexion("Sistema_".$sistemaActual); 
                if ($conn = $baseConn->conectar()) {
                     sqlsrv_query( $conn, $actualiza, $params);                      
                }
                $baseConn->close();
            }
       }
    
asked by Alberto Rojas 14.03.2017 в 17:26
source

1 answer

0

When logging, save in a table cierres_de_sesion the id_usuario and have a cronjob that takes one by one the rows of this table and run asynchrony the logic of release. As I see it, the list of systems is user-dependent, so it would work pretty well.

Pros: You remove this work to close sessions of the logout process and leave it as an external task, so you can redirect to index.php immediately. Cons: If you have MANY customers closing session at the same time, you would have to do some logic of parallelization (and therefore synchronization) so that these jobs run efficiently and do not accumulate a lot of work. You could use a work queue or something, but possibly complicate the system a lot.

    
answered by 14.03.2017 в 18:25