<?php

function startjob($jobname) {
 global $interval;
 
 webstats();
 
 usleep(100000); // 100 milliseconds / 0.1 seconds - load control ... auf 0.3 gestellt

 $currenttime=time();

 $lastruntime=$interval[$jobname]['lastruntime'];
 $jobinterval=$interval[$jobname]['interval'];
 $timemode=$interval[$jobname]['mode'];

 switch ($timemode) {
   case "seconds": $lastruntime=$lastruntime+$jobinterval;
   break;
   case "minutes": $lastruntime=$lastruntime+($jobinterval*60);
   break;
   case "hours": $lastruntime=$lastruntime+($jobinterval*60*60);
   break;
   case "days": $lastruntime=$lastruntime+($jobinterval*86400);
   break;
 } // switch ($timemode) 

 if ($currenttime > $lastruntime) {
  if (function_exists($jobname."Job")) eval($jobname."Job();"); // call function
  if ($currenttime > $interval[$jobname]['lastruntime']) { $interval[$jobname]['lastruntime']=$currenttime; } // overwrite lastruntime @ jobs... 
 } //if ($currenttime>$lastruntime)
} // function end

function convertMemory($size) {
 $unit=array('b','kb','mb','gb','tb','pb');
 return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
} // function end

function someStatsJob() {
 global $globalsettings, $usedMemory, $usedPeakMemory;

  $systemLoad=sys_getloadavg();
  $globalsettings["bot"]["systemload"][0]=round($systemLoad[0],2); // some system give float back... :/
  $globalsettings["bot"]["systemload"][1]=round($systemLoad[1],2);
  $globalsettings["bot"]["systemload"][2]=round($systemLoad[2],2);
  $usedPeakMemory=convertMemory(memory_get_peak_usage(true));
  $usedMemory=convertMemory(memory_get_usage(true)); 
 
} // function end

function webstats() {
 global $runtimeForwardCounter, $shm;
 shm_put_var($shm, 1, $runtimeForwardCounter); 
 shm_put_var($shm, 2, realForwardCount()); 
} // function end

function aliveJob() {
 global $floodwait, $globalsettings, $usedMemory, $usedPeakMemory, $forwardstopmsg, $floodchallange, $shm;

 if ($floodwait != -1) {
	
  $currenttime=time();
  $waittime=dhms($floodwait - $currenttime);
 
  if ($forwardstopmsg == "floodwait") { // adapt bot floodtime to tg session...
   $floodchallange["floodtimer"]=time(); 
   $floodchallange["floodtimerreset"]=1; 
  }  // if ($forwardstopmsg == "floodwait" ) 

  if ($globalsettings["bot"]["systemload"] == array())  { 
 //  echo "is an array\n"; 	  
   $globalsettings["bot"]["systemload"]=array(); // wtf... 
  }  // else echo "is no array\n"; 	  

  if (array_key_exists("0", $globalsettings["bot"]["systemload"]))  logger("job: bot is still alive - load avg: ".$globalsettings["bot"]["systemload"][0]." - mem: ". $usedPeakMemory."/ ".$usedMemory." - ".$forwardstopmsg.": ".$waittime." - queue: ".realForwardCount()); 

  shm_put_var($shm, 3, array(0=>1, 1=> $forwardstopmsg, 2=> $waittime)); 

 } else { 
  
  if ($globalsettings["bot"]["systemload"] == array())  { 
 //  echo "is an array\n"; 	  
   $globalsettings["bot"]["systemload"]=array(); // wtf... 
  }  // else echo "is no array\n"; 	  
 
  if (!array_key_exists(0, $globalsettings["bot"]["systemload"])) someStatsJob();
	  
  logger("job: bot is still alive - load avg: ".$globalsettings["bot"]["systemload"][0].", ".$globalsettings["bot"]["systemload"][1].", ".$globalsettings["bot"]["systemload"][2]." - memory(peak/ used): ".$usedPeakMemory."/ ".$usedMemory." - queue: ".realForwardCount()); 
  
  $runtime=dhms(time() - $floodchallange["floodtimer"], 2);
  $acthour=$runtime["day"] * 24 + $runtime["hour"];
  $actmin= $runtime["min"];
  if (!array_key_exists("acthour-".$acthour, $floodchallange)) $floodchallange["acthour-".$acthour]["forwardcount"]=0; // fix? same problem @ fwdstepper...
  shm_put_var($shm, 3, array(0=> 0, 1=> "forwards act. tg-session hour", 2=> $floodchallange["acthour-".$acthour]["forwardcount"]." (".$actmin." min.)")); 
 }
} // function end

function channelinfoJob() {
 global $channelInfo, $channels;

 logger("job: renew channelInfo"); 
 $channelsInfoNew=loadChannelConfig(false);
 $channelInfo=rebuildChannelInfo($channelsInfoNew);
 $channels=rebuildChannels($channelsInfoNew);

} // funtion end

function readchannelsJob() {
 logger("job: read channels"); 
 getLastMessages("-1", "-1", $startup);
} // function end


function jobs() {
 global $interval;
 if (is_array($interval)) if (count($interval) != 0) foreach(array_keys($interval) as $j) startjob($j);
} // function end