forked from jly8866/archer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs.py
57 lines (45 loc) · 1.76 KB
/
jobs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding:utf-8 -*-
import datetime
import time
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.schedulers import SchedulerAlreadyRunningError, SchedulerNotRunningError
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job
from sql.const import Const
from sql.models import workflow
from .sqlreview import execute_job, getDetailUrl
import logging
logging.basicConfig()
logging.getLogger('apscheduler').setLevel(logging.DEBUG)
logger = logging.getLogger('default')
# 初始化scheduler
scheduler = BackgroundScheduler()
scheduler.add_jobstore(DjangoJobStore(), "default")
register_events(scheduler)
try:
scheduler.start()
logger.debug("Scheduler started!")
except SchedulerAlreadyRunningError:
logger.debug("Scheduler is already running!")
# 添加/修改sql执行任务
def add_sqlcronjob(job_id, run_date, workflowId, url):
scheduler = BackgroundScheduler()
scheduler.add_jobstore(DjangoJobStore(), "default")
scheduler.add_job(execute_job, 'date', run_date=run_date, args=[workflowId, url], id=job_id,
replace_existing=True)
register_events(scheduler)
try:
scheduler.start()
logger.debug("Scheduler started!")
except SchedulerAlreadyRunningError:
logger.debug("Scheduler is already running!")
logger.debug('add_sqlcronjob:' + job_id + " run_date:" + run_date.strftime('%Y-%m-%d %H:%M:%S'))
# 删除sql执行任务
def del_sqlcronjob(job_id):
logger.debug('del_sqlcronjob:' + job_id)
return scheduler.remove_job(job_id)
# 获取任务详情
def job_info(job_id):
return scheduler.get_job(job_id)