@@ -578,6 +578,7 @@ def archiveElements(self, elements):
578578 conn .cache .clear ()
579579 elif isinstance (element , RenderNode ):
580580 StatDB .archiveRenderNode (self , element )
581+ conn = RenderNodes ._connection
581582 conn .query (conn .sqlrepr (Delete (RenderNodes .q , where = (RenderNodes .q .id == element .id ))))
582583 conn .cache .clear ()
583584
@@ -1168,43 +1169,80 @@ def restoreStateFromDb(self, tree, rnsAlreadyLoaded):
11681169
11691170 ### calculate the correct max ids for all elements, get them from db in case of archived elements that would not appear in the dispatchtree
11701171 prevTimer = time .time ()
1172+ statConn = StatDB .createConnection ()
11711173 try :
1172- tree .nodeMaxId = int (max ([FolderNodes .select ().max (FolderNodes .q .id ), TaskNodes .select ().max (TaskNodes .q .id )]))
1174+ folderConn = FolderNodes ._connection
1175+ taskConn = TaskNodes ._connection
1176+ FolderNodes ._connection = statConn
1177+ TaskNodes ._connection = statConn
1178+ #adding 0 to max([...]) to avoid TypeError if stat table is empty
1179+ statMaxId = int (max ([FolderNodes .select ().max (FolderNodes .q .id ), TaskNodes .select ().max (TaskNodes .q .id ), 0 ]))
1180+ FolderNodes ._connection = folderConn
1181+ TaskNodes ._connection = taskConn
1182+ tree .nodeMaxId = int (max ([FolderNodes .select ().max (FolderNodes .q .id ), TaskNodes .select ().max (TaskNodes .q .id ), statMaxId ]))
11731183 except :
11741184 tree .nodeMaxId = 0
11751185 LOGGER .warning (" - Set max id for nodes in %.3f s" % (time .time ()- prevTimer ))
11761186
11771187 prevTimer = time .time ()
11781188 try :
1179- tree .poolMaxId = int (Pools .select ().max (Pools .q .id ))
1189+ conn = Pools ._connection
1190+ Pools ._connection = statConn
1191+ #adding 0 to max([...]) to avoid TypeError if stat table is empty
1192+ statMaxId = int (max ([Pools .select ().max (Pools .q .id ),0 ]))
1193+ Pools ._connection = conn
1194+ tree .poolMaxId = int (max ([Pools .select ().max (Pools .q .id ), statMaxId ]))
11801195 except :
11811196 tree .poolMaxId = 0
11821197 LOGGER .warning (" - Set max id for pools in %.3f s" % (time .time ()- prevTimer ))
11831198
11841199 prevTimer = time .time ()
11851200 try :
1186- tree .renderNodeMaxId = int (RenderNodes .select ().max (RenderNodes .q .id ))
1201+ conn = RenderNodes ._connection
1202+ RenderNodes ._connection = statConn
1203+ #adding 0 to max([...]) to avoid TypeError if stat table is empty
1204+ statMaxId = int (max ([RenderNodes .select ().max (RenderNodes .q .id ),0 ]))
1205+ RenderNodes ._connection = conn
1206+ tree .renderNodeMaxId = int (max ([RenderNodes .select ().max (RenderNodes .q .id ),statMaxId ]))
11871207 except :
11881208 tree .renderNodeMaxId = 0
11891209 LOGGER .warning (" - Set max id for render nodes in %.3f s" % (time .time ()- prevTimer ))
11901210
11911211 prevTimer = time .time ()
11921212 try :
1193- tree .taskMaxId = int (Tasks .select ().max (Tasks .q .id ))
1213+ taskConn = Tasks ._connection
1214+ taskGroupConn = TaskGroups ._connection
1215+ Tasks ._connection = statConn
1216+ TaskGroups ._connection = statConn
1217+ #adding 0 to max([...]) to avoid TypeError if stat table is empty
1218+ statMaxId = int (max ([Tasks .select ().max (Tasks .q .id ), TaskGroups .select ().max (TaskGroups .q .id ),0 ]))
1219+ Tasks ._connection = taskConn
1220+ TaskGroups ._connection = taskGroupConn
1221+ tree .taskMaxId = int (max ([Tasks .select ().max (Tasks .q .id ), TaskGroups .select ().max (TaskGroups .q .id ), statMaxId ]))
11941222 except :
11951223 tree .taskMaxId = 0
11961224 LOGGER .warning (" - Set max id for tasks in %.3f s" % (time .time ()- prevTimer ))
11971225
11981226 prevTimer = time .time ()
11991227 try :
1200- tree .commandMaxId = int (Commands .select ().max (Commands .q .id ))
1228+ conn = Commands ._connection
1229+ Commands ._connection = statConn
1230+ #adding 0 to max([...]) to avoid TypeError if stat table is empty
1231+ statMaxId = int (max ([Commands .select ().max (Commands .q .id ),0 ]))
1232+ Commands ._connection = conn
1233+ tree .commandMaxId = int (max ([Commands .select ().max (Commands .q .id ),statMaxId ]))
12011234 except :
12021235 tree .commandMaxId = 0
12031236 LOGGER .warning (" - Set max id for commands in %.3f s" % (time .time ()- prevTimer ))
12041237
12051238 prevTimer = time .time ()
12061239 try :
1207- tree .poolShareMaxId = int (PoolShares .select ().max (PoolShares .q .id ))
1240+ conn = PoolShares ._connection
1241+ PoolShares ._connection = statConn
1242+ #adding 0 to max([...]) to avoid TypeError if stat table is empty
1243+ statMaxId = int (max ([PoolShares .select ().max (PoolShares .q .id ),0 ]))
1244+ PoolShares ._connection = conn
1245+ tree .poolShareMaxId = int (max ([PoolShares .select ().max (PoolShares .q .id ),statMaxId ]))
12081246 except :
12091247 tree .poolShareMaxId = 0
12101248 LOGGER .warning (" - Set max id for pool shares in %.3f s" % (time .time ()- prevTimer ))
@@ -1411,3 +1449,44 @@ def archivePoolShare(pulidb, element):
14111449 PoolShares .q .archived .fieldName : True }
14121450 conn .query (conn .sqlrepr (Insert (PoolShares .q , values = fields )))
14131451
1452+ @staticmethod
1453+ def getMaxID (Table ):
1454+ conn = Table ._connection
1455+ Table ._connection = StatDB .createConnection ()
1456+ result = Table .select ().max (Table .q .id )
1457+ Table ._connection = conn
1458+ if result :
1459+ return int (result )
1460+ return 0
1461+
1462+ @staticmethod
1463+ def getRenderNodesMaxId ():
1464+ return StatDB .getMaxID (RenderNodes )
1465+
1466+ @staticmethod
1467+ def getFolderNodesMaxId ():
1468+ return StatDB .getMaxID (FolderNodes )
1469+
1470+ @staticmethod
1471+ def getTaskNodesMaxId ():
1472+ return StatDB .getMaxID (TaskNodes )
1473+
1474+ @staticmethod
1475+ def getTasksMaxId ():
1476+ return StatDB .getMaxID (Tasks )
1477+
1478+ @staticmethod
1479+ def getTaskGroupsMaxId ():
1480+ return getMaxID (TaskGroups )
1481+
1482+ @staticmethod
1483+ def getPoolsMaxId ():
1484+ return StatDB .getMaxID (Pools )
1485+
1486+ @staticmethod
1487+ def getPoolSharesMaxId ():
1488+ return StatDB .getMaxID (PoolShares )
1489+
1490+ @staticmethod
1491+ def getCommandsMaxId ():
1492+ return StatDB .getMaxID (Commands )
0 commit comments