View Single Post
  #12  
Old 12-20-2016, 08:24 AM
Hall of Famer's Avatar
Hall of Famer Hall of Famer is offline
Administrator, Lead Coder
 
Join Date: Dec 2008
Location: South Brunswick
Posts: 4,448
Gender: Male
Credits: 338,797
Hall of Famer is on a distinguished road
Default

First of all, you need to create a database table called adopts_tasks(assuming your database prefix is adopts_, change to whatever it is if not). This table should contain some useful data field such as task id, name, description, action to execute, last execution time, next execution time, time interval between each execution, etc.

Second, create a script file for each of your tasks, and find a way to link it to the data field 'action to execute' in your new database table(ie. mooddrop.php). A simple way is to just use include/require and the file being a procedural file. Or even better, write Task classes that will make it easier to inherit some parent properties/variables. Mysidia Adoptables v1.4.0 will use the latter approach, but if you just barely get a hang of programming the former procedural approach may be more feasible.

Next, add some code to index.php file, this way this code always runs whenever someone visits your site, even a search bot. The code will need to pull all the scheduled tasks ready to run from your new database table(current time is greater than or equal to the next execution time), and execute the script file each task links to. After the script is executed, increment the next execution time by the wait time, and update your database table so the tasks will not run again until it should.

For instance, if you have a scheduled task linking to file mooddrop.php, its next execution time is today at 8am and wait time is 3 hours. When you visit your site at 9am, it will load the scheduled task from database, execute the script inside mooddrop.php, and update the next execution time from 8am to 11am(3 hours interval). Then the task will not run again until someone visits your site again after 11am.

Now you see, it is a simple trick that will work well given you have an active site with visitors at least once per 3-5 minutes. If you are on a virtual machine/devbox, it will not function properly since you are the sole visitor. In fact, this is exactly how our forum software's scheduled task system works. It uses pseudo-cronjobs, and you dont have to manually set up all cronjobs from cpanel.

Of course, you may face some challenges such as how to write task files/classes, how to compare date, and how to store data values in database. Lemme know if you have any questions, I will try to explain and help you with this.
__________________


Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site.
Reply With Quote