Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
* Cycles threads management update
* Connect cycle auto-restart
  • Loading branch information
sergejey committed Aug 26, 2018
1 parent a303a39 commit 84e684b
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 30 deletions.
71 changes: 52 additions & 19 deletions cycle.php
Expand Up @@ -189,12 +189,33 @@
}
}

// 1 second sleep
sleep(1);
// Removing cycles properties
$qry="1 AND (TITLE LIKE 'cycle%Run' OR TITLE LIKE 'cycle%Control' OR TITLE LIKE 'cycle%Disabled' OR TITLE LIKE 'cycle%AutoRestart')";
$thisCompObject = getObject('ThisComputer');
$cycles_records=SQLSelect("SELECT properties.* FROM properties WHERE $qry ORDER BY TITLE");
$total = count($cycles_records);
for ($i = 0; $i < $total; $i++) {
//DebMes("Removing property ThisComputer.$property",'threads');
$property=$cycles_records[$i]['TITLE'];
$property_id = $thisCompObject->getPropertyByName($property, $thisCompObject->class_id, $thisCompObject->id);
//DebMes("Property id: $property_id",'threads');
if ($property_id) {
$sqlQuery = "SELECT ID
FROM pvalues
WHERE PROPERTY_ID = " . (int)$property_id . "
AND OBJECT_ID = " . (int)$thisCompObject->id;
$pvalue=SQLSelectOne($sqlQuery);
if ($pvalue['ID']) {
//DebMes("Pvalue: ".$pvalue['ID'],'threads');
SQLExec("DELETE FROM phistory WHERE VALUE_ID=".$pvalue['ID']);
SQLExec("DELETE FROM pvalues WHERE ID=".$pvalue['ID']);
SQLExec("DELETE FROM properties WHERE ID=".$property_id);
}
}
}

// getting list of /scripts/cycle_*.php files to run each in separate thread
$cycles = array();

$reboot_timer=0;

if (is_dir("./scripts"))
Expand Down Expand Up @@ -238,8 +259,7 @@
DebMes("Starting " . $path . " ... ",'threads');
echo "Starting " . $path . " ... \n";

if ((preg_match("/_X/", $path)))
{
if ((preg_match("/_X/", $path))) {
if (!IsWindowsOS())
{
$display = '101';
Expand All @@ -252,20 +272,17 @@
}
$pipe_id = $threads->newXThread($path, $display);
}
}
else
{
} else {
$pipe_id = $threads->newThread($path);
}

$pipes[$pipe_id] = $path;

echo "OK" . PHP_EOL;
}
}

echo "ALL CYCLES STARTED" . PHP_EOL;

/*
if (!is_array($restart_threads))
{
$restart_threads = array(
Expand All @@ -281,6 +298,7 @@
if (!defined('DISABLE_WEBSOCKETS') || DISABLE_WEBSOCKETS==0) {
$restart_threads[]='cycle_websockets.php';
}
*/

$last_restart=array();

Expand All @@ -289,6 +307,7 @@
$auto_restarts=array();
$to_start=array();
$to_stop=array();
$started_when=array();


