Request with ajax [closed]

1

On my system I have an e-mail module I send it through ajax. sending to approximately 300 people on the same request.

The uolhost server supports sending 150 / hour so when I send 150 I give the php sleep command (1hour) and the ajax keeps waiting.

I would like to know if this overloads the server if yes, what to do for the better?

Unfortunately, the company has contracted a plan on uolhost that does not support cron or ssh connection.

    
asked by anonymous 13.10.2016 / 15:15

4 answers

0

You're running your server on the edge, so it consumes a lot of memory. Try to make the sending script not last long and do not use sleep. Make a queue of emails to be sent, get your script, take the sleep and put the routine in a cron job that runs every hour, in the Job cron it will consume less memory than a routine with infinite loop. / p>

Recommendation, use Mandrill for transactional emails, it's cheap and you will not have this kind of problem.

    
13.10.2016 / 15:23
0

I would not do email shots using an ajax request, simply.

Why do not you create a cron job that runs every 1 hour and picks up all the emails that have not yet been sent?

Search for: php cron job

    
13.10.2016 / 15:24
0
The problem with your approach is that the web server (eg Apache) or gateway (eg php-fpm) will kill this process due to timeout, which is by default set to less than 5 minutes. You could increase this limit, but this is not recommended because some bug in the code could crash your server and leave many processes running indefinitely.

You need to implement an asynchronous task queue, possible through Celery-PHP

The ideal would be something like Celery but since you do not have access to the server, increasing the timeout is the only way out. I recommend against leaving the timeout too high and studying alternatives in hosting. Depending on the usage, you can do this for free by Heroku for example.

    
13.10.2016 / 15:27
0

Below is an idea of how to automatically send the batches, just implement the "pagination" in the emails trigger ...

var enviados = 0, timer;
var emails = 10; // emails por lote
var minutos = 5; // tempo de espera entre lotes

$('#enviar').click(function() {
  $(this).prop("disabled", true);
  
  // ajax que envia o lote de emails
  $.get('enviar.php?enviados=' + enviados + '&emails=' + emails);
  // apenas para teste
  console.log('enviar.php?enviados=' + enviados + '&emails=' + emails);

  // incrementa o contador de envios, isso pode ser colocado no callback success do .get
  enviados += emails;

  // ativa o timer que vai continuar o envio depois do tempo determinado
  timer = setTimeout(function() {
    $('#enviar').click();
  }, (60000 * minutos));
});

$('#parar').click(function() {
  $('#enviar').prop("disabled", false);
  clearInterval(timer);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><buttonid="enviar">Enviar</button>
<button id="parar">Parar</button>
    
14.10.2016 / 18:33