Autoscale: use php8.5, support a-z then aa/ab/etc
This commit is contained in:
1 parent
8d1afbdfd4
commit
4ac6d60d02
2 files changed
+22
-12
No files matched your search
@@ -3,4 +3,4 @@ Description=Auto-scale Tor hosting instances based on account count
|
|||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=oneshot
|
Type=oneshot
|
||||||
ExecStart=/usr/bin/php8.2 /var/www/autoscale.php
|
ExecStart=/usr/bin/php8.5 /var/www/autoscale.php
|
||||||
+21
-11
@@ -3,8 +3,23 @@ require(__DIR__ . '/common.php');
|
|||||||
$db = get_db_instance();
|
$db = get_db_instance();
|
||||||
|
|
||||||
$accounts_per_instance = 250;
|
$accounts_per_instance = 250;
|
||||||
$max_instances = 8;
|
|
||||||
$instance_chars = 'abcdefgh';
|
function next_instance_id(array $existing): string {
|
||||||
|
// a-z, then aa, ab, ... az, ba, ...
|
||||||
|
if(empty($existing)){
|
||||||
|
return 'a';
|
||||||
|
}
|
||||||
|
$last = end($existing);
|
||||||
|
$len = strlen($last);
|
||||||
|
for($i = $len - 1; $i >= 0; $i--){
|
||||||
|
if($last[$i] !== 'z'){
|
||||||
|
$last[$i] = chr(ord($last[$i]) + 1);
|
||||||
|
return $last;
|
||||||
|
}
|
||||||
|
$last[$i] = 'a';
|
||||||
|
}
|
||||||
|
return str_repeat('a', $len + 1);
|
||||||
|
}
|
||||||
|
|
||||||
$stmt = $db->query('SELECT instance, COUNT(*) as cnt FROM onions WHERE enabled IN (1, -2) GROUP BY instance;');
|
$stmt = $db->query('SELECT instance, COUNT(*) as cnt FROM onions WHERE enabled IN (1, -2) GROUP BY instance;');
|
||||||
$loads = [];
|
$loads = [];
|
||||||
@@ -15,19 +30,15 @@ while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
|
|||||||
$current = SERVICE_INSTANCES;
|
$current = SERVICE_INSTANCES;
|
||||||
$total_accounts = array_sum($loads);
|
$total_accounts = array_sum($loads);
|
||||||
$needed = max(1, (int)ceil($total_accounts / $accounts_per_instance));
|
$needed = max(1, (int)ceil($total_accounts / $accounts_per_instance));
|
||||||
$needed = min($needed, $max_instances);
|
|
||||||
|
|
||||||
if($needed <= count($current)){
|
if($needed <= count($current)){
|
||||||
echo "OK: $total_accounts accounts across " . count($current) . " instance(s), no scaling needed\n";
|
echo "OK: $total_accounts accounts across " . count($current) . " instance(s)\n";
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
$new_instances = $current;
|
$new_instances = $current;
|
||||||
for($i = count($current); $i < $needed; $i++){
|
while(count($new_instances) < $needed){
|
||||||
$char = $instance_chars[$i];
|
$new_instances[] = next_instance_id($new_instances);
|
||||||
if(!in_array($char, $new_instances)){
|
|
||||||
$new_instances[] = $char;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$old_str = "const SERVICE_INSTANCES=['" . implode("','", $current) . "']";
|
$old_str = "const SERVICE_INSTANCES=['" . implode("','", $current) . "']";
|
||||||
@@ -40,7 +51,6 @@ if(strpos($common, $old_str) === false){
|
|||||||
}
|
}
|
||||||
|
|
||||||
file_put_contents('/var/www/common.php', str_replace($old_str, $new_str, $common));
|
file_put_contents('/var/www/common.php', str_replace($old_str, $new_str, $common));
|
||||||
echo "SCALED: $total_accounts accounts, " . count($current) . " -> " . count($new_instances) . " instances ($new_str)\n";
|
echo "SCALED: $total_accounts accounts, " . count($current) . " -> " . count($new_instances) . " instances\n";
|
||||||
|
|
||||||
echo "Running setup.php to create new instances...\n";
|
|
||||||
require(__DIR__ . '/setup.php');
|
require(__DIR__ . '/setup.php');
|
||||||
Reference in new issue
Block a user