feat(settings): patch API in-memory settings on save and add Back to ENV button
Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
@@ -38,7 +38,9 @@ _last_seen_version: str = ""
|
|||||||
|
|
||||||
def notify_settings_updated() -> None:
|
def notify_settings_updated() -> None:
|
||||||
"""
|
"""
|
||||||
Publish a settings-updated signal by updating the Redis version key.
|
Publish a settings-updated signal by updating the Redis version key, and
|
||||||
|
immediately reload the in-process ``settings`` singleton so the API
|
||||||
|
process serves fresh values without a restart.
|
||||||
|
|
||||||
Call this after every successful settings write so that all worker
|
Call this after every successful settings write so that all worker
|
||||||
processes know they need to reload their in-memory configuration.
|
processes know they need to reload their in-memory configuration.
|
||||||
@@ -56,6 +58,19 @@ def notify_settings_updated() -> None:
|
|||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.warning(f"Could not publish settings update to Redis: {exc}")
|
logger.warning(f"Could not publish settings update to Redis: {exc}")
|
||||||
|
|
||||||
|
# Reload the in-process settings singleton immediately so the API node
|
||||||
|
# returns updated values (e.g. oauth_provider_name on the login page)
|
||||||
|
# without needing a restart. Workers use the task_prerun signal handler
|
||||||
|
# instead, so this only affects the API/web process.
|
||||||
|
try:
|
||||||
|
from app.config import settings
|
||||||
|
from app.utils.config_loader import reload_settings_from_db
|
||||||
|
|
||||||
|
reload_settings_from_db(settings)
|
||||||
|
logger.debug("In-process settings reloaded after settings update")
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning(f"Could not reload in-process settings: {exc}")
|
||||||
|
|
||||||
|
|
||||||
def register_settings_reload_signal() -> None:
|
def register_settings_reload_signal() -> None:
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -185,7 +185,7 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Per-setting Save button (visible only when value has changed) -->
|
<!-- Per-setting Save / Back-to-ENV buttons -->
|
||||||
<div class="ml-4 flex-shrink-0 flex flex-col items-end gap-1 pt-1">
|
<div class="ml-4 flex-shrink-0 flex flex-col items-end gap-1 pt-1">
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
@@ -199,6 +199,18 @@
|
|||||||
<span x-show="savingKey !== '{{ setting.key }}'"><i class="fas fa-save mr-1"></i>Save</span>
|
<span x-show="savingKey !== '{{ setting.key }}'"><i class="fas fa-save mr-1"></i>Save</span>
|
||||||
<span x-show="savingKey === '{{ setting.key }}'">Saving…</span>
|
<span x-show="savingKey === '{{ setting.key }}'">Saving…</span>
|
||||||
</button>
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
x-show="isDbOverride['{{ setting.key }}'] && formData['{{ setting.key }}'] === originalData['{{ setting.key }}']"
|
||||||
|
x-transition
|
||||||
|
@click="revertSetting('{{ setting.key }}')"
|
||||||
|
:disabled="revertingKey === '{{ setting.key }}'"
|
||||||
|
class="px-3 py-1 text-sm bg-orange-500 text-white rounded-md hover:bg-orange-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-orange-400 disabled:opacity-50 disabled:cursor-not-allowed whitespace-nowrap"
|
||||||
|
title="Remove DB override and revert to environment variable or default"
|
||||||
|
>
|
||||||
|
<span x-show="revertingKey !== '{{ setting.key }}'"><i class="fas fa-undo mr-1"></i>Back to ENV</span>
|
||||||
|
<span x-show="revertingKey === '{{ setting.key }}'">Reverting…</span>
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -233,9 +245,11 @@ function settingsApp() {
|
|||||||
return {
|
return {
|
||||||
formData: {},
|
formData: {},
|
||||||
originalData: {},
|
originalData: {},
|
||||||
|
isDbOverride: {},
|
||||||
showPassword: {},
|
showPassword: {},
|
||||||
saving: false,
|
saving: false,
|
||||||
savingKey: null,
|
savingKey: null,
|
||||||
|
revertingKey: null,
|
||||||
showAlert: false,
|
showAlert: false,
|
||||||
alertType: 'success',
|
alertType: 'success',
|
||||||
alertTitle: '',
|
alertTitle: '',
|
||||||
@@ -247,6 +261,7 @@ function settingsApp() {
|
|||||||
{% for setting in settings_list %}
|
{% for setting in settings_list %}
|
||||||
this.formData['{{ setting.key }}'] = {{ setting.display_value|tojson }};
|
this.formData['{{ setting.key }}'] = {{ setting.display_value|tojson }};
|
||||||
this.originalData['{{ setting.key }}'] = {{ setting.display_value|tojson }};
|
this.originalData['{{ setting.key }}'] = {{ setting.display_value|tojson }};
|
||||||
|
this.isDbOverride['{{ setting.key }}'] = {{ (setting.source == 'database')|tojson }};
|
||||||
this.showPassword['{{ setting.key }}'] = false;
|
this.showPassword['{{ setting.key }}'] = false;
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
@@ -297,6 +312,7 @@ function settingsApp() {
|
|||||||
|
|
||||||
if (response.ok && result.success) {
|
if (response.ok && result.success) {
|
||||||
this.originalData[key] = value;
|
this.originalData[key] = value;
|
||||||
|
this.isDbOverride[key] = true;
|
||||||
let message = `Setting '${key}' saved successfully.`;
|
let message = `Setting '${key}' saved successfully.`;
|
||||||
if (result.restart_required) {
|
if (result.restart_required) {
|
||||||
message += ' Please restart the application for this change to take effect.';
|
message += ' Please restart the application for this change to take effect.';
|
||||||
@@ -313,6 +329,30 @@ function settingsApp() {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async revertSetting(key) {
|
||||||
|
this.revertingKey = key;
|
||||||
|
this.hideAlert();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/api/settings/${key}`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
// Reload the page so the ENV/default value and source badge refresh
|
||||||
|
window.location.reload();
|
||||||
|
} else {
|
||||||
|
const result = await response.json();
|
||||||
|
this.showErrorAlert('Revert Failed', result.detail || 'Unknown error');
|
||||||
|
this.revertingKey = null;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error reverting setting:', error);
|
||||||
|
this.showErrorAlert('Error', 'Failed to revert setting. Please try again.');
|
||||||
|
this.revertingKey = null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
async saveSettings() {
|
async saveSettings() {
|
||||||
this.saving = true;
|
this.saving = true;
|
||||||
this.hideAlert();
|
this.hideAlert();
|
||||||
@@ -345,7 +385,10 @@ function settingsApp() {
|
|||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
|
|
||||||
if (response.ok && result.success) {
|
if (response.ok && result.success) {
|
||||||
this.originalData = { ...this.formData };
|
for (const updated of result.updated) {
|
||||||
|
this.originalData[updated.key] = updated.value;
|
||||||
|
this.isDbOverride[updated.key] = true;
|
||||||
|
}
|
||||||
let message = `${result.updated.length} setting(s) updated successfully.`;
|
let message = `${result.updated.length} setting(s) updated successfully.`;
|
||||||
if (result.restart_required) {
|
if (result.restart_required) {
|
||||||
message += ' Please restart the application for changes to take effect.';
|
message += ' Please restart the application for changes to take effect.';
|
||||||
|
|||||||
Reference in New Issue
Block a user