first commit
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
$job_name = pathinfo(__FILE__, PATHINFO_FILENAME);
|
||||
echo "[".date("Y-m-d H:i:s")."] ".$job_name." job is starting.\n";
|
||||
|
||||
require('lock.php');
|
||||
$lock_file = lock_pid_file();
|
||||
|
||||
set_time_limit(0); // No limit!
|
||||
|
||||
require('../backend.php');
|
||||
require('common/Logger.php');
|
||||
|
||||
$httpAuthToken = $savvyext->cfgReadChar('system.oauth2_token');
|
||||
$encryptionAlg = $savvyext->cfgReadChar('encryption.algorithm');
|
||||
$encryptionKey = $savvyext->cfgReadChar('encryption.key');
|
||||
$encryptionIV = $savvyext->cfgReadChar('encryption.iv');
|
||||
|
||||
$baseURL = $savvyext->cfgReadChar('system.api_url');
|
||||
|
||||
$db_host = $savvyext->cfgReadChar('database_replica.host');
|
||||
$db_name = $savvyext->cfgReadChar('database_replica.name');
|
||||
$db_user = $savvyext->cfgReadChar('database_replica.user');
|
||||
$db_pass = $savvyext->cfgReadChar('database_replica.pass');
|
||||
$db_port = $savvyext->cfgReadLong('database_replica.port');
|
||||
$connstr = "host=${db_host} port=${db_port} dbname=${db_name} user=${db_user} password=${db_pass}";
|
||||
$conn = pg_connect($connstr);
|
||||
|
||||
$q = "SELECT * FROM quotes WHERE completed IS NULL or travel_date IS NULL";
|
||||
//$q.= " AND transport_provider_id=2 "; // DEBUG
|
||||
$q.= " ORDER BY created DESC";
|
||||
|
||||
$r = pg_query($conn, $q);
|
||||
|
||||
if ($r && pg_num_rows($r)) {
|
||||
while ($f=pg_fetch_assoc($r)) {
|
||||
list($res,$cost) = check_quote($f["id"]);
|
||||
if ($res>0) {
|
||||
if ($res==2) {
|
||||
echo "[".date("Y-m-d H:i:s")."] Quote ${cost} for ID#".$f["id"].".\n";
|
||||
} else {
|
||||
echo "[".date("Y-m-d H:i:s")."] Unexpected result for ID#".$f["id"].".\n";
|
||||
}
|
||||
} else {
|
||||
if ($res==-1) {
|
||||
echo "[".date("Y-m-d H:i:s")."] Final quote failure for ID#".$f["id"].".\n";
|
||||
} else {
|
||||
echo "[".date("Y-m-d H:i:s")."] check_quote failed(${res}) for ID#".$f["id"].".\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function check_quote($id) {
|
||||
global $job_name;
|
||||
if ($id>0) { // minimal sanity
|
||||
list($payload,$decrypted,$result,$body) = main_api_get("/trips/api/quote/",$id);
|
||||
$log = [
|
||||
'job_name' => $job_name,
|
||||
'function' => __FUNCTION__,
|
||||
'request' => "/trips/api/quote/".$id,
|
||||
'input_data' => $id,
|
||||
'response_data' => $payload,
|
||||
];
|
||||
Logger::debug($log);
|
||||
if (is_array($payload) && array_key_exists('id',$payload) && $payload['id']>0) {
|
||||
if (array_key_exists('cost',$payload) && $payload['cost']>0) {
|
||||
return [2,$payload['cost']];
|
||||
}
|
||||
if (array_key_exists('travel_date',$payload) && $payload['travel_date']!='') {
|
||||
return [-1, NULL];
|
||||
}
|
||||
} else {
|
||||
return [-2, NULL];
|
||||
}
|
||||
}
|
||||
return [-3, NULL];
|
||||
}
|
||||
|
||||
function main_api_get($endpoint,$input) {
|
||||
global $baseURL, $encryptionAlg, $encryptionKey, $encryptionIV, $httpAuthToken;
|
||||
if ($endpoint!="" && $input!="") { // minimal sanity
|
||||
sleep(1);
|
||||
$url = $baseURL . $endpoint . $input;
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_VERBOSE, false);
|
||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
||||
'Content-Type: application/json',
|
||||
'Authorization: Server-Token ' . $httpAuthToken,
|
||||
"client_id: BATCH"
|
||||
)
|
||||
);
|
||||
|
||||
$body = curl_exec($ch);
|
||||
$result = json_decode($body,true);
|
||||
|
||||
$decrypted = openssl_decrypt(
|
||||
hex2bin(
|
||||
$result['payload']
|
||||
),
|
||||
$encryptionAlg,
|
||||
$encryptionKey,
|
||||
OPENSSL_RAW_DATA,
|
||||
$encryptionIV
|
||||
);
|
||||
$payload = json_decode($decrypted, true);
|
||||
return [$payload,$decrypted,$result,$body];
|
||||
}
|
||||
return [NULL,NULL,NULL,NULL];
|
||||
}
|
||||
|
||||
unlock_pid_file($lock_file);
|
||||
|
||||
echo "[".date("Y-m-d H:i:s")."] ".$job_name." job complete.\n";
|
||||
Reference in New Issue
Block a user