def cs(self, service_path, method, params): """ 调用服务 :param service_path: :param method: :param params: :return: """ version = serviceManager.get_loader_version(service_path) return serviceManager.do_service(service_path, method, params=params, version=version)
def do_service(self, service_path, method, params={}): """ 调用服务 :param service_path: :param method: :param params: :return: """ token = self.get_cookie('token') buyer_token = self.get_cookie('buyer_token') language = self.get_cookie('language') if not token: token = self.params('token') if not buyer_token: buyer_token = self.params('buyer_token') if not language: language = self.params('language') params['token'] = token if token else '' params['buyer_token'] = buyer_token if buyer_token else '' params['language'] = language if language else 'cn' return serviceManager.do_service(service_path, method, params=params, version=config.CONF['version'])
def do_service(self, service_path, method, params={}): """ 调用服务 :param service_path: :param method: :param params: :return: """ token = self.get_cookie('token') buyer_token = self.get_cookie('buyer_token') language = self.get_cookie('language') if not token: token = self.params('token') if not buyer_token: buyer_token = self.params('buyer_token') if not language: language = self.params('language') # 从application.settings导入power_tree power_tree = self.settings['power_tree'] # params['token'] = token if token else '' # params['buyer_token'] = buyer_token if buyer_token else '' # params['language'] = language if language else 'cn' return serviceManager.do_service(service_path, method, params=params, version=config.CONF['version'], power=power_tree)
def do_service(self, service_path, method, params={}): """ 调用服务 :param service_path: :param method: :param params: :return: """ return serviceManager.do_service(service_path, method, params=params, version=config.CONF['version'])
async def main(self): version = ServiceManager.get_loader_version(self.path) start_time = time.time() result = await ServiceManager.do_service(self.path, self.method, self.params, version=version) end_time = time.time() # 执行回调 if self.callback: self.callback(result, end_time - start_time)
def import_model(self, model_name): """ 加载数据类 :param model_name: string 数据类名 :return: """ try: version = serviceManager.get_loader_version() model = importlib.import_module('src.module.' + version + '.' + model_name) return model.Model() except Exception as e: self.logger.exception(e) return None
def do_work(self, job_params={}): """ 启动任务 :param job_params: :return: """ if job_params['type'] == 'one': # 执行单次任务 time.sleep(job_params['left_time']) # 将执行时间加入任务参数中 if isinstance(job_params['params'], dict) and 'notify_start_time' not in job_params['params']: job_params['params']['notify_start_time'] = job_params['start_time'] ServiceManager.do_service(job_params['service_path'], job_params['method'], job_params['params'], 'v1') if self.job_stores and job_params['job_id']: # 从数据库中移除任务 conn = self.pool.connection() cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) sql = 'delete from tbl_cfg_schedule_job where job_id = %s' cursor.execute(sql, (job_params['job_id'],)) conn.commit() elif job_params['type'] == 'many': # 执行多次任务 repeat_count = job_params['repeat_count'] while True: ServiceManager.do_service(job_params['service_path'], job_params['method'], job_params['params'], 'v1') time.sleep(job_params['limit_time']) if repeat_count: repeat_count -= 1 else: break if self.job_stores and job_params['job_id']: # 从数据库中移除任务 conn = self.pool.connection() cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) sql = 'delete from tbl_cfg_schedule_job where job_id = %s' cursor.execute(sql, (job_params['job_id'],)) conn.commit() elif job_params['type'] == 'circle': # 无限循环 print job_params['job_id'], 'sleep', job_params['left_time'], time.localtime(time.time()) time.sleep(job_params['left_time']) while True: ServiceManager.do_service(job_params['service_path'], job_params['method'], job_params['params'], 'v1') current_timestamp = int(time.time()) left_time = self.cron_utils.analyze(current_timestamp, job_params['cron']) while left_time == 0: # 防止多次执行 time.sleep(0.1) current_timestamp = int(time.time()) left_time = self.cron_utils.analyze(current_timestamp, job_params['cron']) print job_params['job_id'], 'sleep', left_time, time.localtime(current_timestamp) time.sleep(left_time) # print 'wait 600' # time.sleep(600) # time.sleep(job_params['limit_time']) self.job_id_list.pop(job_params['job_id'])
def do_service(self, service_path, method, params): """ 调用服务 :param service_path: :param method: :param params: :return: """ version = CommonUtil.get_loader_version(service_path) power_tree = self.settings['power_tree'] return serviceManager.do_service(service_path, method, params=params, version=version, power=power_tree)
def do_work(self, job_params={}): """ 启动任务 :param job_params: :return: """ if job_params['type'] == 'one': # 执行单次任务 time.sleep(job_params['left_time']) # 将执行时间加入任务参数中 if isinstance( job_params['params'], dict) and 'notify_start_time' not in job_params['params']: job_params['params']['notify_start_time'] = job_params[ 'start_time'] ServiceManager.do_service(job_params['service_path'], job_params['method'], job_params['params'], 'v1') if self.job_stores and job_params['job_id']: # 从数据库中移除任务 conn = self.pool.connection() cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) sql = 'delete from tbl_cfg_schedule_job where job_id = %s' cursor.execute(sql, (job_params['job_id'], )) conn.commit() elif job_params['type'] == 'many': # 执行多次任务 repeat_count = job_params['repeat_count'] while True: ServiceManager.do_service(job_params['service_path'], job_params['method'], job_params['params'], 'v1') time.sleep(job_params['limit_time']) if repeat_count: repeat_count -= 1 else: break if self.job_stores and job_params['job_id']: # 从数据库中移除任务 conn = self.pool.connection() cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) sql = 'delete from tbl_cfg_schedule_job where job_id = %s' cursor.execute(sql, (job_params['job_id'], )) conn.commit() elif job_params['type'] == 'circle': # 无限循环 print job_params['job_id'], 'sleep', job_params[ 'left_time'], time.localtime(time.time()) if job_params['left_time'] > 0: time.sleep(job_params['left_time']) while True: ServiceManager.do_service(job_params['service_path'], job_params['method'], job_params['params'], 'v1') current_timestamp = int(time.time()) left_time = self.cron_utils.analyze(current_timestamp, job_params['cron']) while left_time == 0: # 防止多次执行 time.sleep(0.1) current_timestamp = int(time.time()) left_time = self.cron_utils.analyze( current_timestamp, job_params['cron']) print job_params['job_id'], 'sleep', left_time, time.localtime( current_timestamp) if left_time > 0: time.sleep(left_time) # print 'wait 600' # time.sleep(600) # time.sleep(job_params['limit_time']) self.job_id_list.pop(job_params['job_id'])
import traceback from tools.logs import Logs pool_size = 3 try: pool_size = int(properties.get('task', 'POOL_NUM')) except Exception, e: print e redis = RedisBase() task_redis = redis.get_conn() error_redis = redis.get_conn() cache_key = cachekey.TASK_DATA_LIST error_cache_key = cachekey.ERROR_TASK_DATA_LIST pool = threadpool.ThreadPool(pool_size) service_manager = ServiceManager() config = config logger = Logs().get_logger() def update_task(last_id): """ 更新任务完成状态 :param last_id: :return: """ model = ModelBase() fields = ['is_complete = 1'] condition = ' id = %s ' value_tuple = (last_id, ) result = model.update(