while (false !== ($result = $threads->iteration()))
Expand All @@ -297,11 +316,11 @@
if ((time()-$last_cycles_control_check)>=5) {
$last_cycles_control_check=time();

$auto_restarts=array();
// $auto_restarts=array();
$qry="1 AND (TITLE LIKE 'cycle%Run' OR TITLE LIKE 'cycle%Control')";
$cycles=SQLSelect("SELECT properties.* FROM properties WHERE $qry ORDER BY TITLE");
$total = count($cycles);

$seen=array();
for ($i = 0; $i < $total; $i++) {
$title = $cycles[$i]['TITLE'];
Expand All @@ -312,17 +331,20 @@
}
$seen[$title]=1;
$control=getGlobal($title.'Control');
/*
$auto_restart=getGlobal($title.'AutoRestart');
if ($auto_restart) {
$auto_restarts[]=$title;
}
*/
if ($control!='') {
DebMes("Got control command '$control' for ".$title,'threads');
if ($control=='stop') {
$to_stop[$title]=time();
} elseif ($control=='start') {
/*} elseif ($control=='start') {
$to_start[$title]=time();
} elseif ($control=='restart') {
*/
} elseif ($control=='restart' || $control=='start') {
$to_stop[$title]=time();
$to_start[$title]=time()+5;
}
Expand All @@ -338,6 +360,11 @@
if (preg_match('/(cycle_.+?)\.php/is',$cmd,$m)) {
$title=$m[1];
$is_running[$title]=$id;
if (!isset($started_when[$title])) $started_when[$title]=time();
if ((time()-$started_when[$title])>30 && !in_array($title,$auto_restarts)) {
DebMes("Adding $title to auto-recovery list",'threads');
$auto_restarts[]=$title;
}
}
}

Expand All @@ -359,8 +386,12 @@
foreach($to_stop as $title=>$tm) {
if ($tm<=time()) {
if (isset($is_running[$title])) {
$id =$is_running[$title];
DebMes("Force closing service ".$title." (id: ".$id.")",'threads');
$id = $is_running[$title];
DebMes("Force closing service " . $title . " (id: " . $id . ")", 'threads');
$key = array_search($title, $auto_restarts);
if ($key !== false) {
unset($auto_restarts[$key]);
}
$threads->closeThread($id);
}
unset($to_stop[$title]);
Expand All @@ -374,6 +405,7 @@
DebMes("Starting service ".$title.' ('.$cmd.')','threads');
$pipe_id = $threads->newThread($cmd);
$is_running[$title]=$pipe_id;
$started_when[$title]=time();
}
unset($to_stop[$title]);
unset($to_start[$title]);
Expand All @@ -400,19 +432,20 @@
$need_restart=1;
}
}
/*
foreach ($restart_threads as $item)
{
if (preg_match('/' . $item . '/is', $closed_thread)) {
$need_restart=1;
}
}
*/
if ($need_restart && $cycle_title) {
DebMes("AUTO-RECOVERY: " . $closed_thread,'threads');
if (!preg_match('/websockets/is', $closed_thread)) {
if (!preg_match('/websockets/is', $closed_thread) && !preg_match('/connect/is', $closed_thread)) {
registerError('cycle_stop', $closed_thread."\n".$result);
}
$to_start[$cycle_title]=time()+5;
//$pipe_id = $threads->newThread($closed_thread);
$to_start[$cycle_title]=time()+2;
}
}
}
Expand Down
25 changes: 18 additions & 7 deletions lib/threads.php
Expand Up @@ -26,13 +26,24 @@ class Threads
public function closeThread($id) {
$pstatus = proc_get_status($this->handles[$id]);
$pid = $pstatus['pid'];
stripos(php_uname('s'), 'win')>-1 ? safe_exec("taskkill /F /T /PID $pid") : safe_exec("kill -9 $pid");
//proc_terminate($this->handles[$id]);
/*
fclose($this->pipes[$id][0]);
fclose($this->pipes[$id][1]);
proc_close($this->handles[$id]);
*/
$cmd = $pstatus['command'];
DebMes("Closing thread $pid ($cmd)",'threads');
//if (!proc_terminate($this->handles[$id])) {
//DebMes("Cannot close process $pid ($cmd) with proc_terminate",'threads');
if (IsWindowsOS()) {
$exec_str="taskkill /F /T /PID $pid";
} else {
$exec_str="kill -9 $pid";
}
DebMes("Executing: ".$exec_str,'threads');
$output = array();
$result = exec($exec_str,$output);
DebMes("Result: ".implode("\n",$output),'threads');
/*
} else {
DebMes("Process $pid ($cmd) closed with proc_terminate",'threads');
}
*/
}

