向下支持 PHP 7.4
This commit is contained in:
parent
50763da6dc
commit
92aa25cea1
@ -43,7 +43,7 @@ $nginx_file_md5 = '';
|
||||
$prefix = $redis_info['prefix'];
|
||||
|
||||
// 检查是否存在配置文件
|
||||
if (!file_exists($nginx_conf_path)) {
|
||||
if (! file_exists($nginx_conf_path)) {
|
||||
echo 'nginx.conf not found';
|
||||
exit;
|
||||
} else {
|
||||
@ -68,11 +68,11 @@ try {
|
||||
// set db 0
|
||||
$redis->select(0);
|
||||
} catch (RedisException $e) {
|
||||
echo 'Connection to Redis failed: ' . $e->getMessage();
|
||||
echo 'Connection to Redis failed: '.$e->getMessage();
|
||||
}
|
||||
|
||||
try {
|
||||
if ($redis->ping()) {
|
||||
if ($redis->ping('')) {
|
||||
output('redis is ok');
|
||||
}
|
||||
} catch (RedisException $e) {
|
||||
@ -114,7 +114,7 @@ while (true) {
|
||||
|
||||
redis_hset('nodes', $node_id, [
|
||||
'type' => 'edge',
|
||||
'id' => $node_id,
|
||||
'id' => 'edge-'.$node_id,
|
||||
'ip' => $ip,
|
||||
// utc +8 timestamp
|
||||
'last_heartbeat' => time(),
|
||||
@ -128,7 +128,7 @@ while (true) {
|
||||
}
|
||||
|
||||
// 重新载入 nginx.conf
|
||||
if (!file_exists($nginx_conf_path)) {
|
||||
if (! file_exists($nginx_conf_path)) {
|
||||
echo 'nginx.conf not found';
|
||||
|
||||
redis_publish('edge.error', [
|
||||
@ -143,7 +143,7 @@ while (true) {
|
||||
|
||||
// 检查 laecloud_database_cluster:nodes 是否存在
|
||||
try {
|
||||
if (!$redis->exists($prefix . 'cluster:nodes')) {
|
||||
if (! $redis->exists($prefix.'cluster:nodes')) {
|
||||
output('cluster:nodes not found');
|
||||
exit;
|
||||
}
|
||||
@ -152,8 +152,8 @@ while (true) {
|
||||
exit;
|
||||
}
|
||||
|
||||
$nodes = redis_hgetAll('nodes', []);
|
||||
if (!$nodes) {
|
||||
$nodes = redis_hgetAll('nodes');
|
||||
if (! $nodes) {
|
||||
output('nodes is empty');
|
||||
|
||||
continue;
|
||||
@ -171,21 +171,19 @@ while (true) {
|
||||
|
||||
foreach ($nodes as $node) {
|
||||
// only allow node type master, slave
|
||||
if (!in_array($node['type'], ['master', 'slave'])) {
|
||||
if (! in_array($node['type'], ['master', 'slave'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
output('node_type: ' . $node['type']);
|
||||
output('node_id: ' . $node['id']);
|
||||
output('node_type: '.$node['type']);
|
||||
output('node_id: '.$node['id']);
|
||||
output('================================================================');
|
||||
|
||||
$temp_conf = "#node {$node['type']}:{$node['id']}\n";
|
||||
$temp_conf .= "server {$node['ip']}";
|
||||
|
||||
if (isset($node['weight'])) {
|
||||
if ($node['weight'] === "0") {
|
||||
continue;
|
||||
} else {
|
||||
if ($node['weight'] !== '0') {
|
||||
$temp_conf .= " weight={$node['weight']} ";
|
||||
}
|
||||
} else {
|
||||
@ -200,11 +198,11 @@ while (true) {
|
||||
|
||||
$temp_conf .= ";\n";
|
||||
|
||||
echo $temp_conf . PHP_EOL;
|
||||
echo $temp_conf.PHP_EOL;
|
||||
|
||||
output('================================================================');
|
||||
|
||||
$conf .= $temp_conf . PHP_EOL;
|
||||
$conf .= $temp_conf.PHP_EOL;
|
||||
}
|
||||
|
||||
output('!!!!!!!!!!!!!!!!!');
|
||||
@ -212,13 +210,13 @@ while (true) {
|
||||
output('!!!!!!!!!!!!!!!!!');
|
||||
|
||||
$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!';
|
||||
EOF;
|
||||
|
||||
// 放入配置文件,以 ##########CLUSTERREADY######### 开始,以 ##########END_CLUSTERREADY######### 结束
|
||||
$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#########.*##########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);
|
||||
|
||||
if (md5($nginx_conf) != $nginx_file_md5) {
|
||||
file_put_contents($nginx_conf_path, $nginx_conf);
|
||||
@ -240,17 +238,17 @@ EOF;
|
||||
function output($context): void
|
||||
{
|
||||
$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;
|
||||
|
||||
try {
|
||||
$value = $redis->get($prefix . 'cluster:' . $key);
|
||||
$value = $redis->get($prefix.'cluster:'.$key);
|
||||
} catch (RedisException $e) {
|
||||
exit('redis get error: ' . $e->getMessage());
|
||||
exit('redis get error: '.$e->getMessage());
|
||||
}
|
||||
|
||||
if (empty($value)) {
|
||||
@ -260,14 +258,14 @@ function redis_get($key, $default = null): mixed
|
||||
return $value;
|
||||
}
|
||||
|
||||
function redis_hget($key, $hash_key, $default = null): mixed
|
||||
function redis_hget($key, $hash_key, $default = null)
|
||||
{
|
||||
global $redis, $prefix;
|
||||
|
||||
try {
|
||||
$value = $redis->hget($prefix . 'cluster:' . $key, $hash_key);
|
||||
$value = $redis->hget($prefix.'cluster:'.$key, $hash_key);
|
||||
} catch (RedisException $e) {
|
||||
exit('redis get error: ' . $e->getMessage());
|
||||
exit('redis get error: '.$e->getMessage());
|
||||
}
|
||||
|
||||
if (empty($value)) {
|
||||
@ -277,14 +275,14 @@ function redis_hget($key, $hash_key, $default = null): mixed
|
||||
return $value;
|
||||
}
|
||||
|
||||
function redis_hgetAll($key): Redis|array
|
||||
function redis_hgetAll($key)
|
||||
{
|
||||
global $redis, $prefix;
|
||||
|
||||
try {
|
||||
$value = $redis->hGetAll($prefix . 'cluster:' . $key);
|
||||
$value = $redis->hGetAll($prefix.'cluster:'.$key);
|
||||
} catch (RedisException $e) {
|
||||
exit('redis get error: ' . $e->getMessage());
|
||||
exit('redis get error: '.$e->getMessage());
|
||||
}
|
||||
|
||||
// json_decode all
|
||||
@ -300,9 +298,9 @@ function redis_hset($key, $hash_key, $value)
|
||||
global $redis, $prefix;
|
||||
|
||||
try {
|
||||
$redis->hset($prefix . 'cluster:' . $key, $hash_key, json_encode($value));
|
||||
$redis->hset($prefix.'cluster:'.$key, $hash_key, json_encode($value));
|
||||
} 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;
|
||||
|
||||
try {
|
||||
$redis->hdel($prefix . 'cluster:' . $key, $hash_key);
|
||||
$redis->hdel($prefix.'cluster:'.$key, $hash_key);
|
||||
} 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);
|
||||
|
||||
try {
|
||||
$redis->publish($prefix . 'cluster_ready', $data);
|
||||
$redis->publish($prefix.'cluster_ready', $data);
|
||||
} catch (RedisException $e) {
|
||||
exit('redis publish error: ' . $e->getMessage());
|
||||
exit('redis publish error: '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user