Пример #1
0
    def post(self, userid):
        user = self.current_user
        try:
            envs = {}
            for key in self.request.body_arguments:
                envs[key] = self.get_body_arguments(key)
            env = {}
            op = self.get_argument('op', '')
            if (op != ''):
                tasktype = op
                if isinstance(tasktype,bytes):
                    tasktype = tasktype.decode()
            else:
                raise Exception('错误参数')
            pass
            for k, v  in envs.items():
                env[k] = json.loads(v[0])
            for taskid, selected  in env['selectedtasks'].items():
                if (selected):
                    task = self.db.task.get(taskid, fields=('id',  'note', 'tplid', 'userid'))
                    if (task):
                        if (task['userid']) == int(userid):
                            if (tasktype == 'disable'):
                                self.db.task.mod(taskid, disabled = True)
                            if (tasktype == 'enable'):
                                self.db.task.mod(taskid, disabled = False)
                            if (tasktype == 'delete'):
                                logs = self.db.tasklog.list(taskid = taskid, fields=('id'))
                                for log in logs:
                                    self.db.tasklog.delete(log['id'])
                                self.db.task.delete(taskid)
                            if (tasktype == 'setgroup'):
                                group_env = env['setgroup']
                                New_group = group_env['newgroup'].strip()
                                if New_group != "" :
                                    target_group = New_group
                                else:
                                    target_group = group_env['checkgroupname'] or 'None'

                                self.db.task.mod(taskid, _groups=target_group)

                            if (tasktype == 'settime'):
                                time_env = env['settime']
                                c = cal()
                                settime_env = {
                                    'sw': True,
                                    'time': time_env['ontime_val'],
                                    'mode': time_env['ontime_method'],
                                    'date': time_env['ontime_run_date'],
                                    'tz1': time_env['randtimezone1'],
                                    'tz2': time_env['randtimezone2'],
                                    'cron_val': time_env['cron_val'],
                                }

                                if (time_env['randtimezone1'] != '') and (time_env['randtimezone1'] != ''):
                                    settime_env['randsw'] = True 
                                if (time_env['cron_sec'] != ''):
                                    settime_env['cron_sec'] = time_env['cron_sec'] 

                                if (len(settime_env['time'].split(':')) == 2):
                                    settime_env['time'] = settime_env['time'] + ':00'

                                tmp = c.calNextTs(settime_env)
                                if (tmp['r'] == 'True'):
                                    self.db.task.mod(taskid, disabled = False, 
                                                            newontime = json.dumps(settime_env), 
                                                            next = tmp['ts'])
                                else:
                                    raise Exception(u'参数错误')
                        else:
                            raise Exception('用户id与任务的用户id不一致')
        except Exception as e:
            traceback.print_exc()
            self.render('utils_run_result.html', log=str(e), title=u'设置失败', flg='danger')
            return

        self.render('utils_run_result.html', log=u'设置成功,请手动刷新页面查看', title=u'设置成功', flg='success')
        return
