Skip to content

Commit

Permalink
mysqli ping to keep connection open
Browse files Browse the repository at this point in the history
  • Loading branch information
glynhudson committed Dec 21, 2017
1 parent d17dc0a commit c21302a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Modules/input/input_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function create_input($userid, $nodeid, $name)
$this->redis->sAdd("user:inputs:$userid", $id);
$this->redis->hMSet("input:$id",array('id'=>$id,'nodeid'=>$nodeid,'name'=>$name,'description'=>"", 'processList'=>""));
}
} else {
$this->log->warn("create_input mysql error");
}
return $id;
}
Expand Down
23 changes: 17 additions & 6 deletions scripts/phpmqtt_input.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@
die;
}

$mysqli = @new mysqli($server,$username,$password,$database,$port);
$mysqli = new mysqli($server,$username,$password,$database,$port);
if ($mysqli->connect_error) { $log->error("Cannot connect to MYSQL database:". $mysqli->connect_error); die('Check log\n'); }

// Enable for testing
// $mysqli->query("SET interactive_timeout=60;");
// $mysqli->query("SET wait_timeout=60;");

if ($redis_enabled) {
$redis = new Redis();
if (!$redis->connect($redis_server['host'], $redis_server['port'])) {
Expand Down Expand Up @@ -98,7 +102,7 @@

$connected = false;
$last_retry = 0;
$last_heartbeat = 0;
$last_heartbeat = time();
$count = 0;

$mqtt_client->onConnect('connect');
Expand All @@ -111,7 +115,7 @@
try {
$mqtt_client->loop();
} catch (Exception $e) {
$log->error($e);
if ($connected) $log->error($e);
}

if (!$connected && (time()-$last_retry)>5.0) {
Expand All @@ -134,6 +138,12 @@
$last_heartbeat = time();
$log->info("$count Messages processed in last 5 minutes");
$count = 0;

// Keep mysql connection open with periodic ping
if (!$mysqli->ping()) {
$log->warn("mysql ping false");
die;
}
}

usleep(1000);
Expand All @@ -144,7 +154,7 @@ function connect($r, $message) {
global $log, $connected;
$connected = true;
//echo "Connected to MQTT server with code {$r} and message {$message}\n";
$log->info("Connecting to MQTT server: {$message}: code: {$r}");
$log->warn("Connecting to MQTT server: {$message}: code: {$r}");
}

function subscribe() {
Expand Down Expand Up @@ -252,9 +262,10 @@ function message($message)
else
{
if (!isset($dbinputs[$nodeid][$name])) {
usleep(100);
$inputid = $input->create_input($userid, $nodeid, $name);
usleep(100);
if (!$inputid) {
$log->warn("error creating input"); die;
}
$dbinputs[$nodeid][$name] = true;
$dbinputs[$nodeid][$name] = array('id'=>$inputid);
$input->set_timevalue($dbinputs[$nodeid][$name]['id'],$time,$value);
Expand Down

1 comment on commit c21302a

@glynhudson
Copy link
Member Author

@glynhudson glynhudson commented on c21302a Dec 21, 2017

Choose a reason for hiding this comment

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

Fix for #752 #541. This commit was made by @TrystanLea using my username my mistake!

Please sign in to comment.