Skip to content

Commit 80ad873

Browse files
committed
Move check to settings model
1 parent 11dcbdc commit 80ad873

File tree

2 files changed

+60
-57
lines changed

2 files changed

+60
-57
lines changed

ProcessMaker/Models/Setting.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Carbon\Carbon;
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Support\Facades\DB;
8+
use Illuminate\Support\Facades\Redis;
9+
use Illuminate\Support\Facades\Schema;
810
use Illuminate\Validation\Rule;
911
use Log;
1012
use ProcessMaker\Cache\Settings\SettingCacheFactory;
@@ -73,6 +75,12 @@ class Setting extends ProcessMakerModel implements HasMedia, PrometheusMetricInt
7375

7476
public const SESSION_CONTROL_GROUP = 'Session Control';
7577

78+
private static bool $redisAvailable = false;
79+
80+
private static bool $settingsTableExists = false;
81+
82+
private static bool $readyToUseSettingsDatabase = false;
83+
7684
/**
7785
* The attributes that aren't mass assignable.
7886
*
@@ -150,6 +158,10 @@ public static function messages()
150158
*/
151159
public static function byKey(string $key)
152160
{
161+
if (!self::readyToUseSettingsDatabase()) {
162+
return null;
163+
}
164+
153165
$settingCache = SettingCacheFactory::getSettingsCache();
154166
$settingKey = $settingCache->createKey([
155167
'key' => $key,
@@ -520,4 +532,51 @@ public function getPrometheusMetricLabel(): string
520532
{
521533
return 'settings.' . $this->key;
522534
}
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+
}
523582
}

ProcessMaker/Repositories/SettingsConfigRepository.php

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,10 @@
44

55
use Illuminate\Config\Repository;
66
use Illuminate\Support\Arr;
7-
use Illuminate\Support\Facades\DB;
8-
use Illuminate\Support\Facades\Redis;
9-
use Illuminate\Support\Facades\Schema;
107
use ProcessMaker\Models\Setting;
118

129
class SettingsConfigRepository extends Repository
1310
{
14-
private bool $redisAvailable = false;
15-
16-
private bool $settingsTableExists = false;
17-
18-
private bool $readyToUseSettingsDatabase = false;
19-
2011
/**
2112
* Determine if the given configuration value exists.
2213
*
@@ -86,7 +77,7 @@ public function getMany($keys)
8677

8778
private function getFromSettings($key)
8879
{
89-
if (!$this->readyToUseSettingsDatabase()) {
80+
if (!Setting::readyToUseSettingsDatabase()) {
9081
return null;
9182
}
9283

@@ -113,51 +104,4 @@ private function getFromSettings($key)
113104

114105
return null;
115106
}
116-
117-
private function readyToUseSettingsDatabase()
118-
{
119-
if (!$this->readyToUseSettingsDatabase) {
120-
$this->readyToUseSettingsDatabase =
121-
app('tenant-resolved') &&
122-
$this->databaseAvailable() &&
123-
$this->redisAvailable() &&
124-
$this->settingsTableExists();
125-
}
126-
127-
return $this->readyToUseSettingsDatabase;
128-
}
129-
130-
private function databaseAvailable()
131-
{
132-
try {
133-
DB::connection()->getPdo();
134-
135-
return true;
136-
} catch (\PDOException $e) {
137-
return false;
138-
}
139-
}
140-
141-
private function redisAvailable()
142-
{
143-
if (!$this->redisAvailable) {
144-
try {
145-
Redis::connection()->ping();
146-
$this->redisAvailable = true;
147-
} catch (\Exception $e) {
148-
$this->redisAvailable = false;
149-
}
150-
}
151-
152-
return $this->redisAvailable;
153-
}
154-
155-
private function settingsTableExists()
156-
{
157-
if (!$this->settingsTableExists) {
158-
$this->settingsTableExists = Schema::hasTable('settings');
159-
}
160-
161-
return $this->settingsTableExists;
162-
}
163107
}

0 commit comments

Comments
 (0)