0

Good afternoon,

I have the following functions that shows and hides a page busy loader:

busyStatusDelay = 1000; //milliseconds
var timer = null;
var show = false;

function busyShow(nodelay,delay) {
    timer = setTimeout('busyDelayShow()',busyStatusDelay);
}

function busyDelayShow() {
    if ($.active > 0) {
        $('#json-overlay').css('display', 'table');
        show = true;
    }
}

function busyHide() {
    clearTimeout(timer);
    timer = null;
    show = false;
    $('#json-overlay').css('display', 'none');
}

This works great for normal ajax queries, we however have a new function that should send emails with SMTP, but this SMTP connection takes a few seconds too long to connect and send the emails.

To avoid this I want the ajax function to not trigger this loader, the function does not call the function to open the loader, but the issue is that when another function gets called that should open the loader, it picks up that there is an active connection so the loader comes back up and does not go away.

I tried to use the following so that the JS does not see it as an active request:

xhr.abort(); busyHide();

but when the ajax gets aborted the php aborts with it.

Note that the functions to send the emails should run in the background and should not wait for a response.

I have been searching for a way to disconnect from the server request without affecting the server's functions running and send the email.

I saw ignore_user_abort in another post so will be doing some reading up on this to see if this will work without affecting the rest of the system.

Thanks in advance for any tips on how to continue!

CopperRabbit
  • 646
  • 3
  • 7
  • 24

1 Answers1

0

A good approach to this would be to execute your SMTP as a background process using some sort of queuing mechanism. So basically, whenever a JS triggers AJAX to mail, the PHP push the email request to a queue and send a response back to the XHR immediately. This way, your AJAX execution won't be blocked for long. If you are using some sort of PHP framework like Laravel, it makes easier to manage queues otherwise have a look at this post.

drunkZombie
  • 112
  • 2
  • 15
  • Thank for the answer, will read up on how to create a queue. I am using CodeIgniter 2. I also thought of calling the function to send the email in a shell or curl function, but have zero experience, so will need to check on how to post to a url with one of the two. – CopperRabbit Jan 17 '19 at 13:50
  • if your solution is running on a linux machine, RHEL or Ubuntu, it already comes with a mail queue system. All you have to do it set it up. The main pupose here is to avoid blocking of main thread and running queues is the best way to achieve the same and is always recommended. – drunkZombie Jan 17 '19 at 13:53
  • We are running CentOS, so is still a Linux machine, however I dont have access to the machine's config, but will ask someone who does who will be able to assist. – CopperRabbit Jan 18 '19 at 05:54
  • In CentOS, you setup something like Postfix for mailqueue solution. Since, you mentioned you don't have direct control of your machine, I would recommend setting up some PHP queuing system and using that. But either way you can achieve what you are looking for. – drunkZombie Jan 18 '19 at 08:01