Request with ajax

Asked

Viewed 424 times

1

In my system I have an email sending module I send it through ajax. shipping to approximately 300 people on the same request.

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

I wonder if this overloads the server if yes, what to do for better?

Unfortunately the company has hired a plan in uolhost that has no cron support and no ssh connection.

4 answers

0

You’re running your server at the limit, so it consumes a lot of memory. Try to make the upload script not last long and don’t use Sleep. Queue up emails to be sent, take your script, take out the Sleep and put the routine in a cron Job that runs hourly, in cron Job it will consume less memory than an endless loop routine.

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

  • Unfortunately the company has hired a plan in uolhost that has no cron support and no ssh connection.

  • Then use Mandrill( gringo and cheap) or All In mail(Brazilian and expensive), sending emails weigh on the server. I say this because I worked a lot with transactional emails.

0

I wouldn’t shoot emails using an ajax request, simply.

Because you don’t create a service (cron job) that runs every 1 hour and picks up all the emails that you haven’t gone through the sending process?

Search for: php cron job

  • Unfortunately the company has hired a plan in uolhost that has no cron support and no ssh connection.

0

The problem with your approach is that the web server (e.g. Apache) or gateway (e.g. php-fpm) will kill this process due to timeout (timeout), which is by default set in less than 5 minutes. You could increase this limit, but this is not recommended, as 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 the Celery-PHP

The ideal would be something like Celery but as you do not have access to the server, increase the timeout is the only output. I recommend against leaving the timeout too high and studying alternatives in lodging. Depending on the use, you can do it for free by Heroku for example.

  • 1

    the timeout problem I’ve already solved

  • Decided to disable it? Timeouts are essential on a web server, you may have bigger problems http://stackoverflow.com/questions/4306605/is-ini-setmax-execution-time-0-a-bad-idea

0

Below is an idea of how to do automatic batch sending, just implement the "paging" in the email 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>

<button id="enviar">Enviar</button>
<button id="parar">Parar</button>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.