Exemple #1
0
def cron_do(cron_id):

    with scheduler.app.app_context():

        cif = CronInfos.query.get(cron_id)

        if not cif:
            jl = JobLog(cron_info_id=cron_id,content="定时任务不存在",create_time=get_now_time(),take_time=0)
            db.session.add(jl)
            db.session.commit()
        else:
            req_url = cif.req_url
            if not req_url:
                jl = JobLog(cron_info_id=cron_id, content="请求链接不存在", create_time=get_now_time(), take_time=0)
                db.session.add(jl)
                db.session.commit()
            else:
                if req_url.find('http') == -1:
                    jl = JobLog(cron_info_id=cron_id, content="请求链接有误,请检查一下", create_time=get_now_time(), take_time=0)
                    db.session.add(jl)
                    db.session.commit()
                else:
                    t = time.time()

                    req = requests.get(req_url,timeout=2*60,headers={'user-agent':'xmb_cron'})

                    ret = req.text

                    try:
                        ret = req.json()
                        errcode = ret.get('errcode')
                        if errcode:
                            if int(errcode) !=0:
                                wechat_info_err('定时任务【%s】发生错误' % cif.task_name,'返回信息:%s' % json.dumps(ret,ensure_ascii=False))
                    except:
                        pass

                    if type(ret) == dict:
                        ret = json.dumps(ret,ensure_ascii=False)

                    jl = JobLog(cron_info_id=cron_id, content=ret, create_time=get_now_time(),take_time=time.time() - t)
                    db.session.add(jl)
                    db.session.commit()

    return ""
Exemple #2
0
def check_pass():
    today = get_now_time()
    msg = request.values.get('msg', '')

    if request.method == 'POST':
        try:
            password = request.values.get('password')
            if not password:
                return redirect("/check_pass?msg=密码不能为空")
            login_pwd = current_app.config.get('LOGIN_PWD')
            if not login_pwd:
                return redirect("/check_pass?msg=请联系管理员")
            if login_pwd != password:
                return redirect("/check_pass?msg=密码有误")
            session['is_login'] = True
            return redirect('/cron_list')
        except:
            return redirect("/check_pass?msg=系统有误,请重新试试")
    return render_template("check_pass.html", msg=msg, today=today)
Exemple #3
0
def cron_do(cron_id):
    try:
        with scheduler.app.app_context():
            nows = get_now_time()

            cif = CronInfos.query.get(cron_id)

            if not cif:
                jl = JobLog(cron_info_id=cron_id,content="定时任务不存在",create_time=nows,take_time=0)
                db.session.add(jl)
                db.session.commit()
            else:
                req_url = cif.req_url
                if not req_url:
                    jl = JobLog(cron_info_id=cron_id, content="请求链接不存在", create_time=nows, take_time=0)
                    db.session.add(jl)
                    db.session.commit()
                else:
                    if req_url.find('http') == -1:
                        jl = JobLog(cron_info_id=cron_id, content="请求链接有误,请检查一下", create_time=nows, take_time=0)
                        db.session.add(jl)
                        db.session.commit()
                    else:
                        try:
                            t = time.time()

                            req = requests.get(req_url,timeout=2*60,headers={'user-agent':'xmb_cron'})

                            ret = req.text

                            try:
                                ret = req.json()
                            except:
                                pass

                            if type(ret) == dict:
                                ret = json.dumps(ret,ensure_ascii=False)

                            error_keyword = configs('error_keyword')

                            if error_keyword:
                                error_keyword = error_keyword.replace(',', ',').split(',')
                                for item in error_keyword:
                                    if item.strip().lower() in ret.lower():
                                        wechat_info_err('定时任务【%s】发生错误' % cif.task_name, '返回信息:%s' % ret)
                                        break

                            jl = JobLog(cron_info_id=cron_id, content=ret, create_time=nows,take_time=time.time() - t)
                            db.session.add(jl)
                            db.session.commit()
                        except Exception as e:
                            jl = JobLog(cron_info_id=cron_id, content="发生严重错误:%s" % str(e), create_time=nows, take_time=time.time() - t)
                            db.session.add(jl)
                            db.session.commit()

                            wechat_info_err('定时任务【%s】发生严重错误' % cif.task_name, '返回信息:%s' % str(e))

    except Exception as e:
        wechat_info_err('定时任务发生严重错误', '返回信息:%s' % str(e))

    return ""
