示例#1
0
    def __runTaskInPool__(self, secTime, task):
        def updateTask():
            try:
                self.taskHandler.update(task)
            except Exception as ex:
                slog.error(ex)

        if not task['pause'] and task['status'] != 'run' and task[
                'status'] != 'wait':
            try:
                task['status'] = "wait"
                self.taskHandler.initRun(task)

                task['now'] = secTime
                task['status'] = 'run'
                updateTask()
                self.taskHandler.run(task)

                task['status'] = 'stop'
                task['time'] = time.time() - secTime
            finally:
                if task['status'] != 'stop':
                    task['status'] = 'exception'
                try:
                    self.taskHandler.endRun(task)
                    updateTask()
                except Exception as ex:
                    slog.error(ex)
            return 1
        return 0
示例#2
0
文件: task.py 项目: chinple/mserver
 def __startTimmer__(self):
     tp = ThreadPool(3)
     while 1:
         try:
             tp.apply_async(self.__runAllTask__)
             time.sleep(self.interval)
         except Exception as ex:
             slog.error("Main thread: %s" % ex)
示例#3
0
 def __callObjFun__(self, reqPath, reqParam):
     try:
         return self.ws.callObjFunInworker(reqPath, reqParam)
     except WorkerManager.WorkerFullException as ex:
         slog.warn(str(ex))
     except:
         slog.error(str(traceback.format_exc()))
     # If worker is full or exception, deal request in main process
     return ObjHandler.__callObjFun__(self, reqPath, reqParam)
示例#4
0
 def __runAllTask__(self):
     now = time.localtime()
     secTime, hour, minute = time.time(), now.tm_hour, now.tm_min
     for sc in self.tasks:
         try:
             self.__runMatchSchedule__(secTime, hour, minute,
                                       self.tasks[sc])
         except:
             slog.error("Fail schedule: %s" % traceback.format_exc())
示例#5
0
def wokerCall(callId, resultContainer, pparam, lockList, objHandler):
    reqPath, reqParam = resultContainer[callId]
    resultContainer.__delitem__(callId)
    try:
        resultContainer[callId] = objHandler.__callObjFun__(reqPath, reqParam)
    except Exception as ex:
        resultContainer[callId] = "Fail to call in process: %s" % ex
        slog.error(traceback.format_exc())
    finally:
        lockList[callId].release()
示例#6
0
文件: cproxy.py 项目: chinple/mserver
 def __getProxyInfo__(self, headers, address, path):
     ip, port, host = "", 80, ""
     if tryGet(headers, "cfrom", None) != self.localHosts:
         try:  # get by host
             ip = headers["host"].lower()
             ps = ip.split(":")
             if len(ps) == 2:
                 ip, port = ps[0], int(ps[1])
             ps = self.hostIp[ip]
             ip, port, host = ps['ip'], ps['port'], ps['host']
         except:  # get by path
             ps = self.__getPathProxy__(path) if self.pathIp else None
             if ps is None:
                 if self.isDebugMode:
                     slog.warn("Not found proxy: %s \t%s" % (ip, path))
             else:
                 ip, port, host = ps['ip'], ps['port'], ps['host']
     if ip == "":
         if self.isDebugMode: slog.error("No proxy hosts: %s" % headers)
         raise ProxyHttpHandle.NoHostException()
     return ip, port, host
示例#7
0
文件: task.py 项目: chinple/mserver
    def __runTaskInPool__(self, curTime, task, mspan, rargs=None):
        def updateTask(isfinish=False, tret=None):
            try:
                self.taskHandler.update(task, isfinish, tret)
            except Exception as ex:
                slog.error("updateTask: %s" % ex)

        task['stime'], ltime = curTime, task['stime']
        if curTime - ltime < mspan: return  # check duplicate run

        if task['status'] != 'run':
            task['status'] = 'run'
            task['stime'], task['runCount'] = curTime, task['runCount'] + 1
            try:
                task['rargs'] = rargs
                tret = None
                self.taskHandler.initRun(task)
                try:  # configured for each task
                    time.sleep(task['targs']['delay'])
                except:
                    pass

                updateTask()
                tret = self.taskHandler.run(task)
                task['status'] = 'finish' if (
                    task['maxCount'] >= 0
                    and task['maxCount'] <= task['runCount']) else 'wait'
            finally:
                if task['result'] > 0: task['fcount'] += 1
                else: task['fcount'] = 0

                task['rspan'] = time.time() - curTime
                if task['status'] == 'run': task['status'] = 'exception'
                try:
                    self.taskHandler.endRun(task)
                    updateTask(True, tret)
                except Exception as ex:
                    slog.error(ex)
            return 1
        return 0
