fix: Move set/unset running to abstract

Avoid issues when a script fails unexpectedly.
pull/197/head
Raphaël Vinot 2021-04-09 14:33:40 +02:00
parent 9470b0c738
commit f865ec912a
3 changed files with 7 additions and 7 deletions

View File

@ -4,7 +4,7 @@
import logging
from lookyloo.abstractmanager import AbstractManager
from lookyloo.helpers import set_running, unset_running, shutdown_requested
from lookyloo.helpers import shutdown_requested
from lookyloo.lookyloo import Lookyloo
logging.basicConfig(format='%(asctime)s %(name)s %(levelname)s:%(message)s',
@ -16,14 +16,13 @@ class AsyncCapture(AbstractManager):
def __init__(self, loglevel: int=logging.INFO):
super().__init__(loglevel)
self.lookyloo = Lookyloo()
self.script_name = 'async_capture'
def _to_run_forever(self):
set_running('async_capture')
while True:
url = self.lookyloo.process_capture_queue()
if url is None or shutdown_requested():
break
unset_running('async_capture')
def main():

View File

@ -4,7 +4,6 @@
import logging
from lookyloo.abstractmanager import AbstractManager
from lookyloo.helpers import set_running, unset_running
from lookyloo.lookyloo import Lookyloo
from lookyloo.exceptions import NoValidHarFile
@ -17,15 +16,14 @@ class BackgroundIndexer(AbstractManager):
def __init__(self, loglevel: int=logging.INFO):
super().__init__(loglevel)
self.lookyloo = Lookyloo()
self.script_name = 'background_indexer'
# make sure discarded captures dir exists
self.discarded_captures_dir = self.lookyloo.capture_dir.parent / 'discarded_captures'
self.discarded_captures_dir.mkdir(parents=True, exist_ok=True)
def _to_run_forever(self):
set_running('background_indexer')
self._build_missing_pickles()
self._check_indexes()
unset_running('background_indexer')
def _build_missing_pickles(self):
for uuid_path in self.lookyloo.capture_dir.glob('*/uuid'):

View File

@ -4,7 +4,7 @@
from abc import ABC
import logging
from .helpers import long_sleep, shutdown_requested
from .helpers import long_sleep, shutdown_requested, set_running, unset_running
class AbstractManager(ABC):
@ -27,9 +27,12 @@ class AbstractManager(ABC):
if shutdown_requested():
break
try:
set_running(self.script_name)
self._to_run_forever()
except Exception:
self.logger.exception(f'Something went terribly wrong in {self.__class__.__name__}.')
finally:
unset_running(self.script_name)
if not long_sleep(sleep_in_sec):
break
self.logger.info(f'Shutting down {self.__class__.__name__}')