Harden braindance state persistence
This commit is contained in:
1 parent
8f58bbef82
commit
9dbd6b7873
2 files changed
+51
-10
No files matched your search
@@ -167,7 +167,8 @@ def touch_feed(name):
|
||||
del FEED[:-80]
|
||||
|
||||
STATE_FILE = os.environ.get('BD_STATE_FILE', '/data/state.json')
|
||||
SNAPSHOT_INTERVAL = float(os.environ.get('SNAPSHOT_INTERVAL_SECONDS', '15'))
|
||||
STATE_BACKUP = STATE_FILE + '.bak'
|
||||
SNAPSHOT_INTERVAL = float(os.environ.get('SNAPSHOT_INTERVAL_SECONDS', '5'))
|
||||
if SNAPSHOT_INTERVAL <= 0:
|
||||
raise ValueError('SNAPSHOT_INTERVAL_SECONDS must be positive')
|
||||
|
||||
@@ -191,6 +192,28 @@ def _json_value(value):
|
||||
return sorted(value)
|
||||
raise TypeError(type(value).__name__)
|
||||
|
||||
def _fsync_parent(path):
|
||||
directory = os.path.dirname(os.path.abspath(path)) or '.'
|
||||
try:
|
||||
fd = os.open(directory, os.O_RDONLY)
|
||||
except OSError:
|
||||
return
|
||||
try:
|
||||
os.fsync(fd)
|
||||
except OSError:
|
||||
pass
|
||||
finally:
|
||||
os.close(fd)
|
||||
|
||||
def _write_snapshot(path, snapshot):
|
||||
temp_path = path + '.tmp'
|
||||
with open(temp_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(snapshot, f, default=_json_value, separators=(',', ':'))
|
||||
f.flush()
|
||||
os.fsync(f.fileno())
|
||||
os.replace(temp_path, path)
|
||||
_fsync_parent(path)
|
||||
|
||||
def _save_state(force=False):
|
||||
global DIRTY
|
||||
try:
|
||||
@@ -201,20 +224,38 @@ def _save_state(force=False):
|
||||
snapshot = _serialize_nolock()
|
||||
DIRTY = False
|
||||
os.makedirs(os.path.dirname(STATE_FILE) or '.', exist_ok=True)
|
||||
with open(STATE_FILE + '.tmp', 'w') as f:
|
||||
json.dump(snapshot, f, default=_json_value, separators=(',', ':'))
|
||||
os.replace(STATE_FILE + '.tmp', STATE_FILE)
|
||||
except OSError:
|
||||
_write_snapshot(STATE_FILE, snapshot)
|
||||
try:
|
||||
_write_snapshot(STATE_BACKUP, snapshot)
|
||||
except OSError:
|
||||
app.logger.exception('Could not update state backup')
|
||||
except (OSError, TypeError, ValueError):
|
||||
with LOCK:
|
||||
DIRTY = True
|
||||
app.logger.exception('Could not save state')
|
||||
|
||||
def _load_state():
|
||||
try:
|
||||
with open(STATE_FILE) as f:
|
||||
data = json.load(f)
|
||||
except (OSError, ValueError):
|
||||
data = None
|
||||
loaded_from = None
|
||||
for path in (STATE_FILE, STATE_FILE + '.tmp', STATE_BACKUP):
|
||||
try:
|
||||
with open(path, encoding='utf-8') as f:
|
||||
candidate = json.load(f)
|
||||
if not isinstance(candidate, dict):
|
||||
raise ValueError('state root must be an object')
|
||||
data = candidate
|
||||
loaded_from = path
|
||||
break
|
||||
except (OSError, ValueError):
|
||||
continue
|
||||
if data is None:
|
||||
if os.path.exists(STATE_FILE):
|
||||
app.logger.error('Could not load state or its backups')
|
||||
return
|
||||
if loaded_from != STATE_FILE:
|
||||
app.logger.warning('Recovered state from %s', loaded_from)
|
||||
global DIRTY
|
||||
DIRTY = True
|
||||
with LOCK:
|
||||
USERS.update(data.get('USERS', {}))
|
||||
for rid, r in data.get('RECORDS', {}).items():
|
||||
|
||||
@@ -22,7 +22,7 @@ services:
|
||||
RETENTION_SECONDS: ${RETENTION_SECONDS:-1800}
|
||||
FLAG_LIFETIME_SECONDS: ${FLAG_LIFETIME_SECONDS:-300}
|
||||
CLEANUP_INTERVAL_SECONDS: ${CLEANUP_INTERVAL_SECONDS:-15}
|
||||
SNAPSHOT_INTERVAL_SECONDS: ${SNAPSHOT_INTERVAL_SECONDS:-15}
|
||||
SNAPSHOT_INTERVAL_SECONDS: ${SNAPSHOT_INTERVAL_SECONDS:-5}
|
||||
MAX_DATA_BYTES: ${BD_MAX_DATA_BYTES:-67108864}
|
||||
ports:
|
||||
- "5000:5000"
|
||||
|
||||
Reference in new issue
Block a user