Example #1
0
def add_task(server, json):
    """
    
    :param json:str类型,表示要添加任务的详细信息 
    :return: list,表示添加的任务ids
    """
    json = eval(json)
    ids = []

    for task in json.keys():

        #电商meta中加入this_url_rule
        this_meta = {}
        if json[task]["type"] == 1:
            this_url_rule = ''
            if json[task].has_key("rules") and json[task]["rules"]:
                for rule in json[task]["rules"].keys():
                    if re.compile(rule).match(task):
                        this_url_rule = rule
                        break
            this_meta["this_url_rule"] = this_url_rule

        json[task]["url"] = task

        with dist_lock(BOT_NAME, server):
            #获取当前任务id号
            pipe = server.pipeline()
            pipe.multi()
            pipe.zcard('%s:running_task' % BOT_NAME)
            pipe.zcard('%s:pausing_task' % BOT_NAME)
            pipe.zcard('%s:done_canceled_task' % BOT_NAME)
            id = sum(pipe.execute()) + 1
            this_meta["id"] = id

            #生成request
            req = request_to_dict(
                Request(task, meta=this_meta, priority=json[task]["priority"]))
            print req
            req = picklecompat.dumps(req)

            #加入到对应task_X队列(zset)、task_information队列(hash)、running_task队列(zset)
            server.execute_command('ZADD', '%s:task_%s' % (BOT_NAME, id),
                                   -json[task]["priority"], req)
            server.hset('%s:task_information' % BOT_NAME, id, json[task])
            server.zadd('%s:running_task' % BOT_NAME, -json[task]["priority"],
                        id)
        ids.append(id)
        # time.sleep(3)
    return ids
Example #2
0
def pause_task(server, id):
    with dist_lock(BOT_NAME, server):
        #查找id对应的优先级
        score = server.zscore('%s:running_task' % BOT_NAME, id)
        if not score:
            return False

        #从running_task中删除
        if server.zrem('%s:running_task' % BOT_NAME, id) != 0:
            #加入到pausing_task中
            if server.zadd('%s:pausing_task' % BOT_NAME, score, id) != 0:
                return True
            else:
                return False
        else:
            return False
Example #3
0
def list_task(server):
    with dist_lock(BOT_NAME, server):
        all_task_information = server.hgetall('%s:task_information' %
                                              BOT_NAME)  #返回dict类型,键值都是str类型
        all_task_information_eval = {}
        for task_information_id in all_task_information:
            task_information_eval = eval(
                all_task_information[task_information_id])

            if server.zscore('%s:running_task' % BOT_NAME,
                             task_information_id):
                task_information_eval['status'] = 1  #正在跑
            elif server.zscore('%s:pausing_task' % BOT_NAME,
                               task_information_id):
                task_information_eval['status'] = 2  #暂停
            elif server.zscore('%s:done_canceled_task' % BOT_NAME,
                               task_information_id):
                task_information_eval['status'] = 3  #取消或完成

            all_task_information_eval[
                task_information_id] = task_information_eval

    return all_task_information_eval
Example #4
0
def cancel_task(server, id):
    with dist_lock(BOT_NAME, server):
        # 查score
        score = server.zscore('%s:running_task' % BOT_NAME, id)
        if not score:
            score = server.zscore('%s:pausing_task' % BOT_NAME, id)
        if not score:
            return False

        #从running_task中删除
        if server.zrem('%s:running_task' % BOT_NAME, id) != 0:
            #加入到done_canceled_task中
            if server.zadd('%s:done_canceled_task' % BOT_NAME, score, id) != 0:
                #删除对应的task队列
                time.sleep(2)
                if server.delete('%s:task_%s' % (BOT_NAME, id)):
                    return True
                else:
                    return False
            else:
                return False
        # 从pausing_task中删除
        elif server.zrem('%s:pausing_task' % BOT_NAME, id) != 0:
            #加入到done_canceled_task中
            if server.zadd('%s:done_canceled_task' % BOT_NAME, score, id) != 0:
                # 删除对应的task队列
                time.sleep(2)
                if server.delete('%s:task_%s' % (BOT_NAME, id)):
                    return True
                else:
                    return False
            else:
                return False
        #两个队列中都未发现,停止失败
        else:
            return False