prepared Task Executer
This commit is contained in:
parent
b178df10e3
commit
3fa9fa6d79
|
@ -54,7 +54,6 @@ $requests_to_do = Array();
|
||||||
// Loop through the selected values
|
// Loop through the selected values
|
||||||
foreach ($sitze as $sitz) {
|
foreach ($sitze as $sitz) {
|
||||||
$data["legalSeatId"] = $sitz;
|
$data["legalSeatId"] = $sitz;
|
||||||
//$response = sendAPICompanySearchRequest($username, $password, $data);
|
|
||||||
$requests_to_do[] = $data;
|
$requests_to_do[] = $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,7 +65,7 @@ $taskString = json_encode($taskdata);
|
||||||
if(!is_dir($taskDir)){
|
if(!is_dir($taskDir)){
|
||||||
mkdir($taskDir, 0755, true);
|
mkdir($taskDir, 0755, true);
|
||||||
}
|
}
|
||||||
$taskfilename = $taskDir.'/'.time().'-'.bin2hex(random_bytes(4)).'.txt';
|
$taskfilename = $taskDir.'/'.time().'-'.bin2hex(random_bytes(4)).'.json';
|
||||||
$taskfile = fopen($taskfilename, 'w');
|
$taskfile = fopen($taskfilename, 'w');
|
||||||
if($taskfile){
|
if($taskfile){
|
||||||
fwrite($taskfile, $taskString);
|
fwrite($taskfile, $taskString);
|
||||||
|
|
|
@ -1,12 +1,49 @@
|
||||||
<?php
|
<?php
|
||||||
|
include "zefixAPI.php";
|
||||||
|
|
||||||
$maxExecutionTime = 80;
|
$maxExecutionTime = 80;
|
||||||
$taskDir = 'tasks';
|
$taskDir = 'tasks';
|
||||||
|
$downloadDir = 'download';
|
||||||
$latesEndTime = time() + $maxExecutionTime;
|
$latesEndTime = time() + $maxExecutionTime;
|
||||||
|
|
||||||
function doRequest($task)
|
|
||||||
|
if(!is_dir($downloadDir)){
|
||||||
|
mkdir($downloadDir, 0755, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function doRequest($data, $filename, $username, $password)
|
||||||
{
|
{
|
||||||
echo "doing Request: ".json_encode($task);
|
echo "doing Request: ".json_encode($data);
|
||||||
|
echo "<br>";
|
||||||
|
$response = sendAPICompanySearchRequest($username, $password, $data);
|
||||||
|
$companyArray = json_decode($response, true);
|
||||||
|
// TODO implement
|
||||||
|
$companyData = [
|
||||||
|
['John', 30, 'New York'],
|
||||||
|
['Alice', 25, 'Los Angeles'],
|
||||||
|
['Bob', 35, 'Chicago'],
|
||||||
|
];
|
||||||
|
$companyData = array();
|
||||||
|
foreach ($companyArray as $company) {
|
||||||
|
$companyData[] = [$company['name'],$company['ehraid'],$company['uid'],$company['chid'],$company['legalSeat'],$company['registryOfCommerceId'],$company['legalForm']['name']['de'],$company['status'],$company['sogcDate'],$company['deletionDate']];
|
||||||
|
}
|
||||||
|
if (! file_exists($filename)) {
|
||||||
|
// If File doesn't exist yet
|
||||||
|
$file = fopen($filename, 'w');
|
||||||
|
if($file) {
|
||||||
|
// TODO change headers
|
||||||
|
fputcsv($file, ['name', 'ehraid', 'uid', 'chid', 'legalSeat', 'registryOfCommerceId', 'legalForm', 'status', 'sogcDate', 'deletionDate']);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If file already exists
|
||||||
|
$file = fopen($filename, 'a');
|
||||||
|
}
|
||||||
|
if ($file) {
|
||||||
|
foreach ($companyData as $row){
|
||||||
|
fputcsv($file, $row);
|
||||||
|
}
|
||||||
|
fclose($file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendEmail($emailAddress, $filename)
|
function sendEmail($emailAddress, $filename)
|
||||||
|
@ -20,15 +57,16 @@ while($latesEndTime - time() > 60){
|
||||||
$taskfiles = scandir($taskDir);
|
$taskfiles = scandir($taskDir);
|
||||||
$taskfiles = array_diff($taskfiles, array('.', '..'));
|
$taskfiles = array_diff($taskfiles, array('.', '..'));
|
||||||
sort($taskfiles);
|
sort($taskfiles);
|
||||||
// if there are any taks which are older then 2 Minutes. To be sure the file writing process is finished.
|
// if there are any taks which are older then 1 Minutes. To be sure the file writing process is finished.
|
||||||
if(count($taskfiles) > 0 && intval(explode("-", $taskfiles[0])[0]) + 120 < time()) {
|
if(count($taskfiles) > 0 && intval(explode("-", $taskfiles[0])[0]) + 60 < time()) {
|
||||||
$taskString = file_get_contents($taskDir.'/'.$taskfiles[0]);
|
$taskString = file_get_contents($taskDir.'/'.$taskfiles[0]);
|
||||||
$task = json_decode($taskString, true);
|
$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], str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]), $username , $password);
|
||||||
echo "Request done";
|
echo "Request done";
|
||||||
|
echo "<br>";
|
||||||
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');
|
||||||
|
@ -39,15 +77,18 @@ while($latesEndTime - time() > 60){
|
||||||
}
|
}
|
||||||
// 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'], str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]));
|
||||||
echo "Email sent";
|
echo "Email sent";
|
||||||
|
echo "<br>";
|
||||||
unlink($taskDir.'/'.$taskfiles[0]);
|
unlink($taskDir.'/'.$taskfiles[0]);
|
||||||
echo "Task File deleted";
|
echo "Task File deleted";
|
||||||
|
echo "<br>";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// acutal Time - start time
|
// acutal Time - start time
|
||||||
echo "nothing to do after: ".strval(time() - ($latesEndTime - $maxExecutionTime));
|
echo "nothing to do after: ".strval(time() - ($latesEndTime - $maxExecutionTime));
|
||||||
echo "sleeping 10 seconds";
|
echo "sleeping 10 seconds";
|
||||||
|
echo "<br>";
|
||||||
sleep(10);
|
sleep(10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,8 @@ $password = getenv("password");
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @return bool|string
|
* @return bool|string
|
||||||
*/
|
*/
|
||||||
function sendAPICompanySearchRequest(string $username, string $password, array $data): string|bool
|
function sendAPICompanySearchRequest(string $username, string $password, array $data): string|bool {
|
||||||
{
|
|
||||||
// API endpoint
|
// API endpoint
|
||||||
$apiUrl = 'https://www.zefix.admin.ch/ZefixPublicREST/api/v1/company/search';
|
$apiUrl = 'https://www.zefix.admin.ch/ZefixPublicREST/api/v1/company/search';
|
||||||
|
|
||||||
|
@ -24,7 +24,6 @@ $headers = array(
|
||||||
|
|
||||||
// Initialize cURL session
|
// Initialize cURL session
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
|
|
||||||
// Set cURL options
|
// Set cURL options
|
||||||
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
|
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
|
||||||
curl_setopt($ch, CURLOPT_URL, $apiUrl);
|
curl_setopt($ch, CURLOPT_URL, $apiUrl);
|
||||||
|
|
Loading…
Reference in New Issue