示例#1
0
def structurePost(item):
    '''
        构造post参数,并带参数post微信,进行消息推送
        (因为多线程传值的问题,不能放在core中)
    '''
    global clazzUrl
    payload = {'touser': str(item['openId']),
               'template_id': "IGdf7wm2xyhdFT09a1sktzpUXiMfQzgEN42DsUlR9yo",
               'url': variable.taskUrl,
               'data': {
                   'first': {
                       "value": "第" + str(item['dayNum']) + "天的挑战开始了,快快完成任务吧。(任务完成后记得打卡哦)",
                       "color": "#000000"
                   },
                   'keyword1': {
                       "value": str(item['clazzName']),
                       "color": "#000000"
                   },
                   'keyword2': {
                       "value": str(item['userName']),
                       "color": "#000000"
                   },
                   'remark': {
                       "value": "平庸,就是失去追求卓越信念的瞬间。今日再接再厉!!快快点击【详情】进入今日任务开启新的征程。",
                       "color": "#000000"
                   }
               }}

    payload = json.dumps(payload)

    dic = {}
    #分辨是否是测试模式
    if (str(item['openId']) == test_Id and test_Id != '') or (test_Id == ''):
        print "success enter"
        ACCESS_TOKEN = search_token()
        message = requests.post("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + ACCESS_TOKEN,payload)
        message = json.loads(message.text)
        parameter = "task=" + str(item['task']) + \
                                  ",openId=" + str(item['openId']) + \
                                  ",dayNum=" + str(item['dayNum']) + \
                                  ",userName="******",clazzName=" + str(item['clazzName'])
        dic['errmsg'] = str(message['errmsg'])
        dic['task'] = item['task']
        dic['userName'] = item['userName']
        dic['parameter'] = parameter
        if (variable.test_Id != ''):
            add_log('测试成功',
                    'system',
                    '')
        return dic
    else:
        return dic
示例#2
0
def push_gambition_task(clazzId,jobId,testId):
    '''
        gambition推送任务
    '''
    global test_Id
    test_Id = testId
    try:
        userTask = taskByClazzId(clazzId)
        pool = ThreadPool(4)
        results = pool.map(structurePost, userTask)
        for each in results:
            if str(each['errmsg']) == 'ok':
                add_log('任务 ' + each['task'] + ' 推送给用户 ' + each['userName'] + ' 成功',
                        jobId,
                        each['parameter'])
            else:
                add_log('任务 ' + each['task'] + ' 推送给用户 ' + each['userName'] + ' 失败',
                        jobId,
                        each['parameter'])
        add_log('push_gambition_task任务执行成功',
                'system',
                '')
    except Exception,e:
        add_log('push_gambition_task出现异常: ' + e.message,
                'system',
                '')
示例#3
0
def delete_task():
    '''
        删除任务(任务流第一层)
    '''
    try:
        date = str(datetime.datetime.now().strftime("%Y-%m-%d"))
        taskList = Task.objects(endDate__lt=date,
                                isRunning=1).all()
        if taskList:
            for each in taskList:
                scheduler.delete_job(str(each['jobId']))
                each.isRunning = 0
                each.save()
                add_log('成功将任务 ' + each.jobId + ' 从任务队列中踢出',
                        'system',
                        '')
    except Exception,e:
        add_log('delete_task出现异常: ' + e.message,
                'system',
                '')
示例#4
0
def add_task():
    '''
        添加任务(任务流第一层)
    '''
    try:
        date = str(datetime.datetime.now().strftime("%Y-%m-%d"))
        taskList = Task.objects(beginDate__lte=date,
                                endDate__gte=date,
                                isRunning=0).all()
        if taskList:
            for each in taskList:
                exec(str(each.command))
                each.isRunning = 1
                each.save()
                add_log('成功将任务 ' + each.jobId + ' 添加到任务队列中',
                        'system',
                        '')
    except Exception,e:
        add_log('add_task出现异常: ' + e.message,
                'system',
                '')
示例#5
0
def repeat_push_gambition_task(oldJobId,jobId):
    '''
        上一次失败的任务再推送
    '''
    try:
        userTask = abortedTask(oldJobId)
        pool = ThreadPool(4)
        results = pool.map(structurePost, userTask)
        for each in results:
            if str(each['errmsg']) == 'ok':
                add_log('二次推送: 任务 ' + each['task'] + ' 推送给用户 ' + each['userName'] + ' 成功',
                        jobId,
                        each['parameter'])
            else:
                add_log('二次推送: 任务 ' + each['task'] + ' 推送给用户 ' + each['userName'] + ' 失败',
                        jobId,
                        each['parameter'])
        add_log('repeat_push_gambition_task任务执行成功',
                'system',
                '')
    except Exception,e:
        add_log('repeat_push_gambition_task出现异常: ' + e.message,
                'system',
                '')