Skip to content
This repository has been archived by the owner on Feb 20, 2018. It is now read-only.

Commit

Permalink
Cleanup bloat bug, cleanup separate transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Zubov authored and Stanislav Zubov committed Nov 16, 2015
1 parent 69b28d7 commit aaf626b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
17 changes: 15 additions & 2 deletions core/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ class SessionLogStep(Base, FeaturesMixin):

# Relationships
sub_steps = relationship(
SessionLogSubStep, backref="session_log_step")
SessionLogSubStep,
cascade="all, delete",
backref=backref(
"session_log_step",
single_parent=True
)
)

def __init__(self, control_line, body=None, session_id=None):
self.control_line = control_line
Expand Down Expand Up @@ -102,7 +108,14 @@ class Session(Base, FeaturesMixin):

# Relationships
session_steps = relationship(
SessionLogStep, backref=backref("session", enable_typechecks=False))
SessionLogStep,
cascade="all, delete",
backref=backref(
"session",
enable_typechecks=False,
single_parent=True
)
)

def set_user(self, username):
self.user = current_app.database.get_user(username=username)
Expand Down
18 changes: 11 additions & 7 deletions vmmaster/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
from core.utils.init import home_dir
from core.logger import setup_logging, log

from shutil import rmtree
from errno import ENOENT

setup_config('%s/config.py' % home_dir())
setup_logging(logdir=config.LOG_DIR, logfile_name='vmmaster_cleanup.log')

Expand Down Expand Up @@ -44,9 +47,6 @@ def old_sessions(dbsession=None):


def delete_files(session=None):
from shutil import rmtree
from errno import ENOENT

if session:
session_dir = os.path.join(config.SCREENSHOTS_DIR, str(session.id))
try:
Expand All @@ -59,7 +59,12 @@ def delete_files(session=None):


@transaction
def delete_session_data(sessions=None, dbsession=None):
def delete(session, dbsession=None):
dbsession.delete(session)
dbsession.commit()


def delete_session_data(sessions=None, ):
sessions_count = len(sessions)

log.info("Got %s sessions. " % str(sessions_count))
Expand All @@ -72,16 +77,15 @@ def delete_session_data(sessions=None, dbsession=None):
log.info("Done: %s%% (0 / %d)" % ('0.0'.rjust(5), sessions_count))
for num, session in enumerate(sessions):
delta = datetime.now() - checkpoint
# Show deletion progress each 10 seconds
if delta > time_step or num == sessions_count - 1:
percentage = str(
round((num + 1)/float(sessions_count) * 100, 1))
log.info("Done: %s%% (%d / %d)" %
(percentage.rjust(5), num + 1, sessions_count))
checkpoint = datetime.now()
delete_files(session)
dbsession.delete(session)
dbsession.commit()
delete(session)

log.info("Total: %s sessions (%d:%d) have been deleted.\n" % (
str(sessions_count), first_id, last_id))
else:
Expand Down

0 comments on commit aaf626b

Please sign in to comment.