/**
Expand Down
6 changes: 6 additions & 0 deletions modules/xray/xray.class.php
Expand Up @@ -368,6 +368,7 @@ function admin(&$out) {
} elseif ($cmd=='restart' && $service!='') {
sg($service.'Run','');
sg($service.'Control','restart');
/*
} elseif ($cmd=='switch_restart' && $service!='') {
if (gg($service.'AutoRestart')) {
sg($service.'AutoRestart',0);
Expand All @@ -380,6 +381,7 @@ function admin(&$out) {
} else {
sg($service.'Disabled',1);
}
*/
}
}
if ($this->view_mode=='timers') {
Expand Down Expand Up @@ -626,8 +628,10 @@ function admin(&$out) {
echo '<td><b>CYCLE</b></td>';
echo '<td><b>LIVE</b></td>';
echo '<td><b>CONTROL</b></td>';
/*
echo '<td><b>DISABLED</b></td>';
echo '<td><b>AUTO-RECOVERY</b></td>';
*/
echo '</tr>';
for($i=0;$i<$total;$i++) {
echo '<tr>';
Expand Down Expand Up @@ -663,6 +667,7 @@ function admin(&$out) {
echo '<a href="'.$url.'&cmd=restart" class="btn btn-default">Restart Now</a>&nbsp;';
echo '</td>';

/*
echo '<td>';
if (getGlobal($title.'Disabled')) {
echo "<font color='red'><b>".LANG_YES."</b></font>";
Expand All @@ -680,6 +685,7 @@ function admin(&$out) {
}
echo '&nbsp;<a href="'.$url.'&cmd=switch_restart" class="btn btn-default">Switch</a>&nbsp;';
echo '</td>';
*/


echo '</tr>';
Expand Down
27 changes: 23 additions & 4 deletions scripts/cycle_connect.php
Expand Up @@ -16,6 +16,9 @@

include_once("./load_settings.php");

$started_time = time();
$max_run_time = 2*60*60; // do restart in 2 hours

set_time_limit(0);
const CONNECT_HOST = 'connect.smartliving.ru';

Expand Down Expand Up @@ -56,7 +59,7 @@
$host = CONNECT_HOST;
$port = 1883;

$query = $username . '/incoming_urls,' . $username . '/menu_session';
$query = $username . '/incoming_urls,' . $username . '/menu_session,'. $username . '/reverse_urls';
$client_name = "MajorDoMo " . $username . " Connect";
$mqtt_client = new phpMQTT($host, $port, $client_name);

Expand Down Expand Up @@ -88,11 +91,17 @@
$menu_sent_time = time();
send_all_menu();
}
if ((time()-$started_time)>$max_run_time) {
echo "Exit cycle CONNET... (reconnecting)";
$mqtt_client->close();
$db->Disconnect();
exit;
}
}
$mqtt_client->close();

} else {
echo "Failed to connect ...\n";
echo date('Y-m-d H:i:s')." Failed to connect ...\n";
sleep(10);
continue;
}
Expand All @@ -119,17 +128,27 @@ function procmsg($topic, $msg)
$url = BASE_URL.$msg;
echo date("Y-m-d H:i:s") . " Incoming url: $url\n";
getURLBackground($url, 0);
} elseif (preg_match('/reverse_urls/is', $topic)) {
echo date("Y-m-d H:i:s") . " Incoming reverse url: $msg\n";
send_reverse_result($msg,$result);
}
echo date("Y-m-d H:i:s") . " Topic:{$topic} $msg\n";
}

function send_menu_element($parent_id) {
echo "Sending menu element $parent_id\n";
echo date('Y-m-d H:i:s')." Sending menu element $parent_id\n";
update_menu_data($parent_id);
}

function send_reverse_result($msg,$result) {
global $connect;
$url = BASE_URL.$msg;
$result = getURL($url, 0);
$connect->sendReverseURL($msg,$result);
}

function send_all_menu() {
echo "Sending full menu\n";
echo date('Y-m-d H:i:s')." Sending full menu\n";
global $connect;
update_menu_data(0);
$connect->sendMenu(1);
Expand Down

0 comments on commit 84e684b

Please sign in to comment.