Location: PHPKode > projects > PhpBlock > A9.8/script/tick/abstractticker.php
<?php
include_once $PATH_TO_CODE."/script/server.class.php";
Log::getInstance()->isDebugOn = getGetPost("debug", false);

function getTimeBase() {
    $locTimeBase = Server::getInstance()->getValue(Server::$SERVER_INFO_TYPE1_TICKER, 3);
    if(!$locTimeBase) {
        $locTimeBase = 1000*10; #10 seconds
    }
    return $locTimeBase;
}

function setTimeBase($parTickWithTimeBaseArray) {
	$locTimeBase = getTimeBase();
    foreach($parTickWithTimeBaseArray as $locTick) {
        $locTick->setTimeBase($locTimeBase);
    }
}

function doTicker($parTickWithFixedTimeArray, $parTickWithTimeBaseArray) {
	$locTimeBase = getTimeBase();
	$locMaxTickCount = getGetPost("count", 1);
    set_time_limit(60*$locMaxTickCount*Constante::getWorldLeftWidth());

    $locPass = getGetPost("pass", "");
    if($locPass!="123456") {
        $locIsInfo = true;
    }

    if($locIsInfo) {
        setTimeBase($parTickWithTimeBaseArray);
        echo "<table border=1 width='100%'>";
        echo "<th>tick name<th>standard duration<th>time before execution<th>execution time (DD/MM/YYYY)";
        foreach($parTickWithTimeBaseArray as $locTick) {
            echo "<tr>";
            echo "<td>";
            echo $locTick->getName();
            echo "<td>";
            $locTimeBetweenRunsInS = ($locTick->getTimeBetweenRunsInMs())/1000.0;
            echo formatDateInSecondToDHMS($locTimeBetweenRunsInS);
            echo "<td>";
            $locTimeBeforeTick = ($locTick->getTimeBetweenRunsInMs()-$locTick->getTimeElapsedInMs())/1000;
            if($locTimeBeforeTick <= 0) {
                echo "ASAP<td>ASAP";
            } else {
                echo formatDateInSecondToDHMS((int)$locTimeBeforeTick);
                echo "<td>".getTimeWithTimeStamp(time()+$locTimeBeforeTick);
            }
            echo "</tr>";
        }
        echo "</table>";
        exit;
    }

    Log::info("time base in s : ".($locTimeBase/1000.0));
	Log::info($PATH_TO_CODE);
	Log::info($PATH_TO_IMAGE);
	Log::info(Site::$INSTALLATION_PATH_TO_BASE);

    
    $locMinimalSleepInMs=200;
    $locTimeInMsBetweenTwoTick = Server::getInstance()->getValue(Server::$SERVER_INFO_TYPE1_TICKER, 4);

    
    Log::info("tick count: $locMaxTickCount");
    $locTickCount = 0;
    //TODO use constant for type2
    $locEndTickMs=Server::getInstance()->getValue(Server::$SERVER_INFO_TYPE1_TICKER, 6);
    while(Server::getInstance()->getValue(Server::$SERVER_INFO_TYPE1_TICKER, 1) && $locTickCount < $locMaxTickCount) {
        Log::info("Tick number : $locTickCount");

        $locBeginWhileInMs=microtime(true)*1000;
        if(Server::getInstance()->getValue(Server::$SERVER_INFO_TYPE1_TICKER, 2)) {
            setTimeBase($parTickWithTimeBaseArray);

            $locTimeElapsedTickInMs = $locBeginWhileInMs - $locEndTickMs;
            Log::info("Elapsed time : ".$locTimeElapsedTickInMs."ms");

            foreach($parTickWithFixedTimeArray as $locTick) {
                $locTick->timeElapsed(min(3600*24*30, $locTimeElapsedTickInMs));
               // commit();
            }
            foreach($parTickWithTimeBaseArray as $locTick) {
                $locTick->timeElapsed(min(3600*24*30, $locTimeElapsedTickInMs));
                //commit();
            }
        } else {
            Log::info("Just do sleep");
        }
        Server::getInstance()->refreshServerInfo();//Does a commit
        $locEndTickMs=$locBeginWhileInMs;
        Server::getInstance()->upsertValue(Server::$SERVER_INFO_TYPE1_TICKER, 6, $locEndTickMs);

        $locTimeInMsBetweenTwoTick = Server::getInstance()->getValue(Server::$SERVER_INFO_TYPE1_TICKER, 4);
        if(!$locTimeInMsBetweenTwoTick) {
            $locTimeInMsBetweenTwoTick = 1000;
        }
        $locMinimalSleepInMs = Server::getInstance()->getValue(Server::$SERVER_INFO_TYPE1_TICKER, 5);
        if(!$locMinimalSleepInMs) {
            $locMinimalSleepInMs = 200;
        }

        $locDurationTickInMs = microtime(true)*1000-$locBeginWhileInMs;
        Log::info("Tick duration : ".$locDurationTickInMs."ms");

        $locSleepInMs = max(($locTimeInMsBetweenTwoTick-$locDurationTickInMs), $locMinimalSleepInMs);
        Log::info("Sleep time : ".$locSleepInMs."ms");

        usleep($locSleepInMs*1000);
        Log::info("");
        $locTickCount++;
    }
    Log::info("Stop ticker");
    disconnect();
}
?>
Return current item: PhpBlock