prepared Task Executer
This commit is contained in:
+37
-28
@@ -1,8 +1,9 @@
|
||||
<?php
|
||||
|
||||
$maxExecutionTime =
|
||||
$maxExecutionTime = 80;
|
||||
$taskDir = 'tasks';
|
||||
phpinfo();
|
||||
$latesEndTime = time() + $maxExecutionTime;
|
||||
|
||||
function doRequest($task)
|
||||
{
|
||||
echo "doing Request: ".json_encode($task);
|
||||
@@ -12,37 +13,45 @@ function sendEmail($emailAddress, $filename)
|
||||
{
|
||||
echo "sending ".$filename." too ".$emailAddress;
|
||||
}
|
||||
if(is_dir($taskDir)) {
|
||||
$taskfiles = scandir($taskDir);
|
||||
$taskfiles = array_diff($taskfiles, array('.', '..'));
|
||||
sort($taskfiles);
|
||||
if(count($taskfiles) > 0) {
|
||||
$taskString = file_get_contents($taskDir.'/'.$taskfiles[0]);
|
||||
$task = json_decode($taskString, true);
|
||||
// Stop executing bevore reaching PHP Max Execution Time
|
||||
while($latesEndTime - time() > 60){
|
||||
// If the directory for Tasks is already created
|
||||
if(is_dir($taskDir)) {
|
||||
$taskfiles = scandir($taskDir);
|
||||
$taskfiles = array_diff($taskfiles, array('.', '..'));
|
||||
sort($taskfiles);
|
||||
// if there are any taks which are older then 2 Minutes. To be sure the file writing process is finished.
|
||||
if(count($taskfiles) > 0 && intval(explode("-", $taskfiles[0])[0]) + 120 < time()) {
|
||||
$taskString = file_get_contents($taskDir.'/'.$taskfiles[0]);
|
||||
$task = json_decode($taskString, true);
|
||||
|
||||
// if there are any requests to do, do the first
|
||||
if(count($task['requests']) > 0) {
|
||||
doRequest($task['requests'][0]);
|
||||
echo "Request done";
|
||||
array_shift($task['requests']);
|
||||
$taskString = json_encode($task);
|
||||
$taskfile = fopen($taskDir.'/'.$taskfiles[0], 'w');
|
||||
if($taskfile){
|
||||
fwrite($taskfile, $taskString);
|
||||
fclose($taskfile);
|
||||
// if there are any requests to do, do the first
|
||||
if(count($task['requests']) > 0) {
|
||||
doRequest($task['requests'][0]);
|
||||
echo "Request done";
|
||||
array_shift($task['requests']);
|
||||
$taskString = json_encode($task);
|
||||
$taskfile = fopen($taskDir.'/'.$taskfiles[0], 'w');
|
||||
if($taskfile){
|
||||
fwrite($taskfile, $taskString);
|
||||
fclose($taskfile);
|
||||
}
|
||||
}
|
||||
}
|
||||
// if there are no Requests left, send the E-Mail
|
||||
// if there are no Requests left, send the E-Mail
|
||||
else {
|
||||
sendEmail($task['email'], $taskDir.'/'.$taskfiles[0]);
|
||||
echo "Email sent";
|
||||
unlink($taskDir.'/'.$taskfiles[0]);
|
||||
sendEmail($task['email'], $taskDir.'/'.$taskfiles[0]);
|
||||
echo "Email sent";
|
||||
unlink($taskDir.'/'.$taskfiles[0]);
|
||||
echo "Task File deleted";
|
||||
}
|
||||
} else {
|
||||
// acutal Time - start time
|
||||
echo "nothing to do after: ".strval(time() - ($latesEndTime - $maxExecutionTime));
|
||||
echo "sleeping 10 seconds";
|
||||
sleep(10);
|
||||
}
|
||||
} else {
|
||||
echo "Task dir Empty";
|
||||
}
|
||||
} else {
|
||||
echo "no Task dir";
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user