Skip to content

Commit

Permalink
在SqlWorkflow中添加一列,timing_task_id
Browse files Browse the repository at this point in the history
  • Loading branch information
woshiyanghai committed Sep 3, 2024
1 parent 01ba0fd commit 9fc3995
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions sql/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ class SqlWorkflow(models.Model, WorkflowAuditMixin):
engineer = models.CharField("发起人", max_length=30)
engineer_display = models.CharField("发起人中文名", max_length=50, default="")
status = models.CharField(max_length=50, choices=SQL_WORKFLOW_CHOICES)
timing_task_id=models.CharField("定时task_id", max_length=100, default="")
audit_auth_groups = models.CharField("审批权限组列表", max_length=255)
run_date_start = models.DateTimeField("可执行起始时间", null=True, blank=True)
run_date_end = models.DateTimeField("可执行结束时间", null=True, blank=True)
Expand Down
13 changes: 8 additions & 5 deletions sql/sql_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import datetime
import logging
import traceback

import time
import simplejson as json
from django.contrib.auth.decorators import permission_required
from django.core.exceptions import PermissionDenied
Expand Down Expand Up @@ -321,7 +321,7 @@ def execute(request):
update_fields=["status"]
)
# 删除定时执行任务
schedule_name = f"sqlreview-timing-{workflow_id}"
schedule_name=SqlWorkflow(id=workflow_id).timing_task_id
del_schedule(schedule_name)
# 加入执行队列
execute_sql_excute.apply_async(
Expand Down Expand Up @@ -398,8 +398,10 @@ def timing_task(request):
return render(request, "error.html", context)

run_date = datetime.datetime.strptime(run_date, "%Y-%m-%d %H:%M")
schedule_name = f"sqlreview-timing-{workflow_id}"

timing_task_id=workflow_detail.timing_task_id
del_schedule(timing_task_id)
task_time = time.time()
schedule_name = f"sqlreview-timing-{workflow_id}-{task_time}"
if on_correct_time_period(workflow_id, run_date) is False:
context = {
"errMsg": "不在可执行时间范围内,如果需要修改执 行时间请重新提交工单!"
Expand All @@ -411,6 +413,7 @@ def timing_task(request):
with transaction.atomic():
# 将流程状态修改为定时执行
workflow_detail.status = "workflow_timingtask"
workflow_detail.timing_task_id=schedule_name
workflow_detail.save()
# 调用添加定时任务
add_sql_schedule(schedule_name, run_date, workflow_id)
Expand Down Expand Up @@ -474,7 +477,7 @@ def cancel(request):
sql_workflow.save()
# 删除定时执行task
if sql_workflow.status == "workflow_timingtask":
del_schedule(f"sqlreview-timing-{workflow_id}")
del_schedule(sql_workflow.timing_task_id)
# 发送取消、驳回通知,开启了Cancel阶段通知参数才发送消息通知
sys_config = SysConfig()
is_notified = (
Expand Down
1 change: 0 additions & 1 deletion sql/utils/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
logger = logging.getLogger("default")

def add_sql_schedule(name, run_date, workflow_id):
del_schedule(name)
# 使用 Celery 的 apply_async 方法来调度任务
# 因这里存在循环调用问题,所以不能直接import execute
sig = signature('sql.utils.execute_sql.execute', args=(workflow_id,))
Expand Down

0 comments on commit 9fc3995

Please sign in to comment.