1- from datetime import datetime
1+ from datetime import datetime , timezone
22
33from loguru import logger
44from 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+
176210async 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-
240271async def mark_repo_as_processed (repo_id : str , repo_state : RepositoryState ):
241272 sql_query = """
242273 UPDATE git."repositoryProcessing"
0 commit comments