How to go about this?: I have written a project that allows for scheduling of mailers. The mailing endpoint is meant to automatically send emails to subscribed users once the scheduled time reaches, and -but- after that, it reschedule for another time as follows:
This is my endpoint.php
<?php
//My script are as follows
//This query fetches only records that are due for the next mail schedule
$scdl=mysqli_query($config, sprintf("SELECT * FROM mail_schedules WHERE next_schedule < '" . (strtotime('now UTC') - 1) . "'")) or die(mysqli_error());
if(mysqli_num_rows($scdl) > 0) :
{
$schedule = mysqli_fetch_assoc($scdl);
$from = 'My Site Title';
do
{
$nextMailSchedule = $schedule['next_schedule'];
if(strtotime('now UTC') >= $nextMailSchedule)
{
$currentUserID = $schedule['mail_userid'];
$email = $schedule['mail_email'];
$scheduledMessageID = $schedule['schedule_message_id'];
//Mailers are sent to subscribers
if(callMailFunction($email, $from, $scheduledMessageID)) : //The callMailFunction() works perfectly, so no need to drop codes
{
$nextMessageInfo = getNextMessageInfo($currentUserID, $scheduledMessageID); //This function uses all previous records of current user to analyse and decide next mail to be sent to this user
$response = json_decode($nextMessageInfo, true);
$nextMessageID = $response['id'];
$nextMessageDuration = $response['duration'];
$periodicals = $response['periodic']; //this is save in either (hour, day, week, month, year)
$calculateNextMailDate = strtotime('now UTC + ' . $nextMessageDuration . ' ' . $periodicals);//Full codes looks like (strtotime('now UTC + 3 day'))
//Once the last scheduled mail is sent, reschedule to the next available date
mysqli_query($config, sprintf("UPDATE mail_schedules SET next_schedule = '" . $calculateNextMailDate . "'
AND schedule_message_id = '" . $nextMessageID . "' WHERE mail_id = '" . $schedule['mail_id'] . "'")) or die(mysqli_error());
}
endif;
//schedule_duration are saved in days
}
}while($schedule = mysqli_fetch_assoc($scdl));
}
endif;
?>
Currently, what I used to call endpoint.php
is by adding the following javascript code at the end of every files on my website which works perfect.
<script>setInterval($.post("endpoint.php"), 1000);</script> // ***
*** Though I know this is not the best practice.
My question is: How do I create persistent loading of the file endpoint.php
so as to keep track of exact time, without depending on active usage of the site.
Is there any websocket that I can plug my endpoint, or are there other ways to go about this.