改进 Edge

This commit is contained in:
iVampireSP.com 2023-02-09 18:59:27 +08:00
parent 876f3dff04
commit bf60034518
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'];
// 检查是否存在配置文件
if (! file_exists($nginx_conf_path)) {
if (!file_exists($nginx_conf_path)) {
echo 'nginx.conf not found';
exit;
} else {
@ -68,7 +68,7 @@ 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 {
@ -114,7 +114,7 @@ while (true) {
redis_hset('nodes', $node_id, [
'type' => 'edge',
'id' => 'edge-'.$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;
}
@ -153,7 +153,7 @@ while (true) {
}
$nodes = redis_hgetAll('nodes', []);
if (! $nodes) {
if (!$nodes) {
output('nodes is empty');
continue;
@ -171,19 +171,23 @@ 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'])) {
$temp_conf .= " weight={$node['weight']} ";
if ($node['weight'] !== "0") {
$temp_conf .= " weight={$node['weight']} ";
}
} else {
$temp_conf .= ' weight=1 ';
}
if ($node['type'] == 'master') {
@ -194,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('!!!!!!!!!!!!!!!!!');
@ -211,8 +215,8 @@ 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);
@ -234,7 +238,7 @@ 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
@ -242,9 +246,9 @@ function redis_get($key, $default = null): mixed
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)) {
@ -259,9 +263,9 @@ function redis_hget($key, $hash_key, $default = null): mixed
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)) {
@ -276,9 +280,9 @@ function redis_hgetAll($key): Redis|array
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
@ -294,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());
}
}
@ -305,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());
}
}
@ -327,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());
}
}