def run(self): ''' 暂停指定的时间,到时间后执行一些列动作 ''' time.sleep(self.seconds) result = running_t.query_one(uuid=self.uuid) if result: if cond.acquire(): # 从running_dict中删除用户 try: del_vm_in_mem(self.uuid) # nginx负载均衡中删除主机 del_nginx_host(self.uuid) except Exception, e: LOG.exception(e) # 从running_t中删除用户 running_t.delete(uuid=self.uuid) # 调用openstack删除主机 nova_api.delete_vm(self.uuid) # 更新backup_t中的deleted_time deleted_time = time.time() backup_t.update(dict(uuid=self.uuid), deleted_time=deleted_time) # 通知排队线程 cond.notify() cond.release() return
def rebuild_timer_and_queue(): ''' 服务器重启时,从mongodb中读取记录并恢复定时器和队列 启动固定数量的timer 并将等待的请求放入到等待队列中 ''' LOG.info("Rebuild timer and waitting queue") running_timer = running_t.query() waitters = waitting_t.query() for line in running_timer: if line.has_key("instance_data"): expired_time = line['expired_time'] seconds = cal_seconds_to(expired_time) uuid = line['uuid'] user_id = line['user_id'] instance_data = line['instance_data'] _oid = line['_id'] # 定时器已经到期,立即删除运行的实例 if seconds < 0: try: nova_api.delete_vm(uuid) except: pass running_t.delete(uuid=uuid) # 更新backup_t中的deleted_time deleted_time = time.time() backup_t.update(dict(uuid=uuid), deleted_time=deleted_time) else: # 恢复running_dict和定时器 item = dict(user_id=user_id, instance_data=instance_data) item['uuid'] = uuid # 2013/03/01 # 增加_oid到元素,RunningPutter可以根据此_oid删除waitting_t中的元素 # 其实从mongodb中取得的记录已包含_id # 此处为了加强重点,重复加入 item['_oid'] = _oid insert_vm_in_mem(item) timer = Timer(uuid=uuid, seconds=seconds, item=item) timer.start() timer_list.append(timer) for line in waitters: if line.has_key("instance_data"): LOG.info("insert data to waitting queue: %s" % line) user_id = line['user_id'] item = dict(_oid=line['_id'], user_id=user_id, instance_data=instance_data) waitting_queue.put(item) LOG.info("Rebuild end")
def run(self): if cond.acquire(): del_vm_in_mem(self.uuid) # 当调用cloudopen接口处理失败时,调用tornado返回500 try: nova_api.delete_vm(self.uuid) except Exception, e: LOG.exception(e) running_t.delete(uuid=self.uuid) # 更新backup_t中的deleted_time deleted_time = time.time() backup_t.update(dict(uuid=self.uuid), deleted_time=deleted_time) self.obj.send_error(500) return else: try: del_nginx_host(self.uuid) except Exception, e: LOG.exception(e)
def run(self): if cond.acquire(): del_vm_in_mem(self.uuid) # 当调用cloudopen接口处理失败时,调用tornado返回404 try: del_nginx_host(self.uuid) nova_api.delete_vm(self.uuid) except Exception, e: LOG.exception(e) self.obj.send_error(500) running_t.delete(uuid=self.uuid) # 更新backup_t中的deleted_time deleted_time = time.time() backup_t.update(dict(uuid=self.uuid), deleted_time=deleted_time) # 当全部处理成功后,调用tornado返回成功状态 self.obj.write({"status": 0}) tornado.ioloop.IOLoop.instance().add_callback(self.obj.on_write) cond.notify() cond.release() return