Пример #2
0
    async def post(self, taskid):
        self.evil(+2)
        start_ts = int(time.time())
        user = self.current_user
        task = self.check_permission(self.db.task.get(taskid, fields=('id', 'tplid', 'userid', 'init_env',
            'env', 'session', 'retry_count', 'retry_interval', 'last_success', 'last_failed', 'success_count', 'note',
            'failed_count', 'last_failed_count', 'next', 'disabled', 'ontime', 'ontimeflg', 'pushsw','newontime')), 'w')

        tpl = self.check_permission(self.db.tpl.get(task['tplid'], fields=('id', 'userid', 'sitename',
            'siteurl', 'tpl', 'interval', 'last_success')))

        fetch_tpl = self.db.user.decrypt(
                0 if not tpl['userid'] else task['userid'], tpl['tpl'])
        env = dict(
                variables = self.db.user.decrypt(task['userid'], task['init_env']),
                session = [],
                )
        
        pushsw = json.loads(task['pushsw'])
        newontime = json.loads(task['newontime'])
        pushertool = pusher()
        caltool = cal()

        try:
            url = utils.parse_url(env['variables'].get('_proxy'))
            if not url:
                new_env = await gen.convert_yielded(self.fetcher.do_fetch(fetch_tpl, env))
            else:
                proxy = {
                    'scheme': url['scheme'],
                    'host': url['host'],
                    'port': url['port'],
                    'username': url['username'],
                    'password': url['password']
                }
                new_env = await gen.convert_yielded(self.fetcher.do_fetch(fetch_tpl, env, [proxy]))
        except Exception as e:
            t = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            title = u"签到任务 {0}-{1} 失败".format(tpl['sitename'], task['note'])
            logtmp = u"{0} \\r\\n日志:{1}".format(t, e)

            self.db.tasklog.add(task['id'], success=False, msg=str(e))
            self.finish('<h1 class="alert alert-danger text-center">签到失败</h1><div class="showbut well autowrap" id="errmsg">%s<button class="btn hljs-button" data-clipboard-target="#errmsg" >复制</button></div>' % logtmp.replace('\\r\\n', '<br>'))

            await pushertool.pusher(user['id'], pushsw, 0x4, title, logtmp)
            return

        self.db.tasklog.add(task['id'], success=True, msg=new_env['variables'].get('__log__'))
        if (newontime["sw"]):
            if ('mode' not in newontime):
                newontime['mode'] = 'ontime'

            if (newontime['mode'] == 'ontime'):
                newontime['date'] = (datetime.datetime.now()+datetime.timedelta(days=1)).strftime("%Y-%m-%d")
            nextTime = caltool.calNextTs(newontime)['ts']
        else:
            nextTime = time.time() + (tpl['interval'] if tpl['interval'] else 24 * 60 * 60)
            
        self.db.task.mod(task['id'],
                disabled = False,
                last_success = time.time(),
                last_failed_count = 0,
                success_count = task['success_count'] + 1,
                mtime = time.time(),
                next = nextTime)
        
        t = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        title = u"签到任务 {0}-{1} 成功".format(tpl['sitename'], task['note'])
        logtmp = new_env['variables'].get('__log__')
        logtmp = u"{0} \\r\\n日志:{1}".format(t, logtmp)

        self.db.tpl.incr_success(tpl['id'])
        self.finish('<h1 class="alert alert-success text-center">签到成功</h1><div class="showbut well autowrap" id="errmsg"><pre>%s</pre><button class="btn hljs-button" data-clipboard-target="#errmsg" >复制</button></div>' % logtmp.replace('\\r\\n', '<br>'))
        
        await pushertool.pusher(user['id'], pushsw, 0x8, title, logtmp)
        logDay = int(self.db.site.get(1, fields=('logDay'))['logDay'])
        for log in self.db.tasklog.list(taskid = taskid, fields=('id', 'ctime')):
            if (time.time() - log['ctime']) > (logDay * 24 * 60 * 60):
                self.db.tasklog.delete(log['id'])
        return
