1
job_stores = {
                'default': MongoDBJobStore(database=databasename,
                                           client=clientname, collection="schedulejob")
            }
            executors = {
                'default': {'type': 'threadpool', 'max_workers': 5}
            }
            job_defaults = {
                'coalesce': True,  # When the same task is triggered multiple times at the same time, it runs only once
                'max_instances': 3,
                'misfire_grace_time': 3600,  # The task is still executed after 30 seconds of expiration
            }
            global sched
            sched = BackgroundScheduler(jobstores=job_stores, executors=executors, job_defaults=job_defaults, daemon=True)
sched.add_job(helloworld, 'interval', hours=3, args=(),
                                  name=name)

I can see schedulejob is created with single entry, but on respective timestamp, my same function helloworld got executed four times.

Note: I am using Flask server with 4 nodes. Is the four nodes being the reason.

SushantPatade
  • 254
  • 2
  • 12
  • Have you read the [FAQ entry](https://apscheduler.readthedocs.io/en/stable/faq.html#how-do-i-use-apscheduler-in-a-web-application) about running apscheduler in web apps? – Alex Grönholm Jul 18 '21 at 09:04
  • Yes I think the number of nodes could be the problem. Could you change it to 2 and see what happens? – Tanveer Jul 19 '21 at 05:02
  • Yes, number of nodes are the issues. Any solution ? In flask is it possible that out of multiple node, set of code is only triggered from one node server ? I am trying to understanding FAQ entry shared by Alex. – SushantPatade Jul 19 '21 at 11:00
  • See https://stackoverflow.com/questions/16053364/make-sure-only-one-worker-launches-the-apscheduler-event-in-a-pyramid-web-app-ru. You need to do something similar. Basically your code is instantiated number of nodes times. In Flask app try calling your scheduler outside the main function. – Tanveer Jul 20 '21 at 04:21

0 Answers0