Add config file locking
This commit is contained in:
parent
cb0d0012c9
commit
f675996e60
@ -1,20 +1,27 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import fcntl
|
||||||
import json
|
import json
|
||||||
|
|
||||||
CONF_FILE = '/srv/vm/config.json'
|
CONF_FILE = '/srv/vm/config.json'
|
||||||
|
# Locking is needed in order to prevent race conditions in WSGI threads
|
||||||
|
LOCK_FILE = '/srv/vm/config.lock'
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.load()
|
self.load()
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
with open(CONF_FILE, 'r') as f:
|
with open(LOCK_FILE, 'w') as l:
|
||||||
self.data = json.load(f)
|
fcntl.flock(l, fcntl.LOCK_EX)
|
||||||
|
with open(CONF_FILE, 'r') as f:
|
||||||
|
self.data = json.load(f)
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
with open(CONF_FILE, 'w') as f:
|
with open(LOCK_FILE, 'w') as l:
|
||||||
json.dump(self.data, f, sort_keys=True, indent=4)
|
fcntl.flock(l, fcntl.LOCK_EX)
|
||||||
|
with open(CONF_FILE, 'w') as f:
|
||||||
|
json.dump(self.data, f, sort_keys=True, indent=4)
|
||||||
|
|
||||||
def __getitem__(self, attr):
|
def __getitem__(self, attr):
|
||||||
return self.data[attr]
|
return self.data[attr]
|
||||||
|
Loading…
Reference in New Issue
Block a user