Skip to content

Commit 6bb8a45

Browse files
committed
Add retry mechanism for reporead updates
When reporead_inotify is unable to reach the postgresql server an exception is thrown and nothing is retried which means db updates are missed. This change adds a simple retry mechanism which retries up till 5 times * 5 seconds and after that the change is ignored. Closes: #336
1 parent adb5eb8 commit 6bb8a45

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

devel/management/commands/reporead_inotify.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from django.core.management.base import BaseCommand, CommandError
2626
from django.db import connection, transaction
27+
from django.db.utils import OperationalError
2728

2829
from main.models import Arch, Repo
2930
from .reporead import read_repo
@@ -117,11 +118,12 @@ class Database(object):
117118
various bits of metadata and state representing the file path, when we last
118119
updated, how long our delay is before performing the update, whether we are
119120
updating now, etc.'''
120-
def __init__(self, arch, path, delay=60.0, nice=3):
121+
def __init__(self, arch, path, delay=60.0, nice=3, retry_limit=5):
121122
self.arch = arch
122123
self.path = path
123124
self.delay = delay
124125
self.nice = nice
126+
self.retry_limit = retry_limit
125127
self.mtime = None
126128
self.last_import = None
127129
self.update_thread = None
@@ -158,10 +160,23 @@ def update(self):
158160
# invoke reporead's primary method. we do this in a separate
159161
# process for memory conservation purposes; these processes grow
160162
# rather large so it is best to free up the memory ASAP.
163+
# A retry mechanism exists for when reporead_inotify runs on a different machine.
161164
def run():
165+
retry = True
166+
retry_count = 0
162167
if self.nice != 0:
163168
os.nice(self.nice)
164-
read_repo(self.arch, self.path, {})
169+
while retry and retry_count < self.retry_limit:
170+
try:
171+
read_repo(self.arch, self.path, {})
172+
retry = False
173+
except OperationalError as exc:
174+
retry_count += 1
175+
logger.error('Unable to update database \'%s\', retrying=%d', self.path, retry_count, exc_info=exc)
176+
time.sleep(5)
177+
178+
if retry_count == self.retry_limit:
179+
logger.error('Unable to update database, exceeded maximum retries')
165180

166181
process = multiprocessing.Process(target=run)
167182
process.start()

0 commit comments

Comments
 (0)