Skip to content

Commit

Permalink
fix: broken reports folder on remote workers (#307)
Browse files Browse the repository at this point in the history
Fixes #301
  • Loading branch information
ocervell authored Apr 17, 2024
1 parent 040cfaf commit 9a7a1f1
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
4 changes: 2 additions & 2 deletions secator/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,6 @@ def is_celery_worker_alive():
result = bool(result)
if result:
console.print('Celery worker is alive !', style='bold green')
# else:
# console.print('No Celery worker alive.', style='bold red')
else:
console.print('No Celery worker alive.', style='bold orange1')
return result
25 changes: 16 additions & 9 deletions secator/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

RUNNER_GLOBAL_OPTS = {
'sync': {'is_flag': True, 'help': 'Run tasks synchronously (automatic if no worker is alive)'},
'worker': {'is_flag': True, 'help': 'Run tasks in worker (automatic if worker is alive)'},
'proxy': {'type': str, 'help': 'HTTP proxy'},
'driver': {'type': str, 'help': 'Export real-time results. E.g: "mongodb"'}
# 'debug': {'type': int, 'default': 0, 'help': 'Debug mode'},
Expand Down Expand Up @@ -264,7 +263,6 @@ def register_runner(cli_endpoint, config):
def func(ctx, **opts):
opts.update(fmt_opts)
sync = opts['sync']
worker = opts['worker']
# debug = opts['debug']
ws = opts.pop('workspace')
driver = opts.pop('driver', '')
Expand All @@ -275,13 +273,22 @@ def func(ctx, **opts):
# opts.update(unknown_opts)
targets = opts.pop(input_type)
targets = expand_input(targets)
if sync or show or not ADDONS_ENABLED['worker']:
if sync or show:
sync = True
elif worker:
sync = False
else: # automatically run in worker if it's alive
else:
from secator.celery import is_celery_worker_alive
sync = not is_celery_worker_alive()
worker_alive = is_celery_worker_alive()
if not worker_alive:
sync = True
else:
sync = False
from secator.definitions import CELERY_BROKER_URL, CELERY_RESULT_BACKEND
broker_protocol = CELERY_BROKER_URL.split('://')[0]
backend_protocol = CELERY_RESULT_BACKEND.split('://')[0]
if CELERY_BROKER_URL:
if (broker_protocol == 'redis' or backend_protocol == 'redis') and not ADDONS_ENABLED['redis']:
_get_rich_console().print('[bold red]Missing `redis` addon: please run `secator install addons redis`[/].')
sys.exit(1)
opts['sync'] = sync
opts.update({
'print_item': not sync,
Expand All @@ -293,8 +300,8 @@ def func(ctx, **opts):
# Build hooks from driver name
hooks = {}
if driver == 'mongodb':
if not ADDONS_ENABLED['mongo']:
_get_rich_console().print('[bold red]Missing MongoDB dependencies: please run `secator install addons mongodb`[/].')
if not ADDONS_ENABLED['mongodb']:
_get_rich_console().print('[bold red]Missing `mongodb` addon: please run `secator install addons mongodb`[/].')
sys.exit(1)
from secator.hooks.mongodb import MONGODB_HOOKS
hooks = MONGODB_HOOKS
Expand Down
3 changes: 1 addition & 2 deletions secator/runners/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ def __init__(self, config, targets, results=[], run_opts={}, hooks={}, context={
# Determine report folder
default_reports_folder_base = f'{REPORTS_FOLDER}/{self.workspace_name}/{self.config.type}s'
_id = get_task_folder_id(default_reports_folder_base)
default_report_folder = f'{default_reports_folder_base}/{_id}'
self.reports_folder = run_opts.get('reports_folder') or default_report_folder
self.reports_folder = f'{default_reports_folder_base}/{_id}'

# Make reports folders
os.makedirs(self.reports_folder, exist_ok=True)
Expand Down
1 change: 0 additions & 1 deletion secator/runners/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def yielder(self):

# Workflow opts
run_opts = self.run_opts.copy()
run_opts['reports_folder'] = self.reports_folder
fmt_opts = {
'json': run_opts.get('json', False),
'print_item': False,
Expand Down
1 change: 0 additions & 1 deletion secator/runners/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def yielder(self):
hooks = {task_cls: self.hooks}
run_opts['hooks'] = hooks
run_opts['context'] = self.context
run_opts['reports_folder'] = self.reports_folder

# Run task
if self.sync:
Expand Down
1 change: 0 additions & 1 deletion secator/runners/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def yielder(self):

# Construct run opts
task_run_opts['hooks'] = self._hooks.get(Task, {})
task_run_opts['reports_folder'] = self.reports_folder
task_run_opts.update(task_fmt_opts)

# Build Celery workflow
Expand Down

0 comments on commit 9a7a1f1

Please sign in to comment.