So, I have a job which runs every day at a fixed time but the first time, I would like it to be run 5 mins after application startup (this is to prevent some uWSGI issue with replicas that I'm seeing). This is what I have so far:
scheduler = BackgroundScheduler(timezone=app.container.config.get("app.timezone"))
job_id = random.randint(0, 999)
job_name = f"mys_fetch_background_job_{job_id}"
# schedule data fetch and run now
scheduler.add_job(
my_service.fetch_data,
CronTrigger.from_crontab(
app.container.config.get("app.my_service.data_reload_cron_utc"),
# this value is 0 13 * * *
"UTC",
),
next_run_time=datetime.time(), # this executes on application startup immediately
id=str(job_id),
name=job_name
)
scheduler.start()
I'm trying to figure out how to schedule the first run just 5 mins after application startup - i.e. how can I do : next_run_time = datetime.time() 5 mins
CodePudding user response:
First off, if you want the current time from datetime you need to use datetime.datetime.now()
, as datetime.time()
will represent a static time (midnight). To get the time 5 minutes from now, you can use a datetime.timedelta
object like this datetime.datetime.now() datetime.timedelta(minutes=5)
. This would work with any datetime.datetime
object (but not a datetime.time
object like you used.