Exemple #4
0
def cron_edit():
    id = request.values.get('id')
    cif = CronInfos.query.get(id)
    if request.method == 'POST':

        datas = request.values.to_dict()
        ds_ms = datas.get('ds_ms')
        task_name = datas.get('task_name')

        if not task_name:
            return web_api_return(code=1, msg='任务名称不能为空')

        task_keyword = datas.get('task_keyword') or ''

        _cif = CronInfos.query.filter(CronInfos.task_name == task_name,
                                      CronInfos.id != datas.get('id')).first()
        if _cif:
            return web_api_return(code=1, msg='任务名称已存在已存在')

        run_date = datas.get('run_date')

        if ds_ms == '2':
            run_date = ''

        if run_date:
            if run_date < get_now_time('%Y-%m-%d %H:%M'):
                return web_api_return(code=1, msg='设置的时间已过期,请重新设置')

        day = datas.get('day')

        if day:
            if day.isdigit() and int(day) not in range(1, 32):
                return web_api_return(code=1, msg='日(号)不在范围内,请检查!')
            else:
                pass

        day_of_week = datas.get('day_of_week')

        if day_of_week:
            if day_of_week.isdigit():
                if int(day_of_week) not in range(0, 7):
                    return web_api_return(code=1, msg='星期 不在范围内,请检查!')
            else:
                if day_of_week not in [
                        'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'
                ]:
                    return web_api_return(code=1, msg='星期 不在范围内,请检查!')

        hour = datas.get('hour')

        if hour and hour.isdigit():
            if int(hour) not in range(0, 24):
                return web_api_return(code=1, msg='小时 不在范围内,请检查!')

        minute = datas.get('minute')
        if minute and minute.isdigit():
            if int(minute) not in range(0, 60):
                return web_api_return(code=1, msg='分钟 不在范围内,请检查!')

        second = datas.get('second')

        if second and second.isdigit():
            if int(second) not in range(0, 60):
                return web_api_return(code=1, msg='秒 不在范围内,请检查!')

        ds_ms = datas.get('ds_ms')
        if ds_ms == '1':
            if not run_date:
                return web_api_return(code=1, msg='时间没设置呢!')
        else:
            if not day_of_week and not day and not hour and not minute and not second:
                return web_api_return(code=1, msg='请完整填写!')

        req_url = datas.get('req_url')

        if not req_url:
            return web_api_return(code=1, msg='回调URL必填!')

        if 'http://' not in req_url and 'https://' not in req_url:
            return web_api_return(code=1, msg='URL格式有误!')

        cif.task_name = task_name
        cif.task_keyword = task_keyword
        cif.run_date = run_date
        cif.day_of_week = day_of_week
        cif.day = day
        cif.hour = hour
        cif.minute = minute
        cif.second = second
        cif.req_url = req_url
        cif.status = 1
        db.session.add(cif)

        db.session.commit()

        cron_id = cif.id

        cron_datas = {}
        if run_date:
            cron_datas['trigger'] = 'date'
            cron_datas['run_date'] = run_date
        else:
            # 定时的
            cron_datas['trigger'] = 'cron'
            if day_of_week:
                cron_datas['day_of_week'] = day_of_week
            if hour:
                cron_datas['hour'] = hour
            if minute:
                cron_datas['minute'] = minute
            if day:
                cron_datas['day'] = day
            if second and second != '*':
                cron_datas['second'] = second

        scheduler.add_job("cron_%s" % cron_id,
                          func=cron_do,
                          args=[cron_id],
                          replace_existing=True,
                          **cron_datas)

        return web_api_return(code=0, msg='修改成功!', url='/cron_list')

    return render_template("cron_edit.html", cif=cif)
Exemple #5
0
def celery_run(url,req_id,queue,priority,delay_time=0,post_json=None,is_fail_try=0,is_first=True,fail_counts=0,remark=''):
    time1 = time.time()
    try:

        is_err = False

        config = current_app.config.get('CONFIGS')

        timeouts = 60*2

        tt_list = config.get('retry_times')

        if tt_list:
            tt_list = tt_list.replace(',',',').split(',')

        else:
            tt_list = [30,60,180,900,1800,3600,43200]

        headers = {
            'User-Agent':'xiaoniu_tasks'
        }

        reqs = requests.Session()

        if post_json:
            rep = reqs.post(url,data=post_json,timeout=timeouts,headers=headers)
        else:
            rep = reqs.get(url, timeout=timeouts,headers=headers)

        ret = rep.text

        try:
            ret = rep.json()
        except:
            pass

        if type(ret) == dict:
            ret = json.dumps(ret, ensure_ascii=False)

        error_keyword = config.get('error_keyword')

        if error_keyword and is_fail_try == 1:
            error_keyword = error_keyword.replace(',', ',').split(',')
            for item in error_keyword:
                if item.strip().lower() in ret.lower():
                    is_err = True
                    wechat_info_err('定时任务【%s】发生错误' % req_id, '返回信息:%s' % ret)
                    break

        if is_err is True:
            #看是否得重试
            if is_first is True:
                is_first = False
                if is_fail_try == 1:delay_time = int(tt_list[0])
            else:
                if is_fail_try== 1:
                    is_first = False
                    if int(delay_time) == int(tt_list[-1]):
                        is_fail_try = 0
                        delay_time = -1
                    else:
                        delay_time = int(tt_list[tt_list.index(str(delay_time)) + 1])

            if delay_time !=-1:
                celery_run.apply_async(queue=queue,priority=priority,countdown = int(delay_time),args=[url,req_id,queue,priority,delay_time,post_json,is_fail_try,is_first,fail_counts,remark])
        time2 = time.time() - time1
        rl = ReqLog.query.filter(ReqLog.req_id == req_id).first()
        if not rl:
            rl = ReqLog(req_url=url,req_id=req_id,post_json=post_json,respond=ret,create_time=get_now_time(),is_err=int(is_err),take_time=time2)
        else:
            rl.respond = "%s<br/><br/>%s" % (rl.respond,ret)
            rl.update_time = get_now_time()
            rl.is_err = int(is_err)
            rl.take_time = time2

        db.session.add(rl)
        db.session.commit()

    except Exception as e:
        if fail_counts <=5:
            fail_counts = fail_counts + 1
            celery_run.apply_async(queue=queue,priority=priority,countdown=30*fail_counts,args=[url, req_id, queue,priority,delay_time, post_json, is_fail_try, is_first,fail_counts,remark])
            # trace_info = traceback.format_exc()
            # print(trace_info)