コード例 #1
0
 def insert_into_queue(self):
     # 创建时间
     created_time = from_now_to_datetime()
     item.setdefault("created_time", created_time)
     
     # 在waitting_t中加入请求
     obj_id = waitting_t.insert(**self.item)
     
     # 请求被排队
     # 日志表里插入created_time,并增加waitting_id
     # waitting_id是在waitting_t中获得的id
     # 用于在RunningPutter线程中更新processed_time
     # NOTICE: 目前使用uuid作为删除实例时更新deleted_time的条件
     # user_id用于查询
     created_time = time.time()
     backup_t.insert(user_id=self.user_id, created_time=created_time, waitting_id=obj_id,
                     instance_data=self.instance_data)
     
     # 将请求加入到等待队列
     # 2013/02/20: 将db的_objectid也压入队列
     # 在waitting_t中删除元素的时候,就根据_objectid删除
     # 而不是user_id
     item['_oid'] = obj_id
     waitting_queue.put(self.item)
     
     LOG.info("put data to waitting queue {0}: {1}".format(self.user_id, self.instance_data))
     result = waitting_t.query(_id=obj_id)
     
     # 返回排队的序号
     for line in result:
         auto_id =  line['auto_id']
         
     self.write(dict(status = auto_id))
     self.finish()
コード例 #2
0
def process_request(user_id, instance_data):
    '''
    处理用户的请求
    如果运行队列未满,则创建虚拟机实例、启动定时器、在running_t中插入记录
    否则将请求投入排队的队列中,并在waitting_t中插入记录
    '''
    item = dict(user_id=user_id, instance_data=instance_data)
    os = instance_data['os']

    # 如果运行队列未满
    if len(running_list) < options.running_dict_size:
        # 创建实例
        uuid = nova_api.create_vm(item)
        item['uuid'] = uuid

        # 为实例增加端口映射
        balancer = addNginxHost(uuid, os)
        balancer.start()

        # 实例加入到running_dict中
        insert_vm_in_mem(item)

        # 实例的到期时间
        expired_time = from_now_to_datetime(seconds=seconds)

        # 启动定时器
        timer = Timer(uuid=uuid,
                      item=item,
                      seconds=cal_seconds_to(expired_time))
        timer.start()
        timer_list.append(timer)

        # 在running_t中插入一条记录
        obj_id = running_t.insert(uuid=uuid,
                                  user_id=user_id,
                                  expired_time=expired_time,
                                  instance_data=instance_data)

        # 请求被立即处理
        # 日志表里插入created_time和processed_time
        # 并增加running_id,用于删除实例时更新deleted_time
        # user_id用于查询用
        processed_time = time.time()
        backup_t.insert(user_id=user_id,
                        created_time=processed_time,
                        processed_time=processed_time,
                        instance_data=instance_data,
                        running_id=obj_id,
                        uuid=uuid)
        LOG.info("Start timer for {0}: {1}".format(user_id, instance_data))
        # 返回0
        return 0
    else:
        # 创建时间
        created_time = from_now_to_datetime()
        item.setdefault("created_time", created_time)

        # 在waitting_t中加入请求
        obj_id = waitting_t.insert(**item)

        # 请求被排队
        # 日志表里插入created_time,并增加waitting_id
        # waitting_id是在waitting_t中获得的id
        # 用于在RunningPutter线程中更新processed_time
        # NOTICE: 目前使用uuid作为删除实例时更新deleted_time的条件
        # user_id用于查询
        created_time = time.time()
        backup_t.insert(user_id=user_id,
                        created_time=created_time,
                        waitting_id=obj_id,
                        instance_data=instance_data)

        # 将请求加入到等待队列
        # 2013/02/20: 将db的_objectid也压入队列
        # 在waitting_t中删除元素的时候,就根据_objectid删除
        # 而不是user_id
        item['_oid'] = obj_id
        waitting_queue.put(item)

        LOG.info("put data to waitting queue {0}: {1}".format(
            user_id, instance_data))
        result = waitting_t.query(_id=obj_id)

        # 返回排队的序号
        for line in result:
            return line['auto_id']
コード例 #3
0
def process_request(user_id, instance_data):
    '''
    处理用户的请求
    如果运行队列未满,则创建虚拟机实例、启动定时器、在running_t中插入记录
    否则将请求投入排队的队列中,并在waitting_t中插入记录
    '''
    item = dict(user_id=user_id, instance_data=instance_data)
    os = instance_data['os']
    
    # 如果运行队列未满
    if len(running_list) < options.running_dict_size:
        # 创建实例
        uuid = nova_api.create_vm(item)
        item['uuid'] = uuid
        
        # 为实例增加端口映射
        balancer = addNginxHost(uuid, os)
        balancer.start()
        
        # 实例加入到running_dict中
        insert_vm_in_mem(item)
        
        # 实例的到期时间
        expired_time = from_now_to_datetime(seconds=seconds)
        
        # 启动定时器
        timer = Timer(uuid=uuid, item=item,
                      seconds=cal_seconds_to(expired_time))
        timer.start()
        timer_list.append(timer)
        
        # 在running_t中插入一条记录
        obj_id = running_t.insert(uuid=uuid, user_id=user_id,
                         expired_time=expired_time, instance_data=instance_data)
        
        # 请求被立即处理
        # 日志表里插入created_time和processed_time
        # 并增加running_id,用于删除实例时更新deleted_time
        # user_id用于查询用
        processed_time = time.time()
        backup_t.insert(user_id=user_id, created_time=processed_time, processed_time=processed_time,
                        instance_data=instance_data, running_id=obj_id, uuid=uuid)
        LOG.info("Start timer for {0}: {1}".format(user_id, instance_data))
        # 返回0
        return 0
    else:
        # 创建时间
        created_time = from_now_to_datetime()
        item.setdefault("created_time", created_time)
        
        # 在waitting_t中加入请求
        obj_id = waitting_t.insert(**item)
        
        # 请求被排队
        # 日志表里插入created_time,并增加waitting_id
        # waitting_id是在waitting_t中获得的id
        # 用于在RunningPutter线程中更新processed_time
        # NOTICE: 目前使用uuid作为删除实例时更新deleted_time的条件
        # user_id用于查询
        created_time = time.time()
        backup_t.insert(user_id=user_id, created_time=created_time, waitting_id=obj_id,
                        instance_data=instance_data)
        
        # 将请求加入到等待队列
        # 2013/02/20: 将db的_objectid也压入队列
        # 在waitting_t中删除元素的时候,就根据_objectid删除
        # 而不是user_id
        item['_oid'] = obj_id
        waitting_queue.put(item)
        
        LOG.info("put data to waitting queue {0}: {1}".format(user_id, instance_data))
        result = waitting_t.query(_id=obj_id)
        
        # 返回排队的序号
        for line in result:
            return line['auto_id']