From 16fc4b8c54c262b0ca5fd2909b7ced7b258f72a4 Mon Sep 17 00:00:00 2001 From: Swann Date: Thu, 10 Dec 2020 15:50:43 +0100 Subject: [PATCH] feat: added record as a timer --- multi_user/delayable.py | 40 ++++++++++++++++++++++++++++++++++++ multi_user/operators.py | 45 +++++++---------------------------------- 2 files changed, 47 insertions(+), 38 deletions(-) diff --git a/multi_user/delayable.py b/multi_user/delayable.py index 3196b98..29bf282 100644 --- a/multi_user/delayable.py +++ b/multi_user/delayable.py @@ -109,6 +109,46 @@ class Timer(Delayable): self.is_running = False +class SessionRecordGraphTimer(Timer): + def __init__(self, timout=60, filepath=None): + self._filepath = filepath + super().__init__(timout) + + + def execute(self): + import networkx as nx + import pickle + import copy + from time import gmtime, strftime + from pathlib import Path + + # Replication graph + nodes_ids = session.list() + #TODO: add dump graph to replication + + nodes =[] + for n in nodes_ids: + nd = session.get(uuid=n) + nodes.append(( + n, + { + 'owner': nd.owner, + 'str_type': nd.str_type, + 'data': nd.data, + 'dependencies': nd.dependencies, + } + )) + + db = dict() + db['nodes'] = nodes + db['users'] = copy.copy(session.online_users) + + time = strftime("%Y_%m_%d_%H_%M_%S", gmtime()) + filepath = Path(self._filepath) + filepath = filepath.with_name(f"{filepath.stem}_{time}{filepath.suffix}") + with open(filepath, "wb") as f: + logging.info(f"Writing db snapshot to {filepath}") + pickle.dump(db, f, protocol=4) class ApplyTimer(Timer): def __init__(self, timout=1, target_type=None): diff --git a/multi_user/operators.py b/multi_user/operators.py index 8780a83..28f141b 100644 --- a/multi_user/operators.py +++ b/multi_user/operators.py @@ -721,48 +721,17 @@ class SessionRecordGraphOperator(bpy.types.Operator, ExportHelper): # ExportHelper mixin class uses this filename_ext = ".db" + def execute(self, context): + recorder = delayable.SessionRecordGraphTimer(filepath=self.filepath) + recorder.register() + + deleyables.append(recorder) + return {'FINISHED'} + @classmethod def poll(cls, context): return session.state['STATE'] == STATE_ACTIVE - def execute(self, context): - import networkx as nx - import pickle - import copy - - # Replication graph - nodes_ids = session.list() - #TODO: add dump graph to replication - - nodes =[] - for n in nodes_ids: - nd = session.get(uuid=n) - nodes.append(( - n, - { - 'owner': nd.owner, - 'str_type': nd.str_type, - 'data': nd.data, - 'dependencies': nd.dependencies, - } - )) - - G = nx.DiGraph() - G.add_nodes_from(nodes) - - for n, nd in nodes: - relations = [(n,d) for d in nd['dependencies']] - G.add_edges_from(relations) - - - # Users - G.graph['users'] = copy.copy(session.online_users) - - with open(self.filepath, "wb") as f: - pickle.dump(G, f, protocol=4) - - return {'FINISHED'} - classes = ( SessionStartOperator,