-
Notifications
You must be signed in to change notification settings - Fork 97
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
Add job to existing scheduler process #155
Comments
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: agronholm/apscheduler#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() |
@jcass77 I am a somewhat confused by your response to this issue. I have a similar use case to the OP's: have APScheduler running in a separate process, and add jobs to it from Django views. According to APScheduler 3.x User Guide's Adding jobs section, "You can schedule jobs on the scheduler at any time" (emphasis in original), which I take is exactly what I need, and it does not seem to be dependent on the job store in use: you just call What am I missing here? Why do you think that I would not be able to call UPD: I tried to implement the use case I have, and I think I understand what you meant. With APScheduler running in a separate process, executing The piece of documentation I was referring to probably means that you could use I am currently looking at https://github.com/django-q2/django-q2 (which I already use in another project, but purely as a task runner, i.e. without schedules) as an alternative to APScheduler. |
@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!
The text was updated successfully, but these errors were encountered: