From e42e1a3af546cf6ec8ceae50620142c6e163ffdd Mon Sep 17 00:00:00 2001 From: boyska Date: Tue, 8 Apr 2014 18:03:08 +0200 Subject: [PATCH] FIX session usage in DB (but in the dirtier way) --- server/forge.py | 1 + server/maint.py | 9 +++++++-- server/techrec.py | 24 +++++++++++++++--------- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/server/forge.py b/server/forge.py index dc74ee8..260cf66 100644 --- a/server/forge.py +++ b/server/forge.py @@ -59,6 +59,7 @@ def mp3_join(named_intervals, target): This function make the (quite usual) assumption that the only start_cut (if any) is at the first file, and the last one is at the last file ''' + # TODO: ffmpeg binary should be configurable ffmpeg = 'ffmpeg' # binary name startskip = None endskip = None diff --git a/server/maint.py b/server/maint.py index 5cad10a..688a73e 100644 --- a/server/maint.py +++ b/server/maint.py @@ -1,6 +1,8 @@ import sys import logging +from sqlalchemy import inspect + from config_manager import get_config from techrec import RecDB @@ -16,7 +18,10 @@ def cleanold_cmd(options): else: for rec in res: logging.info("Deleting " + str(rec)) - db.session.delete(rec) - db.commit() + s = inspect(rec).session + s.delete(rec) + s.commit() logging.info("Cleanold complete: %d deleted" % count) sys.exit(0) + +# vim: set ai ts=4 sw=4 et: diff --git a/server/techrec.py b/server/techrec.py index 4668812..0308234 100644 --- a/server/techrec.py +++ b/server/techrec.py @@ -4,7 +4,7 @@ from datetime import datetime, timedelta import sys -from sqlalchemy import create_engine, Column, Integer, String, DateTime +from sqlalchemy import create_engine, Column, Integer, String, DateTime, inspect from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base @@ -72,8 +72,9 @@ class RecDB: self.err = "" def add(self, simplerecord): - print self.session.add( simplerecord ) - self.commit() + s = self.get_session() + s.add( simplerecord ) + s.commit() self.log.info("New Record: %s" % simplerecord) return ( simplerecord ) @@ -94,7 +95,7 @@ class RecDB: _rlist[0].name = rec["name"] self.log.debug("DB:: Update: data AFTER %s" % _rlist[0]) - self.commit() + inspect(_rlist[0]).session.commit() self.log.debug("DB:: Update complete") return _rlist[0] @@ -112,15 +113,20 @@ class RecDB: self.err = "multiple ID Found %s" % (_rlist) return False - self.session.delete(_rlist[0]) + s = self.get_session() + s.delete(_rlist[0]) logging.info("DB: Delete: delete complete") - self.commit() + s.commit() return True def commit(self): logging.info("DB: Commit!!") self.session.commit() + def get_session(self): + Session = sessionmaker(bind=self.engine) + return Session() + def get_all(self, page=0, page_size=PAGESIZE): return self._search(page=page, page_size=page_size) @@ -135,14 +141,14 @@ class RecDB: def _query_ongoing(self, query=None): if query is None: - query = self.session.query(Rec) + query = self.get_session().query(Rec) return query.filter(Rec.filename == None) def _query_older(self, delta, query=None): if query is None: - query = self.session.query(Rec) + query = self.get_session().query(Rec) return query.filter(Rec.endtime < datetime.now() - delta) @@ -174,7 +180,7 @@ class RecDB: "DB: Search => id:%s name:%s starttime:%s endtime=%s" % (_id, name, starttime, endtime)) - query = self.session.query(Rec) + query = self.get_session().query(Rec) query = self._query_generic(query, _id, name, starttime, endtime) query = self._query_page(query, page, page_size)