timed functions and cycles

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
smarty
Forum Newbie
Posts: 4
Joined: Mon Dec 29, 2003 10:47 am
Location: Tallinn

timed functions and cycles

Post by smarty »

I want to create a function that would do queries from a database during a given time period.
As an example, during 45 sec the function should make a query from the database in every 5 sec and check the field 'control' if it has changed from '0' to '1' or not.
If the value is '1', the cycle should end in there and launch 'function first()' and also start a new cycle for 45 sec that queries in every 5 sec for the value '1' in the database field 'control2'. Again, if the value is '1' it should end the current cycle, launch 'function second()' and end the script.
If during the first cycle no changes in the database were detected 'function three()' should be launched and a new cycle should be started too.
If during the second cycle no changes in the database were detected 'function three()' should be launched and the script should end its work.
Script would be launched with 'exec()'

Code: Select all

<?php 
$times = 9; 
$count = 0; 
while ($count < $times) { 
$count++; 
$query=mysql_query("SELECT devices FROM control"); 
flush(); 
sleep(5); 
} 
?>

?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

At the heart of the class is a dynamic engine (loadEngine method) and the $processes array (a list of object methods). Array_shift knocks a value off the list then, while the loop is running, you can add new items to $processes to continue looping (the load action methods). It'll keep going forever if you aren't careful (although there is a die in this example).

This isn't the finished article. I didn't test nor have a good look with refactoring in mind. Hope it gives you some ideas at least.

Code: Select all

<?php

class DynamicScripterator
{
    $max_time = 45;
    $period = 5;

    function DynamicScripterator() 
    {
        $this->start = $this->getMicrotime();
        $this->log = array();
        $this->processes = array('action1');
        $this->i = 1;
    }
    
    function loadEngine() 
    {
        while(!is_null($process = array_shift($this->processes)))
        {
            $this->scheduler($process);
        }        
    }


    //////////////// PRIVATE //////////////////////


    function scheduler($method_name)
    {        
        if($now = $this->getElapsedTime() > $max_time)
        {
            die('Script complete');
        }        
        if(($this->i * $this->period) > $now)
        {
            $this->performAction($method_name, $now);
            $this->loadNextAction();
            $this->i++;    
        
        } else {
        
            $this->reloadCurrentAction($method_name);
        }
    }

    function performAction($method_name, $now) 
    {
        $this->$method_name();
        $this->log[] = $now;
    }

    function loadNextAction() 
    {
        $this->processes[] = $this->selectNextAction();        
    }

    function reloadCurrentAction($method_name) 
    {
        $this->processes[] = $method_name;
    }

    function selectNextAction() 
    {
        // your code to select the next action
        // return the method name
        return $name;
    }    

    function action1() 
    {
        
    }

    function action2() 
    {
        
    }

    // ..etc - other actions

    // & getMicrotime / getElapsedTime

}

?>
PS: could equally well load up objects from $processes rather than object methods.
Post Reply