linxinhua commited on
Commit
79a7065
·
verified ·
1 Parent(s): ddb0df6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -12
app.py CHANGED
@@ -31,6 +31,14 @@ def init_data_storage_connection():
31
  print(f"[init_data_storage_connection] Pulling latest data from {DATA_STORAGE_REPO}...")
32
  repo.git_pull(rebase=True)
33
 
 
 
 
 
 
 
 
 
34
  print(f"[init_data_storage_connection] Successfully connected to data storage")
35
  print(f"[init_data_storage_connection] Local directory: {LOCAL_DATA_DIR}")
36
  print(f"[init_data_storage_connection] Branch: {DATA_BRANCH_NAME}")
@@ -211,17 +219,53 @@ def write_allocation_record(space_name, student_id, repo_dir):
211
  return False
212
 
213
  def commit_allocation_record(repo, space_name, student_id):
214
- """Commit the allocation record to Git"""
215
- try:
216
- repo.git_add(ALLOCATION_RECORD_FILE)
217
- commit_message = f"Allocate {space_name} to student {student_id}"
218
- repo.git_commit(commit_message)
219
- repo.git_push()
220
- print(f"[commit_allocation_record] Successfully committed allocation record")
221
- return True
222
- except Exception as e:
223
- print(f"[commit_allocation_record] Error committing allocation record: {e}")
224
- return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
 
226
  def analyze_space_activity(available_spaces, repo_dir):
227
  """Analyze activity status of all spaces and return sorted list"""
@@ -331,9 +375,12 @@ def select_space_with_boundary_check(space_activity, student_id, repo):
331
  # Record the allocation BEFORE building the response
332
  write_success = write_allocation_record(selected_space['space_name'], student_id, repo.local_dir)
333
  if write_success:
334
- commit_allocation_record(repo, selected_space['space_name'], student_id)
 
 
335
  else:
336
  print(f"[select_space_with_boundary_check] Warning: Failed to record allocation")
 
337
 
338
  # Create status display
339
  status_display = create_status_display(space_activity)
 
31
  print(f"[init_data_storage_connection] Pulling latest data from {DATA_STORAGE_REPO}...")
32
  repo.git_pull(rebase=True)
33
 
34
+ # Configure Git user identity immediately after initialization
35
+ try:
36
+ repo.git.config("user.email", "load-distributor@civ3283.system")
37
+ repo.git.config("user.name", "CIV3283 Load Distributor")
38
+ print(f"[init_data_storage_connection] Configured Git identity")
39
+ except Exception as config_error:
40
+ print(f"[init_data_storage_connection] Warning: Could not configure Git identity: {config_error}")
41
+
42
  print(f"[init_data_storage_connection] Successfully connected to data storage")
43
  print(f"[init_data_storage_connection] Local directory: {LOCAL_DATA_DIR}")
44
  print(f"[init_data_storage_connection] Branch: {DATA_BRANCH_NAME}")
 
219
  return False
220
 
221
  def commit_allocation_record(repo, space_name, student_id):
222
+ """Commit the allocation record to Git with retry mechanism"""
223
+ max_retries = 3
224
+
225
+ for attempt in range(max_retries):
226
+ try:
227
+ # Ensure Git identity is configured (redundant check)
228
+ try:
229
+ repo.git.config("user.email", "load-distributor@civ3283.system")
230
+ repo.git.config("user.name", "CIV3283 Load Distributor")
231
+ except:
232
+ pass # Ignore config errors if already set
233
+
234
+ # Pull latest changes before committing
235
+ repo.git_pull(rebase=True)
236
+
237
+ # Stage and commit the allocation file
238
+ repo.git_add(ALLOCATION_RECORD_FILE)
239
+
240
+ # Check if there are changes to commit
241
+ try:
242
+ status_output = repo.git.status("--porcelain")
243
+ if not status_output.strip():
244
+ print(f"[commit_allocation_record] No changes to commit")
245
+ return True # Consider this successful
246
+ except:
247
+ pass # Continue with commit attempt
248
+
249
+ commit_message = f"Allocate {space_name} to student {student_id}"
250
+ repo.git_commit(commit_message)
251
+ repo.git_push()
252
+
253
+ print(f"[commit_allocation_record] Successfully committed allocation record (attempt {attempt + 1})")
254
+ return True
255
+
256
+ except Exception as e:
257
+ print(f"[commit_allocation_record] Attempt {attempt + 1} failed: {e}")
258
+
259
+ if attempt < max_retries - 1:
260
+ print(f"[commit_allocation_record] Retrying...")
261
+ import time
262
+ time.sleep(1) # Wait 1 second before retry
263
+ else:
264
+ print(f"[commit_allocation_record] All attempts failed. Allocation recorded locally but not synced to remote.")
265
+ # Even if Git sync fails, the local allocation record still prevents immediate conflicts
266
+ return False
267
+
268
+ return False
269
 
270
  def analyze_space_activity(available_spaces, repo_dir):
271
  """Analyze activity status of all spaces and return sorted list"""
 
375
  # Record the allocation BEFORE building the response
376
  write_success = write_allocation_record(selected_space['space_name'], student_id, repo.local_dir)
377
  if write_success:
378
+ commit_success = commit_allocation_record(repo, selected_space['space_name'], student_id)
379
+ if not commit_success:
380
+ print(f"[select_space_with_boundary_check] Warning: Allocation recorded locally but not synced to remote")
381
  else:
382
  print(f"[select_space_with_boundary_check] Warning: Failed to record allocation")
383
+ # Continue anyway - worst case is potential double allocation, but user gets service
384
 
385
  # Create status display
386
  status_display = create_status_display(space_activity)