Unable to execute newly added jobs

45 views
Skip to first unread message

anubhav kumar

unread,
May 20, 2021, 3:10:35 AM5/20/21
to APScheduler
Hi,

I am trying to load the jobs from job store by initializing the scheduler in the init method. The scheduler adds the jobs from the job store successfully but is unable to execute to the jobs that I add at the later point with the help of celery tasks.

Following is the code to reproduce

def start_survey():
    print("Hello")

class WorkflowSchedular():
    def __init__(self):
        self.create_scheduler()

    def create_scheduler(self):
        db_name = "schedular_db"
         collection_name = "workflow_schedular"
          jobstores = {
                             "mongo":  MongoDBJobStore(
                                                    database=db_name,
                                                     collection=collection_name,
                                                      host="mongo_ip",    # My Mongo IP
                                                      port="mongo_port" # My Mongo Port 
                                                  )
          }
          self.scheduler = BackgroundScheduler(
                  jobstores=jobstores, daemon=True
              )
          self.scheduler.start()

    def handle_workflow_manager(self, job_id, scheduler_data):
        self.scheduler.add_job(
                  start_survey,
                  trigger="date",
                  run_date=datetime.strptime(
                      f"{scheduler_data['start_date']}:00", "%Y-%m-%d %H:%M:%S"
                  ),
                  id=job_id,
                  timezone=scheduler_data["timezone"],
                  jobstore="mongo"
              )

Here the handle_workflow_manager via a celery task. Any jobs that are added after starting the celery worker does trigger at the specified time. When I restart the worker again and it loads the jobs again from the jobstore if shows that a job was missed which is the same job that was added via celery task.

Note: This issue is does not take place if I call create_scheduler from the handle_workflow_manager. But in that case I will not be able to load the old jobs at the start.

Please tell me If I am missing something here. 
Thanks in advance.
Reply all
Reply to author
Forward
0 new messages