Spaces:
Sleeping

PERDYPTO commited on
Commit
03f7c0e
·
verified ·
1 Parent(s): 7c2cca9

Update projects/dataset_verification/router.py

Browse files
projects/dataset_verification/router.py CHANGED
@@ -50,6 +50,7 @@ class Environment(BaseModel):
50
  platform: str
51
  os_version: str
52
  python_versions: List[str]
 
53
  notes: Optional[str] = None
54
 
55
  class WorkerRegisterRequest(BaseModel):
@@ -95,7 +96,7 @@ class ResultObject(BaseModel):
95
  network_activity_detected: Optional[bool] = None
96
 
97
  # ---------------------------------------------------------------------------
98
- # In‑Memory Store
99
  # ---------------------------------------------------------------------------
100
 
101
  class MemoryStore:
@@ -114,7 +115,6 @@ class MemoryStore:
114
  with self.lock:
115
  now = self.clock()
116
  worker = self.workers.get(worker_id)
117
-
118
  if worker is None:
119
  worker = {
120
  "worker_id": worker_id,
@@ -131,10 +131,9 @@ class MemoryStore:
131
  worker["last_seen_at"] = now
132
  worker["registered_at"] = now
133
  worker["reported_done"] = False
134
-
135
  return self._worker_public_locked(worker_id, now)
136
 
137
- # --- Polling -----------------------------------------------------------
138
  def poll_job(self, worker_id: int):
139
  with self.lock:
140
  worker = self.workers.get(worker_id)
@@ -142,17 +141,21 @@ class MemoryStore:
142
  raise KeyError("worker not registered")
143
 
144
  now = self.clock()
145
- # Update last_seen regardless
146
  worker["last_seen_at"] = now
147
 
148
- # Check liveness
149
  if worker.get("reported_done") or (now - worker["last_seen_at"]) > self.settings.liveness_window:
150
  return None
151
 
152
- # Check if this worker instance (by URL) is already busy
153
- for job in self.jobs.values():
154
- if job["status"] == "claimed" and job.get("claimed_by") == worker_id and job.get("claimed_worker_url") == worker["url"]:
155
- return None
 
 
 
 
 
 
156
 
157
  eligible = []
158
  for job in self.jobs.values():
@@ -179,7 +182,7 @@ class MemoryStore:
179
 
180
  return self._public_job(job)
181
 
182
- # --- Result Reporting ---------------------------------------------------
183
  def report_result(self, worker_id: int, url: str, result: ResultObject) -> None:
184
  with self.lock:
185
  job = self.jobs.get(result.job_id)
@@ -203,14 +206,12 @@ class MemoryStore:
203
  if worker is not None:
204
  worker["last_seen_at"] = now
205
 
206
- # --- Graceful Shutdown --------------------------------------------------
207
  def report_done(self, worker_id: int, url: str) -> List[str]:
208
  with self.lock:
209
  worker = self.workers.get(worker_id)
210
  if worker is None:
211
  return []
212
-
213
- # Ignore if the URL doesn't match current registration
214
  if worker["url"] != url:
215
  return []
216
 
@@ -223,10 +224,9 @@ class MemoryStore:
223
  if job["status"] == "claimed" and job["claimed_by"] == worker_id and job.get("claimed_worker_url") == url:
224
  self._abandon_claim_locked(job, "worker reported done", now)
225
  released.append(job["job_id"])
226
-
227
  return released
228
 
229
- # --- Job Submission -----------------------------------------------------
230
  def submit_job(self, job_data: JobSubmission) -> str:
231
  with self.lock:
232
  job_id = job_data.job_id or generate_job_id()
@@ -258,16 +258,14 @@ class MemoryStore:
258
  self.total_submitted += 1
259
  return job_id
260
 
261
- # --- Query Methods -------------------------------------------------------
262
  def get_job(self, job_id: str):
263
  with self.lock:
264
  job = self.jobs.get(job_id)
265
  if job is None:
266
  return None, None
267
-
268
  if job["status"] == "complete":
269
  return "complete", job["result"]
270
-
271
  matching = self._matching_worker_ids_locked(job)
272
  return "pending", {
273
  "retry_count": job["retry_count"],
@@ -325,6 +323,7 @@ class MemoryStore:
325
  "platform": w["environment"].get("platform"),
326
  "status": self._status_locked(wid, now),
327
  "claimed_jobs": claimed_by_worker.get(wid, 0),
 
328
  "last_seen_at": iso_from_ts(w["last_seen_at"]),
329
  "registered_at": iso_from_ts(w["registered_at"]),
330
  })
@@ -342,7 +341,7 @@ class MemoryStore:
342
  "claimed_by_worker": claimed_by_worker,
343
  }
344
 
345
- # --- Background Cleanup --------------------------------------------------
346
  def process_expired_claims(self) -> None:
347
  with self.lock:
348
  now = self.clock()
@@ -358,7 +357,7 @@ class MemoryStore:
358
  if (job["status"] == "complete" and job.get("completed_at") and (now - job["completed_at"]) > self.settings.retention_seconds):
359
  del self.jobs[job_id]
360
 
361
- # --- Internal Helpers ----------------------------------------------------
362
  def _abandon_claim_locked(self, job: dict, reason: str, now: float) -> None:
363
  if job.get("retry_count", 0) >= self.settings.max_retries:
364
  job["status"] = "complete"
@@ -433,7 +432,7 @@ class MemoryStore:
433
  }
434
 
435
  # ---------------------------------------------------------------------------
436
- # Global Store and Background Task
437
  # ---------------------------------------------------------------------------
438
 
439
  settings = Settings()
@@ -448,7 +447,7 @@ def _background_loop():
448
  threading.Thread(target=_background_loop, daemon=True).start()
449
 
450
  # ---------------------------------------------------------------------------
451
- # APIRouter Definition
452
  # ---------------------------------------------------------------------------
453
 
454
  router = APIRouter()
 
50
  platform: str
51
  os_version: str
52
  python_versions: List[str]
53
+ max_concurrent_jobs: int = 20 # NEW: advertised concurrency
54
  notes: Optional[str] = None
55
 
56
  class WorkerRegisterRequest(BaseModel):
 
96
  network_activity_detected: Optional[bool] = None
97
 
98
  # ---------------------------------------------------------------------------
99
+ # Memory Store
100
  # ---------------------------------------------------------------------------
101
 
102
  class MemoryStore:
 
115
  with self.lock:
116
  now = self.clock()
117
  worker = self.workers.get(worker_id)
 
118
  if worker is None:
119
  worker = {
120
  "worker_id": worker_id,
 
131
  worker["last_seen_at"] = now
132
  worker["registered_at"] = now
133
  worker["reported_done"] = False
 
134
  return self._worker_public_locked(worker_id, now)
135
 
136
+ # --- Polling with concurrency ------------------------------------------
137
  def poll_job(self, worker_id: int):
138
  with self.lock:
139
  worker = self.workers.get(worker_id)
 
141
  raise KeyError("worker not registered")
142
 
143
  now = self.clock()
 
144
  worker["last_seen_at"] = now
145
 
 
146
  if worker.get("reported_done") or (now - worker["last_seen_at"]) > self.settings.liveness_window:
147
  return None
148
 
149
+ # Count active claims by this worker instance (same URL)
150
+ active_claims = sum(
151
+ 1 for j in self.jobs.values()
152
+ if j["status"] == "claimed"
153
+ and j.get("claimed_by") == worker_id
154
+ and j.get("claimed_worker_url") == worker["url"]
155
+ )
156
+ max_concurrent = worker["environment"].get("max_concurrent_jobs", 1)
157
+ if active_claims >= max_concurrent:
158
+ return None
159
 
160
  eligible = []
161
  for job in self.jobs.values():
 
182
 
183
  return self._public_job(job)
184
 
185
+ # --- Result reporting ---------------------------------------------------
186
  def report_result(self, worker_id: int, url: str, result: ResultObject) -> None:
187
  with self.lock:
188
  job = self.jobs.get(result.job_id)
 
206
  if worker is not None:
207
  worker["last_seen_at"] = now
208
 
209
+ # --- Graceful shutdown --------------------------------------------------
210
  def report_done(self, worker_id: int, url: str) -> List[str]:
211
  with self.lock:
212
  worker = self.workers.get(worker_id)
213
  if worker is None:
214
  return []
 
 
215
  if worker["url"] != url:
216
  return []
217
 
 
224
  if job["status"] == "claimed" and job["claimed_by"] == worker_id and job.get("claimed_worker_url") == url:
225
  self._abandon_claim_locked(job, "worker reported done", now)
226
  released.append(job["job_id"])
 
227
  return released
228
 
229
+ # --- Job submission -----------------------------------------------------
230
  def submit_job(self, job_data: JobSubmission) -> str:
231
  with self.lock:
232
  job_id = job_data.job_id or generate_job_id()
 
258
  self.total_submitted += 1
259
  return job_id
260
 
261
+ # --- Query methods ------------------------------------------------------
262
  def get_job(self, job_id: str):
263
  with self.lock:
264
  job = self.jobs.get(job_id)
265
  if job is None:
266
  return None, None
 
267
  if job["status"] == "complete":
268
  return "complete", job["result"]
 
269
  matching = self._matching_worker_ids_locked(job)
270
  return "pending", {
271
  "retry_count": job["retry_count"],
 
323
  "platform": w["environment"].get("platform"),
324
  "status": self._status_locked(wid, now),
325
  "claimed_jobs": claimed_by_worker.get(wid, 0),
326
+ "max_concurrent_jobs": w["environment"].get("max_concurrent_jobs", 1),
327
  "last_seen_at": iso_from_ts(w["last_seen_at"]),
328
  "registered_at": iso_from_ts(w["registered_at"]),
329
  })
 
341
  "claimed_by_worker": claimed_by_worker,
342
  }
343
 
344
+ # --- Background tasks ---------------------------------------------------
345
  def process_expired_claims(self) -> None:
346
  with self.lock:
347
  now = self.clock()
 
357
  if (job["status"] == "complete" and job.get("completed_at") and (now - job["completed_at"]) > self.settings.retention_seconds):
358
  del self.jobs[job_id]
359
 
360
+ # --- Internal helpers ----------------------------------------------------
361
  def _abandon_claim_locked(self, job: dict, reason: str, now: float) -> None:
362
  if job.get("retry_count", 0) >= self.settings.max_retries:
363
  job["status"] = "complete"
 
432
  }
433
 
434
  # ---------------------------------------------------------------------------
435
+ # Global store and background loop
436
  # ---------------------------------------------------------------------------
437
 
438
  settings = Settings()
 
447
  threading.Thread(target=_background_loop, daemon=True).start()
448
 
449
  # ---------------------------------------------------------------------------
450
+ # APIRouter
451
  # ---------------------------------------------------------------------------
452
 
453
  router = APIRouter()