django-apscheduler
django-apscheduler copied to clipboard
Add job to existing scheduler process
@jcass77 I apologize if this is covered elsewhere, but I haven't been able to find anything for this particular use case in the docs or Stack Overflow.
If I use the recommended deployment scenario and create a custom Django management command to start a single scheduler in its own dedicated process, is there any way for me to access that scheduler to add additional jobs from a regular Django process? What I would like to do is have a Django view I can trigger to add/remove/modify a scheduled periodic job. This is something I've done with celery beat in the past, but I'm curious to see if I can achieve the same thing with django-apscheduler.
Thanks!
Unfortunately APScheduler does not currently provide a mechanism for the job store to notify the scheduler when new jobs are added to the job store.
But this feature is in the works (see: https://github.com/agronholm/apscheduler/issues/465) and we will revisit django-apschdeler's implementation to potentially make use of the new APScheduler 4 interfaces once they have stabilised.
So for now the django-apscheduler schedules can only be maintained in the Python code of the Django management command.
@jcass77 any update over this ?
We're waiting for APScheduler 4.0 to be released - you can track the status on that project's repository using the link to the issue in the comments above.
About CRUD,That's what I wrote。
from django_apscheduler.jobstores import DjangoJobStore
from apscheduler.triggers.cron import CronTrigger
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.job import Job as AppSchedulerJob
django_job_store = DjangoJobStore()
list all job
django_job_store.get_all_jobs()
look up a job
django_job_store.lookup_job(job_id)
delete a job
django_job_store.remove_job(job_id)
delete all job
django_job_store.remove_all_jobs()
change trigger
def modify_job(job_id: str, crontab_exp: str):
"""修改定时任务的调度周期"""
job: AppSchedulerJob = django_job_store.lookup_job(job_id)
job.trigger = CronTrigger.from_crontab(crontab_exp)
django_job_store.update_job(job)
pause job
def pause_job(job_id: str):
"""暂停定时任务"""
job: AppSchedulerJob = django_job_store.lookup_job(job_id)
job.next_run_time = None
django_job_store.update_job(job)
resume job
def resume_job(job_id: str):
"""激活定时任务"""
job: AppSchedulerJob = django_job_store.lookup_job(job_id)
# 序列化Job对象为字典类型
job_state = job.__getstate__()
del job_state['next_run_time']
scheduler = BackgroundScheduler()
scheduler.add_jobstore(django_job_store)
scheduler.add_job(replace_existing=True, **job_state)
scheduler.start()