向下支持 PHP 7.4

This commit is contained in:
iVampireSP.com 2023-02-10 12:14:18 +08:00
parent 50763da6dc
commit 92aa25cea1
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132

View File

@ -43,7 +43,7 @@ $nginx_file_md5 = '';
$prefix = $redis_info['prefix']; $prefix = $redis_info['prefix'];
// 检查是否存在配置文件 // 检查是否存在配置文件
if (!file_exists($nginx_conf_path)) { if (! file_exists($nginx_conf_path)) {
echo 'nginx.conf not found'; echo 'nginx.conf not found';
exit; exit;
} else { } else {
@ -68,11 +68,11 @@ try {
// set db 0 // set db 0
$redis->select(0); $redis->select(0);
} catch (RedisException $e) { } catch (RedisException $e) {
echo 'Connection to Redis failed: ' . $e->getMessage(); echo 'Connection to Redis failed: '.$e->getMessage();
} }
try { try {
if ($redis->ping()) { if ($redis->ping('')) {
output('redis is ok'); output('redis is ok');
} }
} catch (RedisException $e) { } catch (RedisException $e) {
@ -114,7 +114,7 @@ while (true) {
redis_hset('nodes', $node_id, [ redis_hset('nodes', $node_id, [
'type' => 'edge', 'type' => 'edge',
'id' => $node_id, 'id' => 'edge-'.$node_id,
'ip' => $ip, 'ip' => $ip,
// utc +8 timestamp // utc +8 timestamp
'last_heartbeat' => time(), 'last_heartbeat' => time(),
@ -128,7 +128,7 @@ while (true) {
} }
// 重新载入 nginx.conf // 重新载入 nginx.conf
if (!file_exists($nginx_conf_path)) { if (! file_exists($nginx_conf_path)) {
echo 'nginx.conf not found'; echo 'nginx.conf not found';
redis_publish('edge.error', [ redis_publish('edge.error', [
@ -143,7 +143,7 @@ while (true) {
// 检查 laecloud_database_cluster:nodes 是否存在 // 检查 laecloud_database_cluster:nodes 是否存在
try { try {
if (!$redis->exists($prefix . 'cluster:nodes')) { if (! $redis->exists($prefix.'cluster:nodes')) {
output('cluster:nodes not found'); output('cluster:nodes not found');
exit; exit;
} }
@ -152,8 +152,8 @@ while (true) {
exit; exit;
} }
$nodes = redis_hgetAll('nodes', []); $nodes = redis_hgetAll('nodes');
if (!$nodes) { if (! $nodes) {
output('nodes is empty'); output('nodes is empty');
continue; continue;
@ -171,21 +171,19 @@ while (true) {
foreach ($nodes as $node) { foreach ($nodes as $node) {
// only allow node type master, slave // only allow node type master, slave
if (!in_array($node['type'], ['master', 'slave'])) { if (! in_array($node['type'], ['master', 'slave'])) {
continue; continue;
} }
output('node_type: ' . $node['type']); output('node_type: '.$node['type']);
output('node_id: ' . $node['id']); output('node_id: '.$node['id']);
output('================================================================'); output('================================================================');
$temp_conf = "#node {$node['type']}:{$node['id']}\n"; $temp_conf = "#node {$node['type']}:{$node['id']}\n";
$temp_conf .= "server {$node['ip']}"; $temp_conf .= "server {$node['ip']}";
if (isset($node['weight'])) { if (isset($node['weight'])) {
if ($node['weight'] === "0") { if ($node['weight'] !== '0') {
continue;
} else {
$temp_conf .= " weight={$node['weight']} "; $temp_conf .= " weight={$node['weight']} ";
} }
} else { } else {
@ -200,11 +198,11 @@ while (true) {
$temp_conf .= ";\n"; $temp_conf .= ";\n";
echo $temp_conf . PHP_EOL; echo $temp_conf.PHP_EOL;
output('================================================================'); output('================================================================');
$conf .= $temp_conf . PHP_EOL; $conf .= $temp_conf.PHP_EOL;
} }
output('!!!!!!!!!!!!!!!!!'); output('!!!!!!!!!!!!!!!!!');
@ -212,13 +210,13 @@ while (true) {
output('!!!!!!!!!!!!!!!!!'); output('!!!!!!!!!!!!!!!!!');
$add_header = <<<EOF $add_header = <<<EOF
add_header 'Cluster-Ready-Node-Id' '{$node_id}'; add_header 'Cluster-Ready-Node-Id' 'edge-$node_id';
add_header 'Powered-by' 'Cluster Ready!'; add_header 'Powered-by' 'Cluster Ready!';
EOF; EOF;
// 放入配置文件,以 ##########CLUSTERREADY######### 开始,以 ##########END_CLUSTERREADY######### 结束 // 放入配置文件,以 ##########CLUSTERREADY######### 开始,以 ##########END_CLUSTERREADY######### 结束
$nginx_conf = preg_replace('/##########CLUSTERREADY#########.*##########END_CLUSTERREADY#########/s', '##########CLUSTERREADY#########' . PHP_EOL . $conf . '##########END_CLUSTERREADY#########', $nginx_conf); $nginx_conf = preg_replace('/##########CLUSTERREADY#########.*##########END_CLUSTERREADY#########/s', '##########CLUSTERREADY#########'.PHP_EOL.$conf.'##########END_CLUSTERREADY#########', $nginx_conf);
$nginx_conf = preg_replace('/##########CLUSTERREADY_PROXY#########.*##########END_CLUSTERREADY_PROXY#########/s', '##########CLUSTERREADY_PROXY#########' . PHP_EOL . $add_header . PHP_EOL . '##########END_CLUSTERREADY_PROXY#########', $nginx_conf); $nginx_conf = preg_replace('/##########CLUSTERREADY_PROXY#########.*##########END_CLUSTERREADY_PROXY#########/s', '##########CLUSTERREADY_PROXY#########'.PHP_EOL.$add_header.PHP_EOL.'##########END_CLUSTERREADY_PROXY#########', $nginx_conf);
if (md5($nginx_conf) != $nginx_file_md5) { if (md5($nginx_conf) != $nginx_file_md5) {
file_put_contents($nginx_conf_path, $nginx_conf); file_put_contents($nginx_conf_path, $nginx_conf);
@ -240,17 +238,17 @@ EOF;
function output($context): void function output($context): void
{ {
$time_string = date('Y-m-d H:i:s'); $time_string = date('Y-m-d H:i:s');
echo $time_string . ': ' . $context . PHP_EOL; echo $time_string.': '.$context.PHP_EOL;
} }
function redis_get($key, $default = null): mixed function redis_get($key, $default = null)
{ {
global $redis, $prefix; global $redis, $prefix;
try { try {
$value = $redis->get($prefix . 'cluster:' . $key); $value = $redis->get($prefix.'cluster:'.$key);
} catch (RedisException $e) { } catch (RedisException $e) {
exit('redis get error: ' . $e->getMessage()); exit('redis get error: '.$e->getMessage());
} }
if (empty($value)) { if (empty($value)) {
@ -260,14 +258,14 @@ function redis_get($key, $default = null): mixed
return $value; return $value;
} }
function redis_hget($key, $hash_key, $default = null): mixed function redis_hget($key, $hash_key, $default = null)
{ {
global $redis, $prefix; global $redis, $prefix;
try { try {
$value = $redis->hget($prefix . 'cluster:' . $key, $hash_key); $value = $redis->hget($prefix.'cluster:'.$key, $hash_key);
} catch (RedisException $e) { } catch (RedisException $e) {
exit('redis get error: ' . $e->getMessage()); exit('redis get error: '.$e->getMessage());
} }
if (empty($value)) { if (empty($value)) {
@ -277,14 +275,14 @@ function redis_hget($key, $hash_key, $default = null): mixed
return $value; return $value;
} }
function redis_hgetAll($key): Redis|array function redis_hgetAll($key)
{ {
global $redis, $prefix; global $redis, $prefix;
try { try {
$value = $redis->hGetAll($prefix . 'cluster:' . $key); $value = $redis->hGetAll($prefix.'cluster:'.$key);
} catch (RedisException $e) { } catch (RedisException $e) {
exit('redis get error: ' . $e->getMessage()); exit('redis get error: '.$e->getMessage());
} }
// json_decode all // json_decode all
@ -300,9 +298,9 @@ function redis_hset($key, $hash_key, $value)
global $redis, $prefix; global $redis, $prefix;
try { try {
$redis->hset($prefix . 'cluster:' . $key, $hash_key, json_encode($value)); $redis->hset($prefix.'cluster:'.$key, $hash_key, json_encode($value));
} catch (RedisException $e) { } catch (RedisException $e) {
exit('redis get error: ' . $e->getMessage()); exit('redis get error: '.$e->getMessage());
} }
} }
@ -311,9 +309,9 @@ function redis_hdel($key, $hash_key): void
global $redis, $prefix; global $redis, $prefix;
try { try {
$redis->hdel($prefix . 'cluster:' . $key, $hash_key); $redis->hdel($prefix.'cluster:'.$key, $hash_key);
} catch (RedisException $e) { } catch (RedisException $e) {
exit('redis get error: ' . $e->getMessage()); exit('redis get error: '.$e->getMessage());
} }
} }
@ -333,8 +331,8 @@ function redis_publish($event, $message = []): void
$data = json_encode($data); $data = json_encode($data);
try { try {
$redis->publish($prefix . 'cluster_ready', $data); $redis->publish($prefix.'cluster_ready', $data);
} catch (RedisException $e) { } catch (RedisException $e) {
exit('redis publish error: ' . $e->getMessage()); exit('redis publish error: '.$e->getMessage());
} }
} }