Spaces:
Running
Running
Update config_provider.py
Browse files- config_provider.py +52 -32
config_provider.py
CHANGED
@@ -151,52 +151,72 @@ class ConfigProvider:
|
|
151 |
with cls._file_lock:
|
152 |
try:
|
153 |
# Load current config for race condition check
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
if
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
|
|
|
|
166 |
|
167 |
# Update metadata
|
168 |
config.last_update_date = get_current_timestamp()
|
169 |
config.last_update_user = username
|
170 |
|
171 |
# Convert to JSON - Pydantic v2 kullanımı
|
172 |
-
data = config.model_dump(mode='json')
|
173 |
json_str = json.dumps(data, ensure_ascii=False, indent=2)
|
174 |
|
175 |
-
#
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
temp_path =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
|
187 |
-
#
|
188 |
-
import shutil
|
189 |
-
shutil.move(str(temp_path), str(cls._CONFIG_PATH))
|
190 |
-
|
191 |
-
# Update cache
|
192 |
with cls._lock:
|
193 |
cls._instance = config
|
194 |
-
cls._instance.build_index()
|
195 |
|
196 |
log_info(
|
197 |
-
"Configuration saved",
|
198 |
user=username,
|
199 |
-
|
200 |
)
|
201 |
|
202 |
except Exception as e:
|
|
|
151 |
with cls._file_lock:
|
152 |
try:
|
153 |
# Load current config for race condition check
|
154 |
+
try:
|
155 |
+
current_config = cls._load()
|
156 |
+
|
157 |
+
# Check for race condition
|
158 |
+
if config.last_update_date and current_config.last_update_date:
|
159 |
+
if not timestamps_equal(config.last_update_date, current_config.last_update_date):
|
160 |
+
raise RaceConditionError(
|
161 |
+
"Configuration was modified by another user",
|
162 |
+
current_user=username,
|
163 |
+
last_update_user=current_config.last_update_user,
|
164 |
+
last_update_date=current_config.last_update_date,
|
165 |
+
entity_type="configuration"
|
166 |
+
)
|
167 |
+
except ConfigurationError as e:
|
168 |
+
# Eğer mevcut config yüklenemiyorsa, race condition kontrolünü atla
|
169 |
+
log_warning(f"Could not load current config for race condition check: {e}")
|
170 |
+
current_config = None
|
171 |
|
172 |
# Update metadata
|
173 |
config.last_update_date = get_current_timestamp()
|
174 |
config.last_update_user = username
|
175 |
|
176 |
# Convert to JSON - Pydantic v2 kullanımı
|
177 |
+
data = config.model_dump(mode='json')
|
178 |
json_str = json.dumps(data, ensure_ascii=False, indent=2)
|
179 |
|
180 |
+
# Backup current file if exists
|
181 |
+
backup_path = None
|
182 |
+
if cls._CONFIG_PATH.exists():
|
183 |
+
backup_path = cls._CONFIG_PATH.with_suffix('.backup')
|
184 |
+
shutil.copy2(str(cls._CONFIG_PATH), str(backup_path))
|
185 |
+
log_debug(f"Created backup at {backup_path}")
|
186 |
+
|
187 |
+
try:
|
188 |
+
# Write to temporary file first
|
189 |
+
temp_path = cls._CONFIG_PATH.with_suffix('.tmp')
|
190 |
+
with open(temp_path, 'w', encoding='utf-8') as f:
|
191 |
+
f.write(json_str)
|
192 |
+
|
193 |
+
# Validate the temp file by trying to load it
|
194 |
+
with open(temp_path, 'r', encoding='utf-8') as f:
|
195 |
+
test_data = commentjson.load(f)
|
196 |
+
ServiceConfig.model_validate(test_data)
|
197 |
+
|
198 |
+
# If validation passes, replace the original
|
199 |
+
shutil.move(str(temp_path), str(cls._CONFIG_PATH))
|
200 |
+
|
201 |
+
# Delete backup if save successful
|
202 |
+
if backup_path and backup_path.exists():
|
203 |
+
backup_path.unlink()
|
204 |
+
|
205 |
+
except Exception as e:
|
206 |
+
# Restore from backup if something went wrong
|
207 |
+
if backup_path and backup_path.exists():
|
208 |
+
shutil.move(str(backup_path), str(cls._CONFIG_PATH))
|
209 |
+
log_error(f"Restored configuration from backup due to error: {e}")
|
210 |
+
raise
|
211 |
|
212 |
+
# Update cached instance
|
|
|
|
|
|
|
|
|
213 |
with cls._lock:
|
214 |
cls._instance = config
|
|
|
215 |
|
216 |
log_info(
|
217 |
+
"Configuration saved successfully",
|
218 |
user=username,
|
219 |
+
last_update=config.last_update_date
|
220 |
)
|
221 |
|
222 |
except Exception as e:
|