APScheduler how to get started now

I have an APScheduler application in a Flask application that dispatches events at regular intervals.

Now I need to โ€œupdateโ€ all tasks, because just start them now if they do not start without touching a certain interval.

I'v tried calling job.pause (), then job.resume () and nothing, and using the job. reschedule_job (...) will call it, but also change the interval ... which I don't want.

My actual code is below:

cron = GeventScheduler(daemon=True) # Explicitly kick off the background thread cron.start() cron.add_job(_job_time, 'interval', seconds=5, id='_job_time') cron.add_job(_job_forecast, 'interval', hours=1, id='_job_forecast_01') @app.route("/refresh") def refresh(): refreshed = [] for job in cron.get_jobs(): job.pause() job.resume() refreshed.append(job.id) return json.dumps( {'number': len(cron.get_jobs()), 'list': refreshed} ) 
+5
source share
3 answers

You can simply run the job function directly.

 for job in cron.get_jobs(): job.func() 

If you have arguments or kwargs to be passed to the function, you will have to pull them out of job.args and / or job.kwargs . See apscheduler.job.Job

0
source

I do not recommend calling job.func() as suggested in the accepted answer. The scheduler will not be aware that the work has started and will go wrong with the usual scheduling logic.

Instead, use the modify() function to set its next_run_time property to now() :

 for job in cron.get_jobs(): job.modify(next_run_time=datetime.now()) 

Also refer to the actual implementation of the Job class .

+4
source

As a workaround I made using the following. As a result, I execute all the tasks cron.get_jobs() and create a one-time task using the Job object to the "date" trigger, which runs only once, in datetime.now , as it is not specified.

 def refresh(): refreshed = [] for job in cron.get_jobs(): cron.add_job(job.func, 'date', id='{0}.uniq'.format(job.id), max_instances=1) refreshed.append(job.id) return json.dumps( {'number': len(cron.get_jobs()), 'list': refreshed} ) 
0
source

All Articles