Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用 django-Q 做定时执行 #22

Merged
merged 2 commits into from
Dec 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions archery/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
'django_apscheduler',
'sql',
'themis',
'django_q',
)

MIDDLEWARE = (
Expand Down Expand Up @@ -139,6 +140,22 @@
},
}

Q_CLUSTER = {
'name': 'archery',
'workers': 8,
'recycle': 500,
'timeout': 60,
'compress': True,
'cpu_affinity': 1,
'save_limit': 250,
'queue_limit': 500,
'label': 'Django Q',
'redis': {
'host': '127.0.0.1',
'port': 6379,
'db': 0, }
}

# 缓存配置
CACHES = {
'default': {
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ python-dateutil==2.7.3
pymongo==3.7.1
wtforms_json==0.3.3
mysql-replication==0.18
django-q==1.0.1
redis==3.0.1
36 changes: 16 additions & 20 deletions sql/utils/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,37 @@
from apscheduler.schedulers import SchedulerAlreadyRunningError
from django_apscheduler.jobstores import DjangoJobStore, register_events

from django_q.tasks import async_task, result, schedule
from django_q.models import Schedule

from sql.utils.execute_sql import execute_job

import logging

logger = logging.getLogger('default')

# 初始化scheduler
scheduler = BackgroundScheduler()
scheduler.add_jobstore(DjangoJobStore(), "default")
register_events(scheduler)
try:
scheduler.start()
except SchedulerAlreadyRunningError:
logger.debug("Scheduler is already running!")


# 添加/修改sql执行任务
def add_sqlcronjob(job_id, run_date, workflow_id, url):
scheduler = BackgroundScheduler()
scheduler.add_jobstore(DjangoJobStore(), "default")
scheduler.add_job(execute_job, 'date', run_date=run_date, args=[workflow_id, url], id=job_id, replace_existing=True)
register_events(scheduler)
try:
scheduler.start()
except SchedulerAlreadyRunningError:
logger.debug("Scheduler is already running!")
del_sqlcronjob(job_id)
schedule('sql.utils.execute_sql.execute_job',workflow_id, url, name=job_id ,schedule_type='O', next_run=run_date, repeats=1)
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)
try:
sql_schedule = Schedule.objects.get(name=job_id)
Schedule.delete(sql_schedule)
logger.debug('del_sqlcronjob:' + job_id)
except Schedule.DoesNotExist :
logger.debug('del_sqlcronjob {} failed, job does not exist'.format(job_id))


# 获取任务详情
def job_info(job_id):
return scheduler.get_job(job_id)
try:
sql_schedule = Schedule.objects.get(name=job_id)
return sql_schedule
except Schedule.DoesNotExist:
pass
2 changes: 1 addition & 1 deletion sql/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def detail(request, workflow_id):
job_id = Const.workflowJobprefix['sqlreview'] + '-' + str(workflow_id)
job = job_info(job_id)
if job:
run_date = job.next_run_time
run_date = job.next_run
else:
run_date = ''
else:
Expand Down