def _acquire_lock(self): LOCK_TIMEOUT = timedelta(minutes=120) WAIT_STEP = 15 vms = Internals.get(name='virtual_machines') if vms is None: vms = Internals({'name': 'virtual_machines'}) vms.save() locked_vm = False while not locked_vm: for i, label in enumerate(self.labels): self._use_vm(i) last_locked = "{}.last_locked".format(self.vm_record) if vms.update_value([self.vm_record, 'locked'], True): vms.update_value([self.vm_record, 'last_locked'], datetime.now()) locked_vm = True break expired_date = datetime.now() - LOCK_TIMEOUT if vms._update({'$set': { last_locked: datetime.now() }}, {last_locked: { '$lt': expired_date }}): vms.update_value([self.vm_record, 'locked'], True) locked_vm = True break if not locked_vm: sleep(WAIT_STEP)
def _acquire_lock(self): LOCK_TIMEOUT = timedelta(minutes=120) WAIT_STEP = 15 vms = Internals.get(name='virtual_machines') if vms is None: vms = Internals({'name': 'virtual_machines'}) vms.save() last_locked = "{}.last_locked".format(self.vm_record) while True: if vms.update_value([self.vm_record, 'locked'], True): vms.update_value([self.vm_record, 'last_locked'], datetime.now()) break expired_date = datetime.now() - LOCK_TIMEOUT if vms._update({'$set': { last_locked: datetime.now() }}, {last_locked: { '$lt': expired_date }}): vms.update_value([self.vm_record, 'locked'], True) break sleep(WAIT_STEP)
def create_internals(): updates = Internals.get(name='updates') if updates is None: updates = Internals({ 'name': 'updates', 'last_update': time() }) updates.save()