Skip to content

Commit 226a7ad

Browse files
committed
run importers in parallel
Signed-off-by: ziad <[email protected]>
1 parent c94ed57 commit 226a7ad

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

vulnerabilities/management/commands/import.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# See https://github.com/nexB/vulnerablecode for support or download.
77
# See https://aboutcode.org for more information about nexB OSS projects.
88
#
9-
9+
import threading
1010
import traceback
1111

1212
from django.core.management.base import BaseCommand
@@ -27,6 +27,10 @@ def add_arguments(self, parser):
2727
)
2828
parser.add_argument("--all", action="store_true", help="Run all available importers")
2929

30+
parser.add_argument(
31+
"--all_in_parallel", action="store_true", help="Run all available importers in parallel"
32+
)
33+
3034
parser.add_argument("sources", nargs="*", help="Fully qualified importer name to run")
3135

3236
def handle(self, *args, **options):
@@ -38,6 +42,10 @@ def handle(self, *args, **options):
3842
self.import_data(IMPORTERS_REGISTRY.values())
3943
return
4044

45+
if options["all_in_parallel"]:
46+
self.import_data_in_parallel(IMPORTERS_REGISTRY.values())
47+
return
48+
4149
sources = options["sources"]
4250
if not sources:
4351
raise CommandError('Please provide at least one importer to run or use "--all".')
@@ -77,6 +85,36 @@ def import_data(self, importers):
7785
if failed_importers:
7886
raise CommandError(f"{len(failed_importers)} failed!: {','.join(failed_importers)}")
7987

88+
def import_data_in_parallel(self, importers):
89+
failed_importers = []
90+
thread_list = []
91+
for importer in importers:
92+
self.stdout.write(f"Importing data using {importer.qualified_name}")
93+
try:
94+
thread = threading.Thread(
95+
target=ImportRunner(importer).run(), name=importer.qualified_name
96+
)
97+
thread.start()
98+
thread_list.append(thread)
99+
except Exception:
100+
failed_importers.append(importer.qualified_name)
101+
traceback.print_exc()
102+
self.stdout.write(
103+
self.style.ERROR(
104+
f"Failed to run importer {importer.qualified_name}. Continuing..."
105+
)
106+
)
107+
for thread in thread_list:
108+
thread.join()
109+
110+
success_list = {value for value in IMPORTERS_REGISTRY if value not in failed_importers}
111+
if success_list:
112+
self.stdout.write(
113+
self.style.SUCCESS(f"Successfully imported data using {success_list} ")
114+
)
115+
if failed_importers:
116+
raise CommandError(f"{len(failed_importers)} failed!: {','.join(failed_importers)}")
117+
80118

81119
def validate_importers(sources):
82120
importers = []

0 commit comments

Comments
 (0)