question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use apscheduler to dynamically add jobs I use it in a wrong way?

See original GitHub issue

Step 1. this is my add.py to add jobs

from datetime import datetime, timedelta
import sys
import os

from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.jobstores.redis import RedisJobStore
import logging

jobstores = {
    'default': RedisJobStore(host='localhost', port=6379)
}

scheduler = BackgroundScheduler(jobstores=jobstores)

def alarm():
    time = datetime.now()
    os.system("echo \"Alarm! This alarm was scheduled at %s\" >> /var/log/test.log"%(time))

def add(task_func, task_id):
    #print task_func,task_id
    scheduler.add_job(task_func, 'interval', id=task_id, name=task_id, seconds=10)


if __name__ == '__main__':
    add(alarm, 'my_job1')
    add(alarm, 'my_job2')
    scheduler.start()
    scheduler.shutdown()

I added two jobs first, then start and shutdown

  1. I just used add_job but it will not really add it.
  1. So I did start and shutdown. But I don’t really sure that it need shut down or not.

Step 2 .and then start test.py ; python test.py

from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.jobstores.redis import RedisJobStore
jobstores = {
        'default': RedisJobStore(host='localhost', port=6379)
}
scheduler = BackgroundScheduler(jobstores=jobstores)

if __name__ == '__main__':
    #scheduler.add_job(alarm, 'interval', id='my_job0', seconds=10)
    try:
        scheduler.start()
        # This is here to simulate application activity (which keeps the main thread alive).
        while True:
            time.sleep(2)
    except (KeyboardInterrupt, SystemExit):
        # Not strictly necessary if daemonic mode is enabled but should be done if possible
        print "shutdown scheduler"
        scheduler.shutdown()

it works, but when I add a new job id=my_job3 it did nothing.

Expected Behavior

I want to dynamic add, get , remove jobs when apsheduler is starting. get jobs -> it works, but not sure I use it in a right way. Can you give me a best way? or more use case?

from apscheduler.jobstores.redis import RedisJobStore
RedisJobStore().get_all_jobs()

remove jobs -> it works, but not sure I use it in a right way. Can you give me a best way? or more use case?

from apscheduler.jobstores.redis import RedisJobStore
RedisJobStore().remove_all_jobs()

Current Behavior

Steps to Reproduce

what I am trying is: step 1. add two jobs step 2. starting apsheduler -> successful step 3. add a new job -> fail

Context (Environment)

APScheduler==3.6.3 redis==3.5.3 ubuntu 16.04

Detailed Description

I try others way like: step1. -> step2. -> kill python test.py -> step 3. -> restart test.py (it works) But I am not sure it is a good way to use. When add new job must kill main thread? any option to restart? must kill and then restart?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
agronholmcommented, Nov 6, 2020

You never said that you’re adding jobs from a different process than where the scheduler is running. This is not supported in APScheduler 3. The FAQ explains why this won’t work and provides a workaround.

0reactions
RyanHuang1102commented, Nov 9, 2020

OK, thank you very much

Read more comments on GitHub >

github_iconTop Results From Across the Web

User guide — APScheduler 3.9.1 documentation
The add_job() method returns a apscheduler. job. Job instance that you can use to modify or remove the job later.
Read more >
Python Apscheduler - Schedule Jobs dynamically (nested)
Save this question. Show activity on this post. We have a requirement to schedule multiple jobs dynamically while current job is executing.
Read more >
Pretty stuck: dynamic scheduling of app with Django/Heroku ...
I think I need some combination of a clock process (using APScheduler) and a worker process that retrieves jobs from the queue (using...
Read more >
APScheduler: Dynamic Task Scheduling Library in Python
To overcommunicate, a Scheduler manages the jobs; and a Trigger is to initiate the execution of each job. While we are using a...
Read more >
[Answered]-APScheduler job is not starting as scheduled-django
Another problem must be the use of scheduler.scheduled_job() method. If you read the documentation on adding jobs, you will observe that the recomended...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found