View Single Post
  #24  
Old 01-21-2017, 05:03 PM
IntoRain's Avatar
IntoRain IntoRain is offline
Moderator
 
Join Date: Jul 2013
Location: Portugal
Posts: 461
Gender: Female
Credits: 19,379
IntoRain is on a distinguished road
Default

If your host allows cronjobs, you can just have a page with the tasks you want to perform and set it up through the cpanel ("I want this function from this file to run every day").

All the automatic tasks I've set up are basically like this:

PHP Code:
class CronController extends AppController{

    public function 
__construct(){

    }
    
    public function 
index(){

    }
    
    public function 
resetNotifs() {
        
$mysidia Registry::get("mysidia");
        
$mysidia->db->delete("notifications""Isread = '1'");
    }
    
    public function 
resetClicks() {
        
$mysidia Registry::get("mysidia");
        
$now = new DateTime();
        
$mysidia->db->delete("vote_voters""date != '{$now->format('Y-m-d')}'");
    }
    
    public function 
resetBiomes() {
        
$mysidia Registry::get("mysidia");
        
$mysidia->db->delete("generatedadoptables""1");
    }
    

But this depends on if your host allows cronjobs.

To do it like HoF is saying, you can create a class file for each task you want. For example ResetClicksTask, LowerMoodTask, ... (which can extend a class Task, if you want inheritance)
Each task class has a function like executeTask() that does what you want. In this case it would be like

PHP Code:
class ResetClicksTask extends Model//or extends Task
    
       
public function __construct(){
       }

    public function 
executeTask() {
        
$mysidia Registry::get("mysidia");
        
$now = new DateTime();
        
$mysidia->db->delete("vote_voters""date != '{$now->format('Y-m-d')}'");
    }
    

Then from the index.php file, you can create a function that checks if any task from the database can run, like

PHP Code:
$mysidia Registry::get("mysidia");
$tasks $mysidia->db->select("tasks", array("id""name""executiontime""waittime"), "...");

while(
$task $tasks->fetchObject()) {
        if(
$currenttime >= $task->executiontime) {
              
$taskName $task->name "Task"//so it looks like ResetClicksTask
              
$taskToRun = new $taskName();
              
$taskToRun->executeTask();//run the execute function
              //update the next exe time
              
$newexetime $task->executiontime $task->waittime;
              
$mysidia->db->update("tasks", array("executiontime" => $newexetime), "id = '{$task->id}'");
        }


So when anyone visits the index, the function calls all the tasks that need to run.
__________________


asp.net stole my soul.
Reply With Quote