Skip to content

Commit 1ed9927

Browse files
authored
feat: delay auto onboarding to weekend and prevent them during weekdays [CM-1011] (#3886)
1 parent 216f183 commit 1ed9927

3 files changed

Lines changed: 45 additions & 20 deletions

File tree

services/apps/git_integration/src/crowdgit/database/crud.py

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import datetime
1+
from datetime import datetime, timezone
22

33
from loguru import logger
44
from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_fixed
@@ -151,6 +151,7 @@ async def acquire_recurrent_repo() -> Repository | None:
151151
RepositoryState.PENDING,
152152
RepositoryState.PROCESSING,
153153
RepositoryState.STUCK,
154+
RepositoryState.PENDING_REONBOARD,
154155
)
155156
return await acquire_repository(
156157
recurrent_repo_sql_query,
@@ -173,6 +174,39 @@ async def can_onboard_more():
173174
return False # if query failed mostly due to timeout then db is already under high load
174175

175176

177+
async def acquire_pending_reonboard_repo() -> Repository | None:
178+
"""Acquire a pending_reonboard repo for re-onboarding (only called on weekends)."""
179+
pending_reonboard_sql_query = f"""
180+
WITH selected_repo AS (
181+
SELECT r.id
182+
FROM public.repositories r
183+
JOIN git."repositoryProcessing" rp ON rp."repositoryId" = r.id
184+
WHERE rp.state = $1
185+
AND rp."lockedAt" IS NULL
186+
AND r."deletedAt" IS NULL
187+
ORDER BY rp.priority ASC, rp."lastProcessedAt" ASC
188+
LIMIT 1
189+
FOR UPDATE OF rp SKIP LOCKED
190+
)
191+
UPDATE git."repositoryProcessing" rp
192+
SET "lockedAt" = NOW(),
193+
state = $2,
194+
"lastProcessedCommit" = NULL,
195+
branch = NULL,
196+
"reOnboardingCount" = rp."reOnboardingCount" + 1,
197+
"updatedAt" = NOW()
198+
FROM public.repositories r
199+
CROSS JOIN selected_repo
200+
WHERE rp."repositoryId" = r.id
201+
AND rp."repositoryId" = selected_repo.id
202+
RETURNING {REPO_SELECT_COLUMNS}
203+
"""
204+
return await acquire_repository(
205+
pending_reonboard_sql_query,
206+
(RepositoryState.PENDING_REONBOARD, RepositoryState.PROCESSING),
207+
)
208+
209+
176210
async def acquire_repo_for_processing() -> Repository | None:
177211
"""
178212
Acquire the next repository to process based on priority and system load.
@@ -182,6 +216,8 @@ async def acquire_repo_for_processing() -> Repository | None:
182216
current onboarding count is below MAX_CONCURRENT_ONBOARDINGS
183217
2. Recurrent repos (non-PENDING/non-PROCESSING) - fallback when onboarding
184218
is unavailable or skipped due to high load
219+
3. Pending reonboard repos (PENDING_REONBOARD state) - weekend-only, lowest priority.
220+
These are repos needing re-onboarding that were deferred until the weekend.
185221
186222
Onboarding is delayed when integration.results exceeds MAX_INTEGRATION_RESULTS
187223
to prevent overloading the system during high activity periods.
@@ -192,6 +228,11 @@ async def acquire_repo_for_processing() -> Repository | None:
192228
else:
193229
logger.info("Skipping onboarding due to high load on integration.results")
194230

231+
if not repo_to_process:
232+
is_weekend = datetime.now(timezone.utc).weekday() >= 5
233+
if is_weekend:
234+
repo_to_process = await acquire_pending_reonboard_repo()
235+
195236
if not repo_to_process:
196237
repo_to_process = await acquire_recurrent_repo()
197238

@@ -227,16 +268,6 @@ async def update_last_processed_commit(repo_id: str, commit_hash: str, branch: s
227268
return str(result)
228269

229270

230-
async def increase_re_onboarding_count(repo_id: str):
231-
sql_query = """
232-
UPDATE git."repositoryProcessing"
233-
SET "reOnboardingCount" = "reOnboardingCount" + 1,
234-
"updatedAt" = NOW()
235-
WHERE "repositoryId" = $1
236-
"""
237-
return await execute(sql_query, (repo_id,))
238-
239-
240271
async def mark_repo_as_processed(repo_id: str, repo_state: RepositoryState):
241272
sql_query = """
242273
UPDATE git."repositoryProcessing"

services/apps/git_integration/src/crowdgit/enums.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class RepositoryState(str, Enum):
3333
FAILED = "failed"
3434
REQUIRES_PARENT = "requires_parent" # fork repo without valid parent repo in out system
3535
STUCK = "stuck" # requires manual resolution
36+
PENDING_REONBOARD = "pending_reonboard" # re-onboarding deferred until weekend
3637

3738

3839
class RepositoryPriority(int):

services/apps/git_integration/src/crowdgit/worker/repository_worker.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from crowdgit.database.crud import (
55
acquire_repo_for_processing,
66
get_recently_processed_repository_by_url,
7-
increase_re_onboarding_count,
87
mark_repo_as_processed,
98
release_repo,
109
update_last_processed_commit,
@@ -256,14 +255,8 @@ async def _process_single_repository(self, repository: Repository):
256255
)
257256
processing_state = RepositoryState.STUCK
258257
except ReOnboardingRequiredError:
259-
logger.info(f"Resetting and queueing {repository.url} for re-onboarding")
260-
await update_last_processed_commit(
261-
repo_id=repository.id,
262-
commit_hash=None,
263-
branch=None,
264-
)
265-
processing_state = RepositoryState.PENDING
266-
await increase_re_onboarding_count(repository.id)
258+
logger.info(f"Repo {repository.url} needs re-onboarding, deferring until weekend")
259+
processing_state = RepositoryState.PENDING_REONBOARD
267260
except ParentRepoInvalidError as e:
268261
logger.error(f"Parent repo validation failed: {repr(e)}")
269262
processing_state = RepositoryState.REQUIRES_PARENT

0 commit comments

Comments
 (0)