def fun_task_timeout_monitor(ds, dag, execution_date, **op_kwargs): dag_ids = dag.dag_id # 监控国家 v_country_code = 'NG' # 时间偏移量 v_gap_hour = 0 v_date = GetLocalTime("otrade", execution_date.strftime("%Y-%m-%d %H"), v_country_code, v_gap_hour)['date'] v_hour = GetLocalTime("otrade", execution_date.strftime("%Y-%m-%d %H"), v_country_code, v_gap_hour)['hour'] # 小时级监控 tb_hour_task = [{ "dag": dag, "db": "otrade_dw", "table": "{dag_name}".format(dag_name=dag_ids), "partition": "country_code={country_code}/dt={pt}/hour={now_hour}".format( country_code=v_country_code, pt=v_date, now_hour=v_hour), "timeout": "3000" }] TaskTimeoutMonitor().set_task_monitor(tb_hour_task)
def get_local_date_time(self,country_code): """ 通过函数GetLocalTime ,安装国家码获取时区对应的本地时间 """ v_utc_time='{v_sys_utc}'.format(v_sys_utc=self.v_utc_ds+" "+self.v_utc_hour) #国家对应的本地日期 self.v_local_date=GetLocalTime(self.v_business_key,'{v_utc_time}'.format(v_utc_time=v_utc_time),country_code,self.v_time_offset)["date"] #国家对应的本地小时 self.v_local_hour=GetLocalTime(self.v_business_key,'{v_utc_time}'.format(v_utc_time=v_utc_time),country_code,self.v_time_offset)["hour"]
def alter_partition(self): """ alter partition 语句(包含单国家、多国家) """ alter_str="" # 没有国家分区 && 小时参数为None if self.v_is_country_partition.lower()=="false" and self.v_is_hour_task.lower()=="false": v_par_str="dt='{ds}'".format(ds=self.utc_ds) alter_str="alter table {db}.{table_name} drop partition({v_par});\n alter table {db}.{table_name} add partition({v_par});".format(v_par=v_par_str,table_name=self.v_table_name,db=self.v_db_name) return alter_str # 没有国家分区 && 小时参数不为None if self.v_is_country_partition.lower()=="false" and self.v_is_hour_task.lower()=="true": v_par_str="dt='{ds}',hour='{hour}'".format(ds=self.utc_ds,hour=self.utc_hour) alter_str="alter table {db}.{table_name} drop partition({v_par});\n alter table {db}.{table_name} add partition({v_par});".format(v_par=v_par_str,table_name=self.v_table_name,db=self.v_db_name) return alter_str #有国家分区 for country_code_word in self.country_code_list.split(","): # 有国家分区 && 小时参数为None if self.v_is_country_partition.lower()=="true" and self.v_is_hour_task.lower()=="false": v_par_str="country_code='{country_code}',dt='{ds}'".format(ds=self.utc_ds,country_code=country_code_word) alter_str=alter_str+"\n"+"alter table {db}.{table_name} drop partition({v_par});\n alter table {db}.{table_name} add partition({v_par});".format(v_par=v_par_str,table_name=self.v_table_name,db=self.v_db_name) # 多国家(utc)分区 && 小时参数不为None && utc分区 if self.v_is_country_partition.lower()=="true" and self.v_is_hour_task.lower()=="true" and self.v_frame_type.lower()=="utc": v_par_str="country_code='{country_code}',dt='{ds}',hour='{hour}'".format(ds=self.utc_ds,hour=self.utc_hour,country_code=country_code_word) alter_str=alter_str+"\n"+"alter table {db}.{table_name} drop partition({v_par});\n alter table {db}.{table_name} add partition({v_par});".format(v_par=v_par_str,table_name=self.v_table_name,db=self.v_db_name) # 多国家(本地时间)分区 && 小时参数不为None && 本地时间 if self.v_is_country_partition.lower()=="true" and self.v_is_hour_task.lower()=="true" and self.v_frame_type.lower()=="local": #yyyy-MM-dd hh v_utc_time='{v_sys_utc}'.format(v_sys_utc=self.utc_ds+" "+self.utc_hour) #国家本地日期 v_local_date=GetLocalTime('opay','{v_utc_time}'.format(v_utc_time=v_utc_time),country_code_word,self.time_offset)["date"] #国家本地小时 v_local_hour=GetLocalTime('opay','{v_utc_time}'.format(v_utc_time=v_utc_time),country_code_word,self.time_offset)["hour"] #表分区,时间是本地时间 v_par_str="country_code='{country_code}',dt='{ds}',hour='{hour}'".format(ds=v_local_date,hour=v_local_hour,country_code=country_code_word) alter_str=alter_str+"\n"+"alter table {db}.{table_name} drop partition({v_par});\n alter table {db}.{table_name} add partition({v_par});".format(v_par=v_par_str,table_name=self.v_table_name,db=self.v_db_name) return alter_str
def exist_country_code_data_dir(self,object_task): """ country_partition:是否有国家分区 file_type:是否空文件也生成 success """ try: #获取国家列表 for country_code_word in self.country_code_list.split(","): if country_code_word.lower()=='nal': country_code_word=country_code_word.lower() else: country_code_word=country_code_word.upper() #没有小时级分区 if self.v_is_hour_task.lower()=="false": #输出不同国家的数据路径(没有小时级分区) self.hdfs_data_dir_str=self.v_data_oss_path+"/country_code="+country_code_word+"/dt="+self.utc_ds #(多国家)UTC 小时分区 if self.v_is_hour_task.lower()=="true" and self.v_frame_type.lower()=="utc": #输出不同国家(UTC时间)的数据路径(UTC 小时级分区) self.hdfs_data_dir_str=self.v_data_oss_path+"/country_code="+country_code_word+"/dt="+self.utc_ds+"/hour="+self.utc_hour #(多国家)Local 小时分区 if self.v_is_hour_task.lower()=="true" and self.v_frame_type.lower()=="local": v_utc_time='{v_sys_utc}'.format(v_sys_utc=self.utc_ds+" "+self.utc_hour) #国家本地日期 v_local_date=GetLocalTime('opay','{v_utc_time}'.format(v_utc_time=v_utc_time),country_code_word,self.time_offset)["date"] #国家本地小时 v_local_hour=GetLocalTime('opay','{v_utc_time}'.format(v_utc_time=v_utc_time),country_code_word,self.time_offset)["hour"] #输出不同国家(本地时间)的数据路径(Local 小时级分区) self.hdfs_data_dir_str=self.v_data_oss_path+"/country_code="+country_code_word+"/dt="+v_local_date+"/hour="+v_local_hour #没有开通多国家业务(国家码默认nal) if self.v_is_country_partition.lower()=="true" and self.v_is_countries_online.lower()=="false": #必须有数据才可以生成Success 文件 if self.v_is_result_force_exist.lower()=="true": object_task() #数据为空也生成 Success 文件 if self.v_is_result_force_exist.lower()=="false": object_task() #开通多国家业务 if self.v_is_country_partition.lower()=="true" and self.v_is_countries_online.lower()=="true": #刚刚开国的国家(按照false处理) if self.v_country_code_map[country_code_word].lower()=="new": #删除多国家分区使用 if self.v_del_flag==1: object_task() continue else: self.data_not_file_type_touchz() continue #业务开国很久,必须有数据才可以生成Success 文件 if self.v_is_result_force_exist.lower()=="true": #删除多国家分区使用 if self.v_del_flag==1: object_task() continue #在必须有数据条件下:国家是nal时,数据可以为空 if country_code_word=="nal": self.data_not_file_type_touchz() else: object_task() #数据为空也生成 Success 文件 if self.v_is_result_force_exist.lower()=="false": object_task() except Exception as e: #self.dingding_alert.send('DW调度系统任务 {jobname} 数据产出异常'.format(jobname=table_name)) logging.info(e) sys.exit(1)