|
5 | 5 | use Carbon\Carbon; |
6 | 6 | use Illuminate\Database\Eloquent\Model; |
7 | 7 | use Illuminate\Support\Facades\DB; |
| 8 | +use Illuminate\Support\Facades\Redis; |
| 9 | +use Illuminate\Support\Facades\Schema; |
8 | 10 | use Illuminate\Validation\Rule; |
9 | 11 | use Log; |
10 | 12 | use ProcessMaker\Cache\Settings\SettingCacheFactory; |
@@ -73,6 +75,12 @@ class Setting extends ProcessMakerModel implements HasMedia, PrometheusMetricInt |
73 | 75 |
|
74 | 76 | public const SESSION_CONTROL_GROUP = 'Session Control'; |
75 | 77 |
|
| 78 | + private static bool $redisAvailable = false; |
| 79 | + |
| 80 | + private static bool $settingsTableExists = false; |
| 81 | + |
| 82 | + private static bool $readyToUseSettingsDatabase = false; |
| 83 | + |
76 | 84 | /** |
77 | 85 | * The attributes that aren't mass assignable. |
78 | 86 | * |
@@ -150,6 +158,10 @@ public static function messages() |
150 | 158 | */ |
151 | 159 | public static function byKey(string $key) |
152 | 160 | { |
| 161 | + if (!self::readyToUseSettingsDatabase()) { |
| 162 | + return null; |
| 163 | + } |
| 164 | + |
153 | 165 | $settingCache = SettingCacheFactory::getSettingsCache(); |
154 | 166 | $settingKey = $settingCache->createKey([ |
155 | 167 | 'key' => $key, |
@@ -520,4 +532,51 @@ public function getPrometheusMetricLabel(): string |
520 | 532 | { |
521 | 533 | return 'settings.' . $this->key; |
522 | 534 | } |
| 535 | + |
| 536 | + public static function readyToUseSettingsDatabase() |
| 537 | + { |
| 538 | + if (!self::$readyToUseSettingsDatabase) { |
| 539 | + self::$readyToUseSettingsDatabase = |
| 540 | + app('tenant-resolved') && |
| 541 | + self::databaseAvailable() && |
| 542 | + self::redisAvailable() && |
| 543 | + self::settingsTableExists(); |
| 544 | + } |
| 545 | + |
| 546 | + return self::$readyToUseSettingsDatabase; |
| 547 | + } |
| 548 | + |
| 549 | + private static function databaseAvailable() |
| 550 | + { |
| 551 | + try { |
| 552 | + DB::connection()->getPdo(); |
| 553 | + |
| 554 | + return true; |
| 555 | + } catch (\PDOException $e) { |
| 556 | + return false; |
| 557 | + } |
| 558 | + } |
| 559 | + |
| 560 | + private static function redisAvailable() |
| 561 | + { |
| 562 | + if (!self::$redisAvailable) { |
| 563 | + try { |
| 564 | + Redis::connection()->ping(); |
| 565 | + self::$redisAvailable = true; |
| 566 | + } catch (\Exception $e) { |
| 567 | + self::$redisAvailable = false; |
| 568 | + } |
| 569 | + } |
| 570 | + |
| 571 | + return self::$redisAvailable; |
| 572 | + } |
| 573 | + |
| 574 | + private static function settingsTableExists() |
| 575 | + { |
| 576 | + if (!self::$settingsTableExists) { |
| 577 | + self::$settingsTableExists = Schema::hasTable('settings'); |
| 578 | + } |
| 579 | + |
| 580 | + return self::$settingsTableExists; |
| 581 | + } |
523 | 582 | } |
0 commit comments