Wednesday, 9 March 2022

maximum number of running instances

I'm using apscheduler-django and I created a task that loops every 10 seconds

this function will make a request to API and save the content to my database (postgres)

this my task :

scheduler.add_job(
  SaveAPI,
  trigger=CronTrigger(second="*/10"), 
  id="SaveAPI", 
  max_instances=1,
  replace_existing=True,
)

and my SaveAPI is :

def SaveAPI():
    SPORT = 3
    print('API Queue Started')
    AllMatches = GetAllMatches(SPORT)
    for Match in AllMatches:
        AddToDatabase(Match, SPORT)
    print(f'API Queue Ended')

the GetAllMatches and AddToDatabase are too big and I don't this it's relevant to my question.

so my problem is: sometimes I will get this error:

Run time of job "SaveAPI (trigger: cron[second='*/10'], next run at: 2022-03-05 23:21:00 +0330)" was missed by 0:00:11.445357

when this happens. it will never get replaced with new instance because my SaveAPI function never ends. and apscheduler will always miss new instances.

I did many tests and function does not have any problem.

how can I make apscheduler to stop the last running instance if new instance is going to miss ?

so if my last instance takes more than 10 seconds , just terminate the instnace and create new one !



from maximum number of running instances

No comments:

Post a Comment