Quellcode durchsuchen

定时任务规定时间区域

liweiquan vor 2 Jahren
Ursprung
Commit
9152f48ce6
3 geänderte Dateien mit 26 neuen und 20 gelöschten Zeilen
  1. 0 6
      app/routers/jm_homework.py
  2. 0 1
      app/routers/jm_job_info.py
  3. 26 13
      app/utils/cron_utils.py

+ 0 - 6
app/routers/jm_homework.py

@@ -61,9 +61,3 @@ def delete_jm_homework(jm_id: int, db: Session = Depends(get_db)):
         raise Exception("该作业正在被定时任务使用,不可删除")
     return crud.delete_jm_homework(db, jm_id)
 
-
-@router.get("/test")
-def get_test_dag(db: Session = Depends(get_db)):
-    jm_homework = crud.get_jm_homework_info(db, 83)
-    res = red_dag_and_format(jm_homework, db)
-    return res

+ 0 - 1
app/routers/jm_job_info.py

@@ -154,7 +154,6 @@ def api_execute_jm_job(encryption_id: str, db: Session = Depends(get_db)):
 @web_try()
 @sxtimeit
 def get_cron_expression(cron_expression: schemas.CronExpression):
-    print(cron_expression)
     cron = joint_cron_expression(cron_expression)
     return cron
 

+ 26 - 13
app/utils/cron_utils.py

@@ -8,7 +8,7 @@ def joint_cron_expression(item: schemas.CronExpression):
         if item.minute is not None:
             cron += '0/'+str(item.minute) + ' *'
         elif item.hour is not None:
-            cron += '0 0/'+str(item.hour)
+            cron += '0 4-22/'+str(item.hour)
         else: cron += '*'
         cron += ' * * *'
     elif item.cron_select_type == 1:
@@ -53,28 +53,32 @@ def joint_cron_expression(item: schemas.CronExpression):
         else: cron += ' *'
     else:
         cron = item.cron_expression
-    match_obj = check_cron_expression(cron)
-    if match_obj:
-        return cron
-    return "cron校验未通过"
+    check_cron_expression(cron)
+    check_cron_hour(cron)
+    return cron
 
+# reg_list = [
+#     "^((([0-9]|[1-5][0-9])(\\,|\\-|\\/){1}([1-9]|[1-5][0-9]))|([0-9]|[0-5][0-9])|(\\*))$",
+#     "^((([0-9]|[1][0-9]|2[0-3])(\\,|\\-|\\/){1}([1-9]|[1][0-9]|2[0-3]))|([0-9]|[1][0-9]|2[0-3])|(\\*))$",
+#     "^((([1-9]|[1-2][0-9]|3[01])(\\,|\\-|\\/){1}([1-9]|[1-2][0-9]|3[01]))|([1-9]|[1-2][0-9]|3[01])|(\\*)|(\\?))$",
+#     "^((([1-9]|[1][0-2])(\\,|\\-|\\/){1}([1-9]|[1][0-2]))|([1-9]|[1][0-2])|(\\*))$",
+#     "^((([1-7])(\\,|\\-|\\/){1}([1-7]))|([1-7])|(\\*)|(\\?))$"
+# ]
 
 def check_cron_expression(cron_expression):
-    print(cron_expression)
     cron_list = cron_expression.split(' ')
     unit_list = ['minute', 'hour', 'day', 'month', 'week']
     reg_list = [
-        "^((([0-9]|[1-5][0-9])(\\,|\\-|\\/){1}([1-9]|[1-5][0-9]))|([0-9]|[0-5][0-9])|(\\*))$",
-        "^((([0-9]|[1][0-9]|2[0-3])(\\,|\\-|\\/){1}([1-9]|[1][0-9]|2[0-3]))|([0-9]|[1][0-9]|2[0-3])|(\\*))$",
-        "^((([1-9]|[1-2][0-9]|3[01])(\\,|\\-|\\/){1}([1-9]|[1-2][0-9]|3[01]))|([1-9]|[1-2][0-9]|3[01])|(\\*)|(\\?))$",
-        "^((([1-9]|[1][0-2])(\\,|\\-|\\/){1}([1-9]|[1][0-2]))|([1-9]|[1][0-2])|(\\*))$",
-        "^((([1-7])(\\,|\\-|\\/){1}([1-7]))|([1-7])|(\\*)|(\\?))$"
-        ]
+        "^((([0-9]|[1-5][0-9])(\\,|\\-){1}([1-9]|[1-5][0-9])(\\/){1}([1-9]|[1-5][0-9]))|(([0-9]|[1-5][0-9])(\\,|\\-|\\/){1}([1-9]|[1-5][0-9]))|([0-9]|[0-5][0-9])|(\\*))$",
+        "^((([0-9]|[1][0-9]|2[0-3])(\\,|\\-){1}([1-9]|[1][0-9]|2[0-3])(\\/){1}([1-9]|[1][0-9]|2[0-3]))|(([0-9]|[1][0-9]|2[0-3])(\\,|\\-|\\/){1}([1-9]|[1][0-9]|2[0-3]))|([0-9]|[1][0-9]|2[0-3])|(\\*))$",
+        "^((([1-9]|[1-2][0-9]|3[01])(\\,|\\-){1}([1-9]|[1-2][0-9]|3[01])(\\/){1}([1-9]|[1-2][0-9]|3[01]))|(([1-9]|[1-2][0-9]|3[01])(\\,|\\-|\\/){1}([1-9]|[1-2][0-9]|3[01]))|([1-9]|[1-2][0-9]|3[01])|(\\*)|(\\?))$",
+        "^((([1-9]|[1][0-2])(\\,|\\-){1}([1-9]|[1][0-2])(\\/){1}([1-9]|[1][0-2]))|(([1-9]|[1][0-2])(\\,|\\-|\\/){1}([1-9]|[1][0-2]))|([1-9]|[1][0-2])|(\\*))$",
+        "^((([1-7])(\\,|\\-){1}([1-7])(\\/){1}([1-7]))|(([1-7])(\\,|\\-|\\/){1}([1-7]))|([1-7])|(\\*)|(\\?))$"
+    ]
     for cron, unit, reg in zip(cron_list, unit_list, reg_list):
         match_obj = re.match(reg, cron)
         if match_obj is None:
             raise Exception(f'在{unit}的位置上,数据校验错误')
-    return True
 
 def parsing_cron_expression(cron_expression):
     cron_list = cron_expression.split(' ')
@@ -85,3 +89,12 @@ def parsing_cron_expression(cron_expression):
             cron_dict.update({unit: cron.replace('0/','').replace('1/','')})
     return cron_dict
 
+def check_cron_hour(cron_expression):
+    cron_list = cron_expression.split(' ')
+    unit_list = ['minute', 'hour', 'day', 'month', 'week']
+    for cron, unit in zip(cron_list, unit_list):
+        if unit == 'hour':
+            print(cron)
+            match_obj = re.match("^((([4-9]|[1][0-9]|2[0-2])(\\,|\\-){1}([4-9]|[1][0-9]|2[0-2])(\\/){1}([1-9]|[1][0-9]|2[0-3]))|(([4-9]|[1][0-9]|2[0-2])(\\,|\\-){1}([4-9]|[1][0-9]|2[0-2]))|(([4-9]|[1][0-9]|2[0-2])(\\/){1}([1-9]|[1][0-9]|2[0-3]))|([4-9]|[1][0-9]|2[0-2])|(\\*))$", cron)
+            if match_obj is None:
+                raise Exception('执行时间必须在每日4~22点内')