示例#8
0
文件: cproxy.py 项目: chinple/mserver
    def __handleRequest__(self, reqObj, reqPath, reqParam, isPost):
        if isPost:
            reqParam = reqObj.readPostBody()

        isMock, reqAddress, respTime = False, reqObj.client_address, time.time(
        )
        if self.isMock and _tryExecute(
                self.__isMockRquest__,
            (reqPath, reqParam, reqObj.headers, reqObj.client_address), False):
            respStatus, wait, resp = self.__getMockResponse__(
                reqPath, reqParam, reqObj.headers)
            if wait > 0:
                time.sleep(wait)
            isMock, reqHeader, respBody = True, reqObj.headers, bytes(resp)
            respHeader = [("Content-Length", len(respBody)),
                          ("Content-Type", "text/plan;charset=utf-8")]
        else:
            try:
                reqHeader, respObj = self.__sendProxyRequest(
                    reqObj, reqParam if isPost else None)
                respStatus, respHeader, respBody = respObj.status, respObj.getheaders(
                ), respObj.read()
            except ProxyHttpHandle.NoHostException:
                return "/__file__%s" % reqPath
            except Exception as ex:
                reqHeader, respStatus, respHeader, respBody = reqObj.headers, 555, {}, str(ex)
                slog.error("Fail to proxy %s: %s, %s: %s" %
                           (type(ex), ex, reqPath, reqObj.headers))

        try:
            reqTime = time.time() - respTime
            self.__sendResponse(reqObj, respStatus, respHeader, respBody)
        finally:
            respTime = time.time() - respTime - reqTime
            self.logPool.apply_async(self.__analyzeSession__,
                                     args=(isMock, reqObj.command, reqPath,
                                           reqParam, respBody, reqTime,
                                           respTime, respStatus, reqAddress,
                                           dict(reqHeader), dict(respHeader)))
示例#9
0
文件: driver.py 项目: chinple/mserver
    def __runCases__(self, *modelTypes):
        slog.info(time.strftime("%Y-%m-%d %H:%M:%S testing..."))
        for modelType in modelTypes:
            if self.tc.isInMode("run", 1):
                try:
                    tcObj = modelType()
                except Exception as ex:
                    slog.warn('Instance Class: %s with %s' % (modelType, ex))
                    continue
            else:
                tcObj = modelType()
            try:
                tcInfo, caseRunList = self.tc.getRunInfo(tcObj.__class__.__name__, tcObj)
            except Exception as ex:
                slog.error("No case: %s" % ex)
                continue

            if len(caseRunList) == 0:
                continue

            for caseName in caseRunList:
                if eval("tcObj.%s()" % caseName) == False:
                    return False
                if self.endScenarioHandler != None:
                    self.endScenarioHandler(tcObj, caseName)

            if tcInfo.isTested:
                if self.tc.isInMode("run", 1):
                    try:
                        tcObj.tearDown()
                    except Exception as ex:
                        slog.warn('tearDown Should NOT Fail! %s: %s' % (ex, self.getMTestTraceBack()))
                else:
                    tcObj.tearDown()
            if self.endModelHandler != None:
                self.endModelHandler(tcInfo)

        self.tlog.close()
        return self.tc.report()
示例#10
0
 def updateTask():
     try:
         self.taskHandler.update(task)
     except Exception as ex:
         slog.error(ex)
示例#11
0
文件: task.py 项目: chinple/mserver
 def updateTask(isfinish=False, tret=None):
     try:
         self.taskHandler.update(task, isfinish, tret)
     except Exception as ex:
         slog.error("updateTask: %s" % ex)