|  | @@ -58,10 +58,18 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse, task_name):
 | 
	
		
			
				|  |  |                                                  service_name, wechat_name, task_name)
 | 
	
		
			
				|  |  |                      except Exception as e:
 | 
	
		
			
				|  |  |                          res = {'sucess': False, 'result_info': str(e)}
 | 
	
		
			
				|  |  | -                        sql_tools.action_record(res, sql_session, layout_create_action, user_id, plan_name,
 | 
	
		
			
				|  |  | +                        print('layout 创建失败', layout_create_action, res)
 | 
	
		
			
				|  |  | +                        # 创建落地页,计划都失败
 | 
	
		
			
				|  |  | +                        sql_tools.action_record(res, sql_session, layout_create_action, user_id, layout_name,
 | 
	
		
			
				|  |  | +                                                action_record_table,
 | 
	
		
			
				|  |  | +                                                service_name, wechat_name, task_name)
 | 
	
		
			
				|  |  | +                        res = {'sucess': False, 'result_info': '落地页创建失败'}
 | 
	
		
			
				|  |  | +                        sql_tools.action_record(res, sql_session, ad_plan_create_action, user_id, plan_name,
 | 
	
		
			
				|  |  |                                                  action_record_table,
 | 
	
		
			
				|  |  |                                                  service_name, wechat_name, task_name)
 | 
	
		
			
				|  |  | +                        raise
 | 
	
		
			
				|  |  |                          continue
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |                  log_ad.refresh_driver()
 | 
	
		
			
				|  |  |                  # 3.创建计划
 | 
	
		
			
				|  |  |                  log_ad.select_ad_master(service_name, wechat_name)
 | 
	
	
		
			
				|  | @@ -75,23 +83,19 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse, task_name):
 | 
	
		
			
				|  |  |                                          action_record_table,
 | 
	
		
			
				|  |  |                                          service_name, wechat_name, task_name)
 | 
	
		
			
				|  |  |                  logging.info('创建计划任务结束')
 | 
	
		
			
				|  |  | -                log_ad.refresh_driver()
 | 
	
		
			
				|  |  |              except Exception as e:
 | 
	
		
			
				|  |  |                  log_ad.driver.save_screenshot(
 | 
	
		
			
				|  |  |                      'user_id:{}_time_{}_plan_name:{}_plan_error.png'.format(user_id, datetime.now().strftime(
 | 
	
		
			
				|  |  |                          "%Y-%m-%d, %H:%M:%S"), plan_name))
 | 
	
		
			
				|  |  |                  logging.error(str(e))
 | 
	
		
			
				|  |  | -                log_ad.refresh_driver()
 | 
	
		
			
				|  |  |                  res = {'sucess': False, 'result_info': str(e)}
 | 
	
		
			
				|  |  |                  sql_tools.action_record(res, sql_session, ad_plan_create_action, user_id, plan_name,
 | 
	
		
			
				|  |  |                                          action_record_table,
 | 
	
		
			
				|  |  |                                          service_name, wechat_name, task_name)
 | 
	
		
			
				|  |  |                  raise
 | 
	
		
			
				|  |  | +            finally:
 | 
	
		
			
				|  |  | +                log_ad.refresh_driver()
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        # 每次运行微信相关操作,对微信相关信息进行刷新
 | 
	
		
			
				|  |  | -        get_human_info(user_id, log_ad, db, cookie_canuse, task_name)
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        check_task_in_hand(user_id, db, log_ad)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      except Exception as e:
 | 
	
		
			
				|  |  |          sql_tools.update_task_status_error(sql_session=sql_session, user_id=user_id, task_name=task_name)
 | 
	
	
		
			
				|  | @@ -99,11 +103,11 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse, task_name):
 | 
	
		
			
				|  |  |          logging.error(e)
 | 
	
		
			
				|  |  |          raise
 | 
	
		
			
				|  |  |      finally:
 | 
	
		
			
				|  |  | -        try:
 | 
	
		
			
				|  |  | -            print('任务结束')
 | 
	
		
			
				|  |  | -            log_ad.driver.quit()
 | 
	
		
			
				|  |  | -        except:
 | 
	
		
			
				|  |  | -            pass
 | 
	
		
			
				|  |  | +        print('创建计划,任务结束')
 | 
	
		
			
				|  |  | +        # 每次运行微信相关操作,对微信相关信息进行刷新
 | 
	
		
			
				|  |  | +        sql_session.commit()
 | 
	
		
			
				|  |  | +        log_ad.driver.quit()
 | 
	
		
			
				|  |  | +        check_task_in_hand(user_id, db)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  def get_human_info(user_id, log_ad, db, cookie_canuse, task_name):
 | 
	
	
		
			
				|  | @@ -125,6 +129,17 @@ def get_human_info(user_id, log_ad, db, cookie_canuse, task_name):
 | 
	
		
			
				|  |  |          if not cookie_canuse:
 | 
	
		
			
				|  |  |              log_ad.cookies_save(log_ad)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        # 动作存放
 | 
	
		
			
				|  |  | +        object_name = ''
 | 
	
		
			
				|  |  | +        action_info = {'user_id': user_id, 'service_name': service_name,
 | 
	
		
			
				|  |  | +                       'wechat_name': wechat_name,
 | 
	
		
			
				|  |  | +                       'action_type': refresh_wechat_action, 'object_name': object_name, 'task_name': task_name,
 | 
	
		
			
				|  |  | +                       'status': 'todo'}
 | 
	
		
			
				|  |  | +        record_insert = sql_tools.save_action_record(action_record_info=action_info,
 | 
	
		
			
				|  |  | +                                                     table_action_record=action_record_table)
 | 
	
		
			
				|  |  | +        sql_session.execute(record_insert)
 | 
	
		
			
				|  |  | +        sql_session.commit()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          # wechat_info.每次都删除掉前面全部数据,进行更新
 | 
	
		
			
				|  |  |          # human_info 进行全局更新
 | 
	
		
			
				|  |  |          w_api = WechatApi(log_ad=log_ad)
 | 
	
	
		
			
				|  | @@ -132,7 +147,7 @@ def get_human_info(user_id, log_ad, db, cookie_canuse, task_name):
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          if not res_info['sucess']:
 | 
	
		
			
				|  |  |              dingtalk.send_message('user_id:{} 获取微信数据为空,进行检查'.format(user_id))
 | 
	
		
			
				|  |  | -            log_ad.driver.close()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |              sql_tools.action_record(res_info, sql_session, refresh_wechat_action, user_id,
 | 
	
		
			
				|  |  |                                      object_name, action_record_table,
 | 
	
		
			
				|  |  |                                      service_name, wechat_name, task_name)
 | 
	
	
		
			
				|  | @@ -143,12 +158,12 @@ def get_human_info(user_id, log_ad, db, cookie_canuse, task_name):
 | 
	
		
			
				|  |  |          delete_wechat_info(sql_session=sql_session, user_id=user_id)
 | 
	
		
			
				|  |  |          # 2.重新添加一遍相关数据
 | 
	
		
			
				|  |  |          for _ in res_info['result_list']:
 | 
	
		
			
				|  |  | -            service_name = _['service_name']
 | 
	
		
			
				|  |  | -            wechat_name = _['wechat_name']
 | 
	
		
			
				|  |  | +            service_name_wechat = _['service_name']
 | 
	
		
			
				|  |  | +            wechat_name_wechat = _['wechat_name']
 | 
	
		
			
				|  |  |              appid = _['appid']
 | 
	
		
			
				|  |  |              wxname = _['wxname']
 | 
	
		
			
				|  |  | -            wechat_info = {'appid': appid, 'wxname': wxname, 'service_name': service_name,
 | 
	
		
			
				|  |  | -                           'wechat_name': wechat_name,
 | 
	
		
			
				|  |  | +            wechat_info = {'appid': appid, 'wxname': wxname, 'service_name': service_name_wechat,
 | 
	
		
			
				|  |  | +                           'wechat_name': wechat_name_wechat,
 | 
	
		
			
				|  |  |                             'user_id': user_id}
 | 
	
		
			
				|  |  |              wechat_insert = save_wechat_info(wechat_info=wechat_info,
 | 
	
		
			
				|  |  |                                               table_wechat=wechat_info_table)
 | 
	
	
		
			
				|  | @@ -159,18 +174,19 @@ def get_human_info(user_id, log_ad, db, cookie_canuse, task_name):
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          # human info 相关数据进行更新
 | 
	
		
			
				|  |  |          for _ in res_info['result_list']:
 | 
	
		
			
				|  |  | -            service_name = _['service_name']
 | 
	
		
			
				|  |  | -            wechat_name = _['wechat_name']
 | 
	
		
			
				|  |  | +            service_name_human = _['service_name']
 | 
	
		
			
				|  |  | +            wechat_name_human = _['wechat_name']
 | 
	
		
			
				|  |  |              human_info = _['data']['list']
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              update_res = human_info_table.update() \
 | 
	
		
			
				|  |  | -                .where(human_info_table.c.service_name == service_name) \
 | 
	
		
			
				|  |  | -                .where(human_info_table.c.wechat_name == wechat_name) \
 | 
	
		
			
				|  |  | +                .where(human_info_table.c.service_name == service_name_human) \
 | 
	
		
			
				|  |  | +                .where(human_info_table.c.wechat_name == wechat_name_human) \
 | 
	
		
			
				|  |  |                  .values(human_info=human_info)
 | 
	
		
			
				|  |  |              update_res = sql_session.execute(update_res)
 | 
	
		
			
				|  |  |              sql_session.commit()
 | 
	
		
			
				|  |  |              if update_res.rowcount == 0:
 | 
	
		
			
				|  |  | -                human_info = {'service_name': service_name, 'wechat_name': wechat_name, 'human_info': human_info}
 | 
	
		
			
				|  |  | +                human_info = {'service_name': service_name_human, 'wechat_name': wechat_name_human,
 | 
	
		
			
				|  |  | +                              'human_info': human_info}
 | 
	
		
			
				|  |  |                  human_insert = save_human_info(human_info=human_info,
 | 
	
		
			
				|  |  |                                                 table_human=human_info_table)
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -178,13 +194,11 @@ def get_human_info(user_id, log_ad, db, cookie_canuse, task_name):
 | 
	
		
			
				|  |  |                  sql_session.commit()
 | 
	
		
			
				|  |  |              logging.info('update human info')
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        # 浏览器关闭
 | 
	
		
			
				|  |  | -        log_ad.driver.close()
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |          sql_tools.action_record(res_info, sql_session, refresh_wechat_action,
 | 
	
		
			
				|  |  |                                  user_id, object_name, action_record_table,
 | 
	
		
			
				|  |  |                                  service_name, wechat_name, task_name)
 | 
	
		
			
				|  |  | -        check_task_in_hand(user_id=user_id, db=db, log_ad=log_ad)
 | 
	
		
			
				|  |  | +        log_ad.refresh_driver()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      except Exception as e:
 | 
	
		
			
				|  |  |          res_info = {'sucess': False, 'result_info': str(e)}
 | 
	
		
			
				|  |  |          sql_tools.action_record(res_info, sql_session, refresh_wechat_action,
 | 
	
	
		
			
				|  | @@ -194,17 +208,17 @@ def get_human_info(user_id, log_ad, db, cookie_canuse, task_name):
 | 
	
		
			
				|  |  |          log_ad.driver.save_screenshot(
 | 
	
		
			
				|  |  |              'user_id:{}_time_{}_wechat_info_error.png'.format(user_id, datetime.now().strftime("%Y-%m-%d, %H:%M:%S")))
 | 
	
		
			
				|  |  |          dingtalk.send_message('user_id:{} 获取微信数据出错,进行检查\n{}'.format(user_id, str(e)))
 | 
	
		
			
				|  |  | +        log_ad.refresh_driver()
 | 
	
		
			
				|  |  |          logging.error(e)
 | 
	
		
			
				|  |  |          raise
 | 
	
		
			
				|  |  |      finally:
 | 
	
		
			
				|  |  | -        try:
 | 
	
		
			
				|  |  | -            print('任务结束')
 | 
	
		
			
				|  |  | -            log_ad.driver.quit()
 | 
	
		
			
				|  |  | -        except:
 | 
	
		
			
				|  |  | -            pass
 | 
	
		
			
				|  |  | +        print('刷新用户,任务结束')
 | 
	
		
			
				|  |  | +        sql_session.commit()
 | 
	
		
			
				|  |  | +        log_ad.driver.quit()
 | 
	
		
			
				|  |  | +        check_task_in_hand(user_id=user_id, db=db)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -def check_task_in_hand(user_id, db, log_ad):
 | 
	
		
			
				|  |  | +def check_task_in_hand(user_id, db):
 | 
	
		
			
				|  |  |      sql_session = db.DBSession()
 | 
	
		
			
				|  |  |      lines = sql_tools.get_task_in_hand_limit_one(user_id=user_id, sql_session=sql_session)
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -216,11 +230,9 @@ def check_task_in_hand(user_id, db, log_ad):
 | 
	
		
			
				|  |  |          ad_plan_list.append(json.loads(typesetting))
 | 
	
		
			
				|  |  |      if action_type == refresh_wechat_action:
 | 
	
		
			
				|  |  |          # 刷新log_ad,以免log_ad超过生命周期
 | 
	
		
			
				|  |  | -        log_ad.driver.quit()
 | 
	
		
			
				|  |  |          log_ad = LogIn(user_id=user_id)
 | 
	
		
			
				|  |  |          get_human_info(user_id, log_ad, db, True, task_name)
 | 
	
		
			
				|  |  |      if action_type == ad_plan_create_action:
 | 
	
		
			
				|  |  | -        log_ad.driver.quit()
 | 
	
		
			
				|  |  |          log_ad = LogIn(user_id=user_id)
 | 
	
		
			
				|  |  |          carry_plan(user_id, ad_plan_list, log_ad, db, True, task_name)
 | 
	
		
			
				|  |  |  
 |