Commit 78c75b57 authored by Benjamin Fischer's avatar Benjamin Fischer
Browse files

workspace.ini reader: avoid polling by implementing a workspace-global watch...

workspace.ini reader: avoid polling by implementing a workspace-global watch emitting a message on changes or deletion/renaming
parent a8b783e0
......@@ -86,7 +86,11 @@ class AbstractController(object):
elif key == "fs":
workspace = request.workspace if len(args) == 0 else args[0]
return vispa.workspace.get_instance(
"vispa.remote.filesystem.FileSystem", workspace=workspace)
"vispa.remote.filesystem.FileSystem", workspace=workspace,
init_args={
'workspaceid': workspace.id,
'userid': request.user.id
})
elif key == "workspace_id":
return request.private_params.get("_workspaceId", None)
elif key == "view_id":
......
......@@ -146,7 +146,7 @@ var FileBase = Class.extend({
}
this.helper.sortItems(content, sort, reverse);
this.workflow.currentView.setContent(content);
this.bookmarkcontainer.setContent();
this.bookmarkcontainer.setup();
this.menuitems.hideMenu();
this.instance.setLabel(this.workflow.path, true);
this.instance.setLoading(false);
......
......@@ -4,6 +4,26 @@ var BookmarkContainer = Class.extend({
this.FileBase = FileBase;
this.instance = FileBase.instance;
this.parentnode = null;
this._workspaceini_mtime = 0;
this._setup_done = false;
},
setup: function() {
if (this._setup_done) {
return;
}
this._setup_done = true;
var self = this;
vispa.socket.on('workspace.ini_modified', function(data){
if (data.workspaceId == self.instance.getWorkspaceId() &&
self._workspaceini_mtime != data.mtime) {
self._workspaceini_mtime = data.mtime;
self.setContent();
}
});
this.setContent();
},
setContainer: function(node) {
......@@ -21,7 +41,7 @@ var BookmarkContainer = Class.extend({
setContent: function() {
var self = this;
// get names from workspaceini
self.instance.POST("/ajax/fs/get_workspaceini",{
this.instance.POST("/ajax/fs/get_workspaceini",{
request: JSON.stringify({
Bookmarks:['names','paths']
})
......@@ -122,7 +142,6 @@ var BookmarkContainer = Class.extend({
// set new data to ini and update view
this.saveBookmarks().done(function(res) {
if (res.success) {
self.setContent();
self.editBookmark(self.names.length - 1);
} else {
self.instance.alert("Couldn't add bookmark.");
......@@ -146,9 +165,7 @@ var BookmarkContainer = Class.extend({
self.paths.splice(index, 1);
// set new data to ini and update view
self.saveBookmarks().done(function(res) {
if (res.success) {
self.setContent();
} else {
if (!res.success) {
self.instance.alert("Couldn't remove bookmark.");
self.names.splice(index, 0, name);
self.paths.splice(index, 0, path);
......@@ -190,9 +207,7 @@ var BookmarkContainer = Class.extend({
self.paths.splice(newindex, 0, path);
// set new data to ini and update view
self.saveBookmarks().done(function(res) {
if (res) {
self.setContent();
} else {
if (!res.success) {
self.instance.alert("Couldn't edit bookmark.");
self.names.splice(newindex, 1);
self.names.splice(index, 0, name);
......
......@@ -43,14 +43,19 @@ class FileSystem(object):
"pxlio": "text/plain",
"root": "text/plain"
}
PRIVATE_WORKSPACE_CONF = "~/.vispa/workspace.ini"
GLOBAL_WORKSPACE_CONF = "/etc/vispa/workspace.ini"
def __init__(self):
def __init__(self, userid, workspaceid):
# allowed extensions
self.allowed_extensions = FileSystem.FILE_EXTENSIONS
self._userid = userid
self._workspaceid = workspaceid
self._monitor_thread = fsmonitor.FSMonitorThread(self._monitor_callback)
self._monitor_watches = {}
self._monitor_listener = {}
self._monitor_lock = Lock()
self._monitor_watch_workspaceini = None
def __del__(self):
self.close()
......@@ -523,25 +528,38 @@ class FileSystem(object):
else:
mtime = -1
with self._monitor_lock:
# inform all listener
for combined_id in event.watch.listener:
vispa.remote.send_topic(
"extension.%s.socket.watch" % combined_id[1],
window_id=combined_id[0],
data={
"path": event.watch.path,
"action_name": event.action_name,
"watch_id": combined_id[2],
if event.watch == self._monitor_watch_workspaceini:
if mtime == -1 or event.action_name == 'modify':
vispa.remote.send_topic('workspace.ini_modified',
user_id=self._userid, data={
"workspaceId": self._workspaceid,
"mtime": mtime
})
# cleanup yourself
if event.action_name in ['delete self','move self']:
if mtime == -1:
self._monitor_thread.remove_watch(event.watch)
self._monitor_watch_workspaceini = None
return
else:
with self._monitor_lock:
# inform all listener
for combined_id in event.watch.listener:
del self._monitor_listener[combined_id]
event.watch.listener = []
self._check_watch(event.watch)
vispa.remote.send_topic(
"extension.%s.socket.watch" % combined_id[1],
window_id=combined_id[0],
data={
"path": event.watch.path,
"action_name": event.action_name,
"watch_id": combined_id[2],
"mtime": mtime
})
# cleanup yourself
if event.action_name in ['delete self','move self']:
for combined_id in event.watch.listener:
del self._monitor_listener[combined_id]
event.watch.listener = []
self._check_watch(event.watch)
def _check_watch(self, watch):
if len(watch.listener) == 0:
......@@ -612,8 +630,10 @@ class FileSystem(object):
try:
request_dict = json.loads(request)
config = ConfigParser.ConfigParser()
config.read(["/etc/vispa/workspace.ini",
self.expand("$HOME/.vispa/workspace.ini")])
config.read([FileSystem.GLOBAL_WORKSPACE_CONF,
self.expand(FileSystem.PRIVATE_WORKSPACE_CONF)])
mtime = os.path.getmtime(self.expand(FileSystem.PRIVATE_WORKSPACE_CONF))
self._watch_workspaceini()
if not isinstance(request_dict, dict):
request_dict = dict.fromkeys(config.sections(), True)
data = {}
......@@ -634,7 +654,8 @@ class FileSystem(object):
raise Exception('workspace.ini is missing the section [%s]' % section)
return json.dumps({
"content": data,
"success": True
"success": True,
"mtime": mtime
})
except Exception as e:
return json.dumps({
......@@ -649,7 +670,7 @@ class FileSystem(object):
if not isinstance(request_dict, dict):
raise Exception('Given values to be set in workspace.ini in wrong format')
config = ConfigParser.ConfigParser()
config.read(self.expand("$HOME/.vispa/workspace.ini"))
config.read(self.expand(FileSystem.PRIVATE_WORKSPACE_CONF))
for section, options in request_dict.iteritems():
if not isinstance(options, dict):
raise Exception('Given values to be set in workspace.ini in wrong format')
......@@ -657,11 +678,20 @@ class FileSystem(object):
config.add_section(section)
for name, value in options.iteritems():
config.set(section, name, value)
with open(self.expand("$HOME/.vispa/workspace.ini"), 'w') as f:
with open(self.expand(FileSystem.PRIVATE_WORKSPACE_CONF), 'w') as f:
self._watch_workspaceini()
config.write(f)
return ""
except Exception as e:
return str(e)
def _watch_workspaceini(self):
if self._monitor_watch_workspaceini:
return
if not self.exists(FileSystem.PRIVATE_WORKSPACE_CONF, 'f'):
return
self._monitor_watch_workspaceini = self._monitor_thread.add_file_watch(
self.expand(FileSystem.PRIVATE_WORKSPACE_CONF))
def string_compare(a, b):
if a == b:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment