Can we force jobs to run in the APScheduler job storage?

Using APScheduler version 3.0.3. In my application, internal applications use APScheduler to schedule and run tasks. I also created a wrapper class around the actual APScheduler (facade only, helps with unit tests). For unit testing of these services, I can mock this wrapper class. But I have a situation where I would really like the APScheduler to complete the task (during the test). Is there a way I can get it to start work?

+4
source share
2 answers

There is no default trigger for starting a task immediately, to achieve this you can get the current time and set the DateTrigger for the task as follows:

my_job.modify_job(trigger=DateTrigger(run_date=datetime.datetime.now()))

this way you will "force" to complete the task, but you need to insert the task again in the scheduler, another option simply creates a new task to run the same function using the add_job function

sched.add_job(func=your_function(),
              trigger=DateTrigger(run_date=datetime.datetime.now()))

this way you do not need to take any extra steps.

+4
source

Another approach: you can write the logic of your work in a separate function. Thus, you can call this function in your scheduled task, as well as in another place. I guess this is a more explicit way to do what you want.

0
source

Source: https://habr.com/ru/post/1612223/


All Articles