Skip to content

Commit 978e059

Browse files
committed
Allow SolrmarcIndexBackend to ignore empty recsets
Previously if an index update operation was called for an empty record set, the SolrmarcIndexBackend would still generate a MARC file (empty) and attempt to run the Solrmarc Java process. This would lead to a weird "divide by zero" error simply due to the empty file. This just adds checks to make sure the file isn't empty before the Solrmarc process is called.
1 parent 4a9b9ed commit 978e059

1 file changed

Lines changed: 25 additions & 23 deletions

File tree

django/sierra/sierra/solr_backend.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,14 @@ def log_error(self, index, obj_str, err):
214214
def _records_to_marcfile(self, index, records):
215215
batch = index.s2marc_class(records)
216216
out_recs = batch.to_marc()
217-
try:
218-
filename = batch.to_file(out_recs, append=False)
219-
except IOError as e:
220-
raise IOError('Error writing to output file: {}'.format(e))
221-
for e in batch.errors:
222-
self.log_error(index, e.id, e.msg)
223-
return filename
217+
if out_recs:
218+
try:
219+
filename = batch.to_file(out_recs, append=False)
220+
except IOError as e:
221+
raise IOError('Error writing to output file: {}'.format(e))
222+
for e in batch.errors:
223+
self.log_error(index, e.id, e.msg)
224+
return filename
224225

225226
def _formulate_solrmarc_cmd(self, index, rec_filepath, commit):
226227
def_ip = '{}_index.properties'.format(self.get_core_name())
@@ -245,23 +246,24 @@ def update(self, index, records, commit=False):
245246
if not filedir.endswith('/'):
246247
filedir = '{}/'.format(filedir)
247248
rec_filename = self._records_to_marcfile(index, records)
248-
rec_filepath = '{}{}'.format(filedir, rec_filename)
249-
cmd = self._formulate_solrmarc_cmd(index, rec_filepath, commit)
250-
call_options = {'stderr': subprocess.STDOUT, 'shell': False,
251-
'universal_newlines': True}
252-
try:
253-
result = subprocess.check_output(shlex.split(cmd), **call_options)
254-
output = result.decode('unicode-escape')
255-
except subprocess.CalledProcessError as e:
256-
msg = ('Solrmarc process did not run successfully: {}'
249+
if rec_filename is not None:
250+
rec_filepath = '{}{}'.format(filedir, rec_filename)
251+
cmd = self._formulate_solrmarc_cmd(index, rec_filepath, commit)
252+
call_opts = {'stderr': subprocess.STDOUT, 'shell': False,
253+
'universal_newlines': True}
254+
try:
255+
result = subprocess.check_output(shlex.split(cmd), **call_opts)
256+
output = result.decode('unicode-escape')
257+
except subprocess.CalledProcessError as e:
258+
msg = ('Solrmarc process did not run successfully: {}'
257259
''.format(e.output))
258-
self.log_error(index, 'ERROR', msg)
259-
else:
260-
for line in output.split("\n")[:-1]:
261-
line = re.sub(r'^\s+', '', line)
262-
if re.match(r'^(WARN|ERROR)', line):
263-
self.log_error(index, 'WARNING', line)
264-
os.remove(rec_filepath)
260+
self.log_error(index, 'ERROR', msg)
261+
else:
262+
for line in output.split("\n")[:-1]:
263+
line = re.sub(r'^\s+', '', line)
264+
if re.match(r'^(WARN|ERROR)', line):
265+
self.log_error(index, 'WARNING', line)
266+
os.remove(rec_filepath)
265267

266268

267269
class SolrmarcEngine(BaseEngine):

0 commit comments

Comments
 (0)