Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions webservices/cron_status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

require_once(__DIR__.'/../approot.inc.php');
require_once(APPROOT.'/application/application.inc.php');
require_once(APPROOT.'/application/startup.inc.php');

const ERROR_ALREADY_RUNNING = "error_already_running";
const RUNNING = "running";
const STOPPED = "stopped";
const ERROR = "error";

$sAuthUser = ReadParam("auth_user");
$sAuthPwd = ReadParam("auth_pwd");

try {
$sAuthUser = ReadParam("auth_user");
$sAuthPwd = ReadParam("auth_pwd");

if (is_null($sAuthUser) || is_null($sAuthPwd)) {
throw new \Exception("Missing credentials");
}

if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd)) {
UserRights::Login($sAuthUser); // Login & set the user's language
} else {
throw new \Exception("Invalid credentials");
}

$sLogFilename = ReadParam("cron_log_file", "cron.log");

$sStatus = STOPPED;
$sMsg = "";
$sLogFile = APPROOT."log/$sLogFilename";
if (is_file($sLogFile)) {
$sContent = exec("tail -n 1 $sLogFile");
if (0 === strpos($sContent, 'Exiting: ')) {
exec("tail -n 2 $sLogFile", $aContent);
//var_dump($aContent);
$sContent = implode("\n", $aContent);
if (false !== strpos($sContent, 'Already running')) {
$sStatus = ERROR_ALREADY_RUNNING;
} else if (preg_match('/ERROR: (.*)\\n/', $sContent, $aMatches)) {
$sMsg = "$aMatches[1]";
$sStatus = ERROR;
} else {
$sMsg = "$sContent";
$sStatus = STOPPED;
}
} else {
$sStatus = RUNNING;
}
} else {
$sMsg = "missing $sLogFile";
$sStatus = ERROR;
}

http_response_code(200);
$oP = new JsonPage();
$oP->add_header('Access-Control-Allow-Origin: *');
$oP->SetData(["status" => $sStatus, 'message' => $sMsg]);
$oP->SetOutputDataOnly(true);
$oP->Output();
}
catch (Exception $e) {
\IssueLog::Error("Cannot cron status", null, ['msg' => $e->getMessage(), 'stack' => $e->getTraceAsString()]);
http_response_code(500);
$oP = new JsonPage();
$oP->add_header('Access-Control-Allow-Origin: *');
$oP->SetData(["message" => $e->getMessage()]);
$oP->SetOutputDataOnly(true);
$oP->Output();
}

function ReadParam($sParam, $sDefaultValue = null, $sSanitizationFilter = utils::ENUM_SANITIZATION_FILTER_RAW_DATA)
{
$sValue = utils::ReadParam($sParam, null, true, $sSanitizationFilter);
if (is_null($sValue)) {
$sValue = utils::ReadPostedParam($sParam, $sDefaultValue, $sSanitizationFilter);
}

return trim($sValue);
}
100 changes: 100 additions & 0 deletions webservices/launch_cron_asynchronously.php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this better be implemented as an option/argument on the current cron.php instead of yet another endpoint?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will take your point when team will discuss this enhancement

Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

require_once(__DIR__.'/../approot.inc.php');
require_once(APPROOT.'/application/application.inc.php');
require_once(APPROOT.'/application/startup.inc.php');

try {
$sAuthUser = ReadParam("auth_user");
$sAuthPwd = ReadParam("auth_pwd");

if (is_null($sAuthUser) || is_null($sAuthPwd)) {
throw new \Exception("Missing credentials");
}

if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd)) {
UserRights::Login($sAuthUser); // Login & set the user's language
} else {
throw new \Exception("Invalid credentials");
}

$sLogFilename = ReadParam("cron_log_file", "cron.log");
$sLogFile = APPROOT."log/$sLogFilename";

$sCliParams = ReadParam("cron_cli_parameters");

$bAsynchronous = true;
if (is_null($sCliParams)) {
$sCliParams = "--help";
$bAsynchronous = false;
} else {
$sCliParams = trim(base64_decode($sCliParams, true));

if (false === strpos($sCliParams, '--auth_user=')) {
$sCliParams = "--auth_user=$sAuthUser ".$sCliParams;
}

if (false === strpos($sCliParams, '--auth_pwd=')) {
$sCliParams = "--auth_pwd=$sAuthPwd ".$sCliParams;
}

if (false !== strpos($sCliParams, '--status_only=1')) {
$bAsynchronous = false;
}
}

touch($sLogFile);
$sPHPExec = trim(\MetaModel::GetConfig()->Get('php_path'));

if ($bAsynchronous) {
$sCli = sprintf("$sPHPExec %s/cron.php $sCliParams 2>&1 >>$sLogFile &", __DIR__);
file_put_contents($sLogFile, $sCli);
$process = popen($sCli, 'r');
} else {


$sCli = sprintf("\n $sPHPExec %s/cron.php $sCliParams", __DIR__);
$fp = fopen($sLogFile, 'a+');
fwrite($fp, $sCli);

$aDescriptorSpec = [
0 => ["pipe", "r"], // stdin
1 => ["pipe", "w"], // stdout
];
$rProcess = proc_open($sCli, $aDescriptorSpec, $aPipes, __DIR__, null);

$sStdOut = stream_get_contents($aPipes[1]);
fclose($aPipes[1]);
$iCode = proc_close($rProcess);

fwrite($fp, $sStdOut);
fwrite($fp, "Exiting: ".time().' ('.date('Y-m-d H:i:s').')');
fclose($fp);
}

http_response_code(200);
$oP = new JsonPage();
$oP->add_header('Access-Control-Allow-Origin: *');
$oP->SetData(["message" => "OK"]);
$oP->SetOutputDataOnly(true);
$oP->Output();
}
catch (Exception $e) {
\IssueLog::Error("Cannot run cron", null, ['msg' => $e->getMessage(), 'stack' => $e->getTraceAsString()]);
http_response_code(500);
$oP = new JsonPage();
$oP->add_header('Access-Control-Allow-Origin: *');
$oP->SetData(["message" => $e->getMessage()]);
$oP->SetOutputDataOnly(true);
$oP->Output();
}

function ReadParam($sParam, $sDefaultValue = null, $sSanitizationFilter = utils::ENUM_SANITIZATION_FILTER_RAW_DATA)
{
$sValue = utils::ReadParam($sParam, null, true, $sSanitizationFilter);
if (is_null($sValue)) {
$sValue = utils::ReadPostedParam($sParam, $sDefaultValue, $sSanitizationFilter);
}

return trim($sValue);
}