1

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • 3
    These types of things are usually done with scheduling tools such as [`cron`](https://en.wikipedia.org/wiki/Cron): you schedule a command line PHP script to be executed at set intervals. Another option would be to run a PHP script from the command line with a `while` loop as kind of a daemon/service. If you search for any of these terms you'll likely find some ideas that suit you best. – Decent Dabbler Nov 01 '21 at 21:55
  • 1
    Only your own imagination is the limit, but I'd also go towards the direction what @DecentDabbler commented: What you have created is somewhat a deviation from "A Poor Mans' Cron" (no offence), so if you're able to go along with cron, you normally get it out of the box as it is damn standard. As an alternative consider a worker queue, but any queue you need to manage and for starters it is normally easier with cron - it also does not prevent you from doing any future steps. – hakre Nov 01 '21 at 22:17
  • Thanks @hakre. Your words are motivating. I have followed the guide you provided, and I'm already finding my way around Cron. Hopefully before the end of the day, I must have come up with something. – Festus Akushie Nov 02 '21 at 04:17
  • 1
    You have an error. [`mysqli_error()`](https://www.php.net/manual/en/mysqli.error.php) needs one argument. Please consider switching error mode on instead. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Nov 02 '21 at 07:02

0 Answers0