developer/web/jobs.php

223 lines
8.8 KiB
PHP
Raw Normal View History

2013-08-28 22:47:04 +02:00
<?php
2013-10-16 20:59:18 +02:00
2013-08-28 22:47:04 +02:00
/**
* File: jobs.php.
* Author: Ulrich Block
* Date: 26.05.12
* Time: 10:56
* Contact: <ulrich.block@easy-wi.com>
*
* This file is part of Easy-WI.
*
* Easy-WI is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Easy-WI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Easy-WI. If not, see <http://www.gnu.org/licenses/>.
*
* Diese Datei ist Teil von Easy-WI.
*
* Easy-WI ist Freie Software: Sie koennen es unter den Bedingungen
* der GNU General Public License, wie von der Free Software Foundation,
* Version 3 der Lizenz oder (nach Ihrer Wahl) jeder spaeteren
* veroeffentlichten Version, weiterverbreiten und/oder modifizieren.
*
* Easy-WI wird in der Hoffnung, dass es nuetzlich sein wird, aber
* OHNE JEDE GEWAEHELEISTUNG, bereitgestellt; sogar ohne die implizite
* Gewaehrleistung der MARKTFAEHIGKEIT oder EIGNUNG FUER EINEN BESTIMMTEN ZWECK.
* Siehe die GNU General Public License fuer weitere Details.
*
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
* Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
*/
if (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
2013-08-28 22:47:04 +02:00
$timelimit=ini_get('max_execution_time')-10;
} else {
$timelimit=600;
}
2013-10-16 20:59:18 +02:00
2013-08-28 22:47:04 +02:00
if (isset($argv)) {
$args = array();
2013-08-28 22:47:04 +02:00
foreach ($argv as $a) {
if ($a == 'deamon') $deamon = true;
else if (is_numeric($a)) $sleep = $a;
2013-08-28 22:47:04 +02:00
else {
$e=explode(':',$a);
if (isset($e[1])) $args[$e[0]] = $e[1];
2013-08-28 22:47:04 +02:00
}
}
2013-10-13 13:11:38 +02:00
if (!isset($deamon)) {
2013-08-28 22:47:04 +02:00
print 'Running job management as cronjob'."\r\n";
$deamon = false;
2013-08-28 22:47:04 +02:00
set_time_limit($timelimit);
} else {
print 'Running job management as Deamon'."\r\n";
}
2013-10-13 13:11:38 +02:00
if (!isset($sleep)) $sleep=60;
2013-08-28 22:47:04 +02:00
}
if (!isset($ip) or $_SERVER['SERVER_ADDR'] == $ip) {
define('EASYWIDIR', dirname(__FILE__));
include(EASYWIDIR . '/stuff/vorlage.php');
include(EASYWIDIR . '/stuff/functions.php');
include(EASYWIDIR . '/stuff/class_validator.php');
include(EASYWIDIR . '/stuff/class_rootserver.php');
include(EASYWIDIR . '/stuff/settings.php');
include(EASYWIDIR . '/stuff/ssh_exec.php');
include(EASYWIDIR . '/stuff/class_voice.php');
include(EASYWIDIR . '/stuff/mysql_functions.php');
include(EASYWIDIR . '/stuff/keyphrasefile.php');
$gsprache = getlanguagefile('general','uk',0);
2013-08-28 22:47:04 +02:00
class runGraph {
private $jobsDone = 0;
private $startTime = 0;
2013-08-28 22:47:04 +02:00
private $newLine="\r\n";
private $jobCount = 0;
private $spinnerCount = 0;
2013-08-28 22:47:04 +02:00
private $spinners=array('-','/','-','\\','|','/','-','\\','|','/');
private $spinner='-';
private $oneJobPercent = 1;
2013-08-28 22:47:04 +02:00
function __construct($jobCount,$newLine) {
$this->startTime=strtotime('now');
$this->jobCount = $jobCount;
2013-08-28 22:47:04 +02:00
if ($jobCount>0) {
$this->oneJobPercent=100/$jobCount;
} else {
$this->oneJobPercent=100;
}
$this->newLine = $newLine;
2013-08-28 22:47:04 +02:00
$this->startTime=strtotime('now');
}
public function updateCount($jobCount) {
$this->jobCount = $jobCount;
2013-08-28 22:47:04 +02:00
if ($jobCount>0) {
$this->oneJobPercent=100/$jobCount;
} else {
$this->oneJobPercent=100;
}
}
public function printGraph ($newCommand) {
$this->jobsDone = $this->jobsDone+1;
2013-08-28 22:47:04 +02:00
$percentDone=number_format($this->jobsDone*$this->oneJobPercent,2);
$elapsedSeconds=strtotime('now')-$this->startTime;
print $this->spinner . ' ' . $percentDone.'%'.' done; '.$elapsedSeconds.' Seconds elapsed; Last job: '.$newCommand.$this->newLine;
2013-08-28 22:47:04 +02:00
flush();
$this->runSpinner();
}
private function runSpinner () {
if ($this->newLine=="\r") {
if ($this->spinnerCount<9) {
$this->spinnerCount++;
} else {
$this->spinnerCount = 0;
2013-08-28 22:47:04 +02:00
}
$this->spinner = $this->spinners[$this->spinnerCount].' ';
2013-08-28 22:47:04 +02:00
} else {
$this->spinner = '';
2013-08-28 22:47:04 +02:00
}
}
function __destruct() {
$this->jobsDone = null;
$this->startTime = null;
$this->newLine = null;
$this->jobCount = null;
$this->spinnerCount = null;
$this->spinners = null;
$this->spinner = null;
$this->oneJobPercent = null;
2013-08-28 22:47:04 +02:00
unset($this->jobsDone,$this->startTime,$this->newLine,$this->jobCount,$this->spinnerCount,$this->spinners,$this->spinner,$this->oneJobPercent);
}
}
$runJobs = true;
2013-08-28 22:47:04 +02:00
if (isset($ip)) {
$newLine="\r\n";
} else {
$newLine="\r";
}
while ($runJobs == true) {
$countp = $sql->prepare("SELECT COUNT(`jobID`) AS `jobCount` FROM `jobs` WHERE `status` IS NULL OR `status`='1'");
2013-08-28 22:47:04 +02:00
$countp->execute();
$jobCount = $countp->rowCount();
2013-08-28 22:47:04 +02:00
print 'Total jobs open: '.$jobCount.'. Cleaning up outdated and duplicated entries'."\r\n";
updateStates('dl','us');
updateStates('dl');
updateStates('ad');
updateStates('md');
2013-08-28 22:47:04 +02:00
$countp->execute();
$jobCount = $countp->rowCount();
2013-08-28 22:47:04 +02:00
print "\r\n".'Total jobs open after cleanup: '.$jobCount."\r\n";
print 'Executing user cleanup jobs'."\r\n";
$startTime=strtotime('now');
$theOutput=new runGraph($jobCount,$newLine);
# us > vo > gs > my > vs
include(EASYWIDIR . '/stuff/jobs_user.php');
2013-08-28 22:47:04 +02:00
$countp->execute();
$jobCount = $countp->rowCount();
2013-08-28 22:47:04 +02:00
$theOutput->updateCount($jobCount);
print "\r\n".'Total jobs open after user cleanup jobs are done: '.$jobCount."\r\n";
print 'Executing voice jobs'."\r\n";
include(EASYWIDIR . '/stuff/jobs_voice.php');
2013-08-28 22:47:04 +02:00
$countp->execute();
$jobCount = $countp->rowCount();
2013-08-28 22:47:04 +02:00
$theOutput->updateCount($jobCount);
print "\r\n".'Total jobs open after voice jobs are done: '.$jobCount."\r\n";
print 'Executing TS DNS jobs'."\r\n";
include(EASYWIDIR . '/stuff/jobs_tsdns.php');
2013-08-28 22:47:04 +02:00
$countp->execute();
$jobCount = $countp->rowCount();
2013-08-28 22:47:04 +02:00
$theOutput->updateCount($jobCount);
print "\r\n".'Total jobs open after TS DNS jobs are done: '.$jobCount."\r\n";
print 'Executing mysql jobs'."\r\n";
include(EASYWIDIR . '/stuff/jobs_mysql.php');
2013-08-28 22:47:04 +02:00
$countp->execute();
$jobCount = $countp->rowCount();
2013-08-28 22:47:04 +02:00
$theOutput->updateCount($jobCount);
print "\r\n".'Total jobs open after mysql jobs are done: '.$jobCount."\r\n";
print 'Executing gameserver jobs'."\r\n";
include(EASYWIDIR . '/stuff/jobs_gserver.php');
2013-08-28 22:47:04 +02:00
$countp->execute();
$jobCount = $countp->rowCount();
2013-08-28 22:47:04 +02:00
$theOutput->updateCount($jobCount);
print "\r\n".'Total jobs open after gameserver jobs are done: '.$jobCount."\r\n";
print 'Executing root server jobs'."\r\n";
include(EASYWIDIR . '/stuff/jobs_roots.php');
2013-08-28 22:47:04 +02:00
$countp->execute();
$jobCount = $countp->rowCount();
2013-08-28 22:47:04 +02:00
$theOutput->updateCount($jobCount);
print "\r\n".'Total jobs open after root server jobs are done: '.$jobCount."\r\n";
print 'Executing user remove jobs'."\r\n";
include(EASYWIDIR . '/stuff/jobs_user_rm.php');
2013-08-28 22:47:04 +02:00
print "\n";
$test='ech';
if ($deamon == true) {
$sql = null;
$theOutput = null;
2013-08-28 22:47:04 +02:00
unset($sql,$theOutput);
if ($dbConnect['type'] == 'mysql') {
$sql=new PDO($dbConnect['connect'],$dbConnect['user'],$dbConnect['pwd'], array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES utf8") );
2013-08-28 22:47:04 +02:00
} else {
$sql=new PDO($dbConnect['connect'],$dbConnect['user'],$dbConnect['pwd']);
}
if ($dbConnect['debug']==1) {
$sql->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}
flush();
print "\r\n".'Deamon run finished. Current memory usage is: '.memory_get_usage().' Bytes. Waiting '.$sleep.' seconds before next job run'."\r\n\r\n";
sleep($sleep);
} else {
$runJobs = false;
2013-08-28 22:47:04 +02:00
}
$query = $sql->prepare("UPDATE `settings` SET `lastCronJobs`=UNIX_TIMESTAMP() WHERE `resellerid`=0 LIMIT 1");
2013-08-28 22:47:04 +02:00
$query->execute();
}
}