63 lines
1.6 KiB
Python
63 lines
1.6 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
|
from web.config.apscheduler_config import ApschedulerConfig
|
|
|
|
|
|
class ApschedulerManager:
|
|
"""
|
|
apscheduler管理器
|
|
"""
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
self.apscheduler = BackgroundScheduler(executors=ApschedulerConfig.executors,
|
|
job_defaults=ApschedulerConfig.job_defaults,
|
|
jobstores=ApschedulerConfig.job_stores, timezone='Asia/Shanghai')
|
|
|
|
def create(self, func_, job_id, seconds_, kwargs_):
|
|
"""
|
|
创建定时任务
|
|
"""
|
|
job = self.apscheduler.add_job(func_, 'interval', id=job_id, seconds=seconds_, kwargs=kwargs_, replace_existing=True)
|
|
self.apscheduler.start()
|
|
return job
|
|
|
|
def delete(self, job_id):
|
|
"""
|
|
删除定时任务
|
|
"""
|
|
self.apscheduler.remove_job(job_id)
|
|
|
|
def pause(self, job_id):
|
|
"""
|
|
暂停定时任务
|
|
"""
|
|
self.apscheduler.pause_job(job_id)
|
|
|
|
def resume(self, job_id):
|
|
"""
|
|
恢复定时任务
|
|
"""
|
|
self.apscheduler.resume_job(job_id)
|
|
|
|
def shutdown_apscheduler(self):
|
|
"""
|
|
关闭调度器
|
|
"""
|
|
self.apscheduler.shutdown()
|
|
|
|
def pause_apscheduler(self):
|
|
"""
|
|
暂停调度器
|
|
"""
|
|
self.apscheduler.pause()
|
|
|
|
def resume_apscheduler(self):
|
|
"""
|
|
恢复调度器
|
|
"""
|
|
self.apscheduler.resume()
|