Skip to content

Commit

Permalink
pyDKB/cds: more accurate handling of with...as
Browse files Browse the repository at this point in the history
There`s no guarantee that after `with...as` clause the main program
  is immediately goes to its end, so we need to propagate
  all the exceptions, including KeyboardInterrupt.

As we exit the `with...as` clause, we need to destroy the browser
  process. If there was an exception propagated, `__del__()` method
  is not called (at least before the end of the main program),
  so we need call `delete()` manually.

To ensure that phantomjs received the SIGTERM, we send it manually
  in the `delete()`. It doesn`t seem to help with Issue #45, however.
  • Loading branch information
mgolosova committed Oct 28, 2017
1 parent 8174a70 commit 1513ee9
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions Utils/Dataflow/pyDKB/dataflow/cds.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
__all__ = ["CDSInvenioConnector", "KerberizedCDSInvenioConnector"]

import sys
import signal

try:
import kerberos
Expand All @@ -26,11 +27,20 @@ def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
if self.browser:
""" Propagate exceptions after with...as construction. """
self.delete()
if exc_type:
return False
return True

def __del__(self):
self.delete()

def delete(self):
if getattr(self, 'browser', None):
self.browser.driver.service.process.send_signal(signal.SIGTERM)
self.browser.quit()
if isinstance(exc_val, KeyboardInterrupt):
return True
return False
del self.browser

class KerberizedCDSInvenioConnector(CDSInvenioConnector):
"""
Expand Down

0 comments on commit 1513ee9

Please sign in to comment.