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

Add job to existing scheduler process #155

Open
andrewtmendoza opened this issue Aug 31, 2021 · 5 comments
Open

Add job to existing scheduler process #155

andrewtmendoza opened this issue Aug 31, 2021 · 5 comments

Comments

@andrewtmendoza
Copy link

@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!

@jcass77
Copy link
Owner

jcass77 commented Aug 31, 2021

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.

@nitishxp
Copy link

@jcass77 any update over this ?

@jcass77
Copy link
Owner

jcass77 commented Oct 21, 2021

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.

@TEag1e
Copy link

TEag1e commented Feb 15, 2022

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()

@Ch00k
Copy link

Ch00k commented Nov 10, 2024

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 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 add_job(), and that takes care of persisting the job and passing it on to the executor.

What am I missing here? Why do you think that I would not be able to call add_job() from outside the process that started the scheduler itself?

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 add_job() from a Django view gives me Adding job tentatively -- it will be properly scheduled when the scheduler starts. So, indeed, the state of the scheduler is local to the process it was started in.

The piece of documentation I was referring to probably means that you could use BackgroundScheduler, and add jobs to it at any time, but only within the same process it was started in.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants