MCPcopy
hub / github.com/hyperopt/hyperopt / reserve

Method reserve

hyperopt/mongoexp.py:483–522  ·  view source on GitHub ↗
(self, host_id, cond=None, exp_key=None)

Source from the content-addressed store, hash-verified

481 return self.delete_all(cond={"state": JOB_STATE_ERROR})
482
483 def reserve(self, host_id, cond=None, exp_key=None):
484 now = coarse_utcnow()
485 if cond is None:
486 cond = {}
487 else:
488 cond = copy.copy(
489 cond
490 ) # copy is important, will be modified, but only the top-level
491
492 if exp_key is not None:
493 cond["exp_key"] = exp_key
494
495 # having an owner of None implies state==JOB_STATE_NEW, so this effectively
496 # acts as a filter to make sure that only new jobs get reserved.
497 if cond.get("owner") is not None:
498 raise ValueError("refusing to reserve owned job")
499 else:
500 cond["owner"] = None
501 cond[
502 "state"
503 ] = JOB_STATE_NEW # theoretically this is redundant, theoretically
504
505 try:
506 rval = self.jobs.find_and_modify(
507 cond,
508 {
509 "$set": {
510 "owner": host_id,
511 "book_time": now,
512 "state": JOB_STATE_RUNNING,
513 "refresh_time": now,
514 }
515 },
516 new=True,
517 upsert=False,
518 )
519 except pymongo.errors.OperationFailure as e:
520 logger.error("Error during reserve_job: %s" % str(e))
521 rval = None
522 return rval
523
524 def refresh(self, doc):
525 self.update(doc, dict(refresh_time=coarse_utcnow()))

Callers 1

run_oneMethod · 0.80

Calls 3

coarse_utcnowFunction · 0.85
getMethod · 0.80
errorMethod · 0.80

Tested by

no test coverage detected