Skip to content

Commit 4555b2b

Browse files
committed
Changed sync scopes to run in parallel
1 parent 54bca38 commit 4555b2b

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

packages/opal-server/opal_server/scopes/service.py

+28-28
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import asyncio
12
import datetime
23
import shutil
34
from functools import partial
45
from pathlib import Path
5-
from typing import List, Optional, Set, cast
6+
from typing import List, Optional, cast
67

78
import git
89
from ddtrace import tracer
@@ -196,32 +197,31 @@ async def sync_scopes(self, only_poll_updates=False, notify_on_changes=True):
196197

197198
fetched_source_ids = set()
198199
skipped_scopes = []
199-
for scope in scopes:
200-
src_id = GitPolicyFetcher.source_id(scope.policy)
201-
202-
# Give priority to scopes that have a unique url per shard (so we'll clone all repos asap)
203-
if src_id in fetched_source_ids:
204-
skipped_scopes.append(scope)
205-
continue
206-
207-
try:
208-
await self.sync_scope(
209-
scope=scope,
210-
force_fetch=True,
211-
notify_on_changes=notify_on_changes,
200+
async with asyncio.TaskGroup() as g:
201+
for scope in scopes:
202+
src_id = GitPolicyFetcher.source_id(scope.policy)
203+
204+
# Give priority to scopes that have a unique url per shard (so we'll clone all repos asap)
205+
if src_id in fetched_source_ids:
206+
skipped_scopes.append(scope)
207+
continue
208+
209+
g.create_task(
210+
self.sync_scope(
211+
scope=scope,
212+
force_fetch=True,
213+
notify_on_changes=notify_on_changes,
214+
)
212215
)
213-
except Exception as e:
214-
logger.exception(f"sync_scope failed for {scope.scope_id}")
215-
216-
fetched_source_ids.add(src_id)
217-
218-
for scope in skipped_scopes:
219-
# No need to refetch the same repo, just check for changes
220-
try:
221-
await self.sync_scope(
222-
scope=scope,
223-
force_fetch=False,
224-
notify_on_changes=notify_on_changes,
216+
fetched_source_ids.add(src_id)
217+
218+
async with asyncio.TaskGroup() as g:
219+
for scope in skipped_scopes:
220+
# No need to refetch the same repo, just check for changes
221+
g.create_task(
222+
self.sync_scope(
223+
scope=scope,
224+
force_fetch=False,
225+
notify_on_changes=notify_on_changes,
226+
)
225227
)
226-
except Exception as e:
227-
logger.exception(f"sync_scope failed for {scope.scope_id}")

0 commit comments

Comments
 (0)