Add command to debug cawangan connections

This commit is contained in:
Afiq Hamzah
2023-12-14 19:57:28 +08:00
parent b6f093fa2d
commit 3194cedb72
3 changed files with 175 additions and 17 deletions
+69 -16
View File
@@ -2,6 +2,7 @@
namespace App;
use DateTime;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
class Helper
@@ -32,29 +33,81 @@ class Helper
public static function getActiveCawanganDbConnection()
{
$queryActiveCawanganDbConnection = function () {
$all_active_db_keys = array_filter(array_keys(config('database.connections')), function ($connection) {
try {
DB::connection($connection)->getPdo();
return true;
} catch (\Throwable $th) {
return false;
}
});
$non_cawangan_database = ['lelong', 'online_arrahn', 'kaunterpkb'];
$all_cawangan_db = array_filter($all_active_db_keys, function ($db_key) use ($non_cawangan_database) {
$isNotCawangan = in_array(config('database.connections.' . $db_key . '.database'), $non_cawangan_database);
if ($isNotCawangan) {
return false;
}
$all_active_db_keys = array_filter(array_keys(config('database.connections')), function ($connection) {
try {
DB::connection($connection)->getPdo();
return true;
} catch (\Throwable $th) {
return false;
}
});
});
$non_cawangan_database = ['lelong', 'online_arrahn', 'kaunterpkb'];
return $all_cawangan_db;
$all_cawangan_db = array_filter($all_active_db_keys, function ($db_key) use ($non_cawangan_database) {
$isNotCawangan = in_array(config('database.connections.' . $db_key . '.database'), $non_cawangan_database);
};
if ($isNotCawangan) {
return false;
}
$all_cawangan_db_connection = Cache::store('file')->get('active_cawangan_db_connection');
return true;
});
if ($all_cawangan_db_connection === null or count($all_cawangan_db_connection) === 0) {
$all_cawangan_db_connection = $queryActiveCawanganDbConnection();
Cache::store('file')->put('active_cawangan_db_connection', $all_cawangan_db_connection, 3600);
}
return $all_cawangan_db;
return $all_cawangan_db_connection;
}
public static function getInactiveCawanganDbConnection()
{
$queryInactiveCawanganDbConnection = function () {
$all_inactive_db_keys = array_filter(array_keys(config('database.connections')), function ($connection) {
try {
DB::connection($connection)->getPdo();
return false;
} catch (\Throwable $th) {
return true;
}
});
$non_cawangan_database = ['lelong', 'online_arrahn', 'kaunterpkb'];
$all_cawangan_db = array_filter($all_inactive_db_keys, function ($db_key) use ($non_cawangan_database) {
$isNotCawangan = in_array(config('database.connections.' . $db_key . '.database'), $non_cawangan_database);
$isNotCawanganKey = in_array($db_key, $non_cawangan_database);
if ($isNotCawangan OR $isNotCawanganKey) {
return false;
}
return true;
});
return $all_cawangan_db;
};
$all_cawangan_db_connection = array_map(function ($db_connection) {
return [
'connection' => $db_connection,
'database' => config('database.connections.' . $db_connection . '.database'),
];
}, $queryInactiveCawanganDbConnection());
return $all_cawangan_db_connection;
}