Skip to content
Snippets Groups Projects
Commit 95ee6a06 authored by ale's avatar ale
Browse files

commit more often

parent bb28c60c
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,8 @@ from djrandom.database import Session ...@@ -5,6 +5,8 @@ from djrandom.database import Session
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
COMMIT_EVERY = 50
class Processor(object): class Processor(object):
"""Base class to perform operations on database objects. """Base class to perform operations on database objects.
...@@ -24,7 +26,6 @@ class Processor(object): ...@@ -24,7 +26,6 @@ class Processor(object):
n = 0 n = 0
items = self.query() items = self.query()
for item in items: for item in items:
n += 1
try: try:
self.process(item) self.process(item)
Session.add(item) Session.add(item)
...@@ -32,6 +33,9 @@ class Processor(object): ...@@ -32,6 +33,9 @@ class Processor(object):
log.error( log.error(
'Unexpected exception processing item %s:\n %s' % ( 'Unexpected exception processing item %s:\n %s' % (
item, traceback.format_exc())) item, traceback.format_exc()))
n += 1
if (n % COMMIT_EVERY == 0) and not self._dry_run:
Session.commit()
if n > 0 and not self._dry_run: if n > 0 and not self._dry_run:
Session.commit() Session.commit()
self.commit() self.commit()
...@@ -53,7 +57,7 @@ class Processor(object): ...@@ -53,7 +57,7 @@ class Processor(object):
try: try:
n = self.full_scan() n = self.full_scan()
except Exception, e: except Exception, e:
log.error('Temporary error: %s' % e) log.exception('Temporary error')
Session.remove() Session.remove()
if run_once: if run_once:
break break
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment