diff --git a/emhttp/plugins/dynamix/include/publish.php b/emhttp/plugins/dynamix/include/publish.php index 66d42ac9e..89f79b545 100644 --- a/emhttp/plugins/dynamix/include/publish.php +++ b/emhttp/plugins/dynamix/include/publish.php @@ -25,16 +25,34 @@ function curl_socket($socket, $url, $message='') { } function publish($endpoint, $message, $len=1) { + if ( is_file("/tmp/nginxStopped") ) + return false; + $com = curl_init("http://localhost/pub/$endpoint?buffer_length=$len"); curl_setopt_array($com,[ CURLOPT_UNIX_SOCKET_PATH => "/var/run/nginx.socket", CURLOPT_HTTPHEADER => ['Accept:text/json'], CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $message, - CURLOPT_RETURNTRANSFER => 1 + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_FAILONERROR => true ]); $reply = curl_exec($com); + $err = curl_error($com); curl_close($com); + if ($err) { + preg_match_all("/[0-9]+/",$err,$matches); + // 500: out of shared memory when creating a channel + // 507: out of shared memory publishing a message + // nginx automatically starts when OS detects its not running + + if ( ($matches[0][0] ?? "") == 507 || ($matches[0][0] ?? "") == 500 ) { + my_logger("Nchan out of shared memory. Restarting nginx"); + // prevent multiple attempts at restarting + touch("/tmp/nginxStopped"); + exec("/etc/rc.d/rc.nginx stop"); + } + } if ($reply===false) my_logger("curl to $endpoint failed", 'publish'); return $reply; }