prepared Task Executer

This commit is contained in:
schrom01 2023-09-25 14:29:53 +02:00
parent 91d85232ca
commit 01095093d7
3 changed files with 43 additions and 30 deletions

View File

@ -61,7 +61,7 @@ $requests_to_do = Array();
} }
$taskdata = Array(); $taskdata = Array();
$taskdata['requests'] = $requests_to_do; $taskdata['requests'] = $requests_to_do;
$taskdata['email'] = 'roman.schenk37@gmail.com'; $taskdata['email'] = $_POST['email'];
$taskString = json_encode($taskdata); $taskString = json_encode($taskdata);
if(!is_dir($taskDir)){ if(!is_dir($taskDir)){
mkdir($taskDir, 0755, true); mkdir($taskDir, 0755, true);

View File

@ -135,7 +135,11 @@ include "zefixAPI.php";
<input type="checkbox" id="phonetischeSuche" name="phonetischeSuche" disabled> <input type="checkbox" id="phonetischeSuche" name="phonetischeSuche" disabled>
<label for="phonetischeSuche">Phonetische Suche (noch in Entwicklung)</label><br><br> <label for="phonetischeSuche">Phonetische Suche (noch in Entwicklung)</label><br><br>
<input type="submit" value="Suchergebnisse exportieren"> <label for="email">Ihre E-Mail Adresse:</label>
<p>Das aufbereiten der Daten kann lange dauern. Sie einen Download-Link per E-Mail sobald die Daten bereit sind.</p>
<input type="text" id="firma" name="firma" required><br><br>
<input type="submit" value="Export starten">
</form> </form>
<script> <script>

View File

@ -1,8 +1,9 @@
<?php <?php
$maxExecutionTime = $maxExecutionTime = 80;
$taskDir = 'tasks'; $taskDir = 'tasks';
phpinfo(); $latesEndTime = time() + $maxExecutionTime;
function doRequest($task) function doRequest($task)
{ {
echo "doing Request: ".json_encode($task); echo "doing Request: ".json_encode($task);
@ -12,37 +13,45 @@ function sendEmail($emailAddress, $filename)
{ {
echo "sending ".$filename." too ".$emailAddress; echo "sending ".$filename." too ".$emailAddress;
} }
if(is_dir($taskDir)) { // Stop executing bevore reaching PHP Max Execution Time
$taskfiles = scandir($taskDir); while($latesEndTime - time() > 60){
$taskfiles = array_diff($taskfiles, array('.', '..')); // If the directory for Tasks is already created
sort($taskfiles); if(is_dir($taskDir)) {
if(count($taskfiles) > 0) { $taskfiles = scandir($taskDir);
$taskString = file_get_contents($taskDir.'/'.$taskfiles[0]); $taskfiles = array_diff($taskfiles, array('.', '..'));
$task = json_decode($taskString, true); 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 there are any requests to do, do the first
if(count($task['requests']) > 0) { if(count($task['requests']) > 0) {
doRequest($task['requests'][0]); doRequest($task['requests'][0]);
echo "Request done"; echo "Request done";
array_shift($task['requests']); array_shift($task['requests']);
$taskString = json_encode($task); $taskString = json_encode($task);
$taskfile = fopen($taskDir.'/'.$taskfiles[0], 'w'); $taskfile = fopen($taskDir.'/'.$taskfiles[0], 'w');
if($taskfile){ if($taskfile){
fwrite($taskfile, $taskString); fwrite($taskfile, $taskString);
fclose($taskfile); fclose($taskfile);
}
} }
} // if there are no Requests left, send the E-Mail
// if there are no Requests left, send the E-Mail
else { else {
sendEmail($task['email'], $taskDir.'/'.$taskfiles[0]); sendEmail($task['email'], $taskDir.'/'.$taskfiles[0]);
echo "Email sent"; echo "Email sent";
unlink($taskDir.'/'.$taskfiles[0]); 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";
} }
?> ?>