Пример #3
0
    async def do(self, task):
        task['note'] = self.db.task.get(task['id'], fields=('note'))['note']
        user = self.db.user.get(task['userid'], fields=('id', 'email', 'email_verified', 'nickname', 'logtime'))
        tpl = self.db.tpl.get(task['tplid'], fields=('id', 'userid', 'sitename', 'siteurl', 'tpl', 'interval', 'last_success'))
        ontime = self.db.task.get(task['id'], fields=('ontime', 'ontimeflg', 'pushsw', 'newontime', 'next'))
        newontime = json.loads(ontime["newontime"])
        pushtool = pusher()
        caltool = cal()
        logtime = json.loads(user['logtime'])
        pushsw = json.loads(ontime['pushsw'])

        if 'ErrTolerateCnt' not in logtime:logtime['ErrTolerateCnt'] = 0 

        if task['disabled']:
            self.db.tasklog.add(task['id'], False, msg='task disabled.')
            self.db.task.mod(task['id'], next=None, disabled=1)
            return False

        if not user:
            self.db.tasklog.add(task['id'], False, msg='no such user, disabled.')
            self.db.task.mod(task['id'], next=None, disabled=1)
            return False

        if not tpl:
            self.db.tasklog.add(task['id'], False, msg='tpl missing, task disabled.')
            self.db.task.mod(task['id'], next=None, disabled=1)
            return False

        if tpl['userid'] and tpl['userid'] != user['id']:
            self.db.tasklog.add(task['id'], False, msg='no permission error, task disabled.')
            self.db.task.mod(task['id'], next=None, disabled=1)
            return False

        start = time.time()
        try:
            fetch_tpl = self.db.user.decrypt(0 if not tpl['userid'] else task['userid'], tpl['tpl'])
            env = dict(
                    variables = self.db.user.decrypt(task['userid'], task['init_env']),
                    session = [],
                    )

            url = utils.parse_url(env['variables'].get('_proxy'))
            if not url:
                new_env = await gen.convert_yielded(self.fetcher.do_fetch(fetch_tpl, env))
            else:
                proxy = {
                    'scheme': url['scheme'],
                    'host': url['host'],
                    'port': url['port'],
                    'username': url['username'],
                    'password': url['password']
                }
                new_env = await gen.convert_yielded(self.fetcher.do_fetch(fetch_tpl, env, [proxy]))

            variables = self.db.user.encrypt(task['userid'], new_env['variables'])
            session = self.db.user.encrypt(task['userid'],
                    new_env['session'].to_json() if hasattr(new_env['session'], 'to_json') else new_env['session'])

            # todo next not mid night
            if (newontime['sw']):
                if ('mode' not in newontime):
                    newontime['mode'] = 'ontime'
                if (newontime['mode'] == 'ontime'):
                    newontime['date'] = (datetime.datetime.now()+datetime.timedelta(days=1)).strftime("%Y-%m-%d")
                next = caltool.calNextTs(newontime)['ts']
            else:
                next = time.time() + max((tpl['interval'] if tpl['interval'] else 24 * 60 * 60), 1*60)
                if tpl['interval'] is None:
                    next = self.fix_next_time(next)

            # success feedback
            self.db.tasklog.add(task['id'], success=True, msg=new_env['variables'].get('__log__'))
            self.db.task.mod(task['id'],
                    last_success=time.time(),
                    last_failed_count=0,
                    success_count=task['success_count']+1,
                    env=variables,
                    session=session,
                    mtime=time.time(),
                    next=next)
            self.db.tpl.incr_success(tpl['id'])

            t = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            title = u"签到任务 {0}-{1} 成功".format(tpl['sitename'], task['note'])
            logtemp = new_env['variables'].get('__log__')
            logtemp = u"{0} \\r\\n日志:{1}".format(t, logtemp)
            await pushtool.pusher(user['id'], pushsw, 0x2, title, logtemp)

            logger.info('taskid:%d tplid:%d successed! %.5fs', task['id'], task['tplid'], time.time()-start)
            # delete log
            self.ClearLog(task['id'])
        except Exception as e:
            # failed feedback
            traceback.print_exc()
            next_time_delta = self.failed_count_to_time(task['last_failed_count'], task['retry_count'], task['retry_interval'], tpl['interval'])
                        
            t = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            title = u"签到任务 {0}-{1} 失败".format(tpl['sitename'], task['note'])
            content = u"{0} \\r\\n日志:{1}".format(t, str(e))
            disabled = False
            if next_time_delta:
                next = time.time() + next_time_delta
                content = content + u" \\r\\n下次运行时间:{0}".format(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(next)))
                if (logtime['ErrTolerateCnt'] <= task['last_failed_count']):
                    await pushtool.pusher(user['id'], pushsw, 0x1, title, content)
            else:
                disabled = True
                next = None
                content = u" \\r\\n任务已禁用"
                await pushtool.pusher(user['id'], pushsw, 0x1, title, content)

            self.db.tasklog.add(task['id'], success=False, msg=str(e))
            self.db.task.mod(task['id'],
                    last_failed=time.time(),
                    failed_count=task['failed_count']+1,
                    last_failed_count=task['last_failed_count']+1,
                    disabled = disabled,
                    mtime = time.time(),
                    next=next)
            self.db.tpl.incr_failed(tpl['id'])

            logger.error('taskid:%d tplid:%d failed! %r %.4fs', task['id'], task['tplid'], str(e), time.time()-start)
            return False
        return True