def time_before_next_run(self) -> Optional[str]: job = scheduler.get_job(self.aps_job_id) if job and job.next_run_time: delta = job.next_run_time.replace(tzinfo=None) - datetime.now() hours, remainder = divmod(delta.seconds, 3600) minutes, seconds = divmod(remainder, 60) days = f"{delta.days} days, " if delta.days else "" return f"{days}{hours}h:{minutes}m:{seconds}s" return None
def schedule(self) -> None: default, trigger = self.kwargs() if not scheduler.get_job(self.aps_job_id): scheduler.add_job(**{**default, **trigger}) else: scheduler.reschedule_job(default.pop("id"), **trigger)
def next_run_time(self) -> Optional[str]: job = scheduler.get_job(self.aps_job_id) if job and job.next_run_time: return job.next_run_time.strftime("%Y-%m-%d %H:%M:%S") return None
def delete_task(self) -> None: if scheduler.get_job(self.aps_job_id): scheduler.remove_job(self.aps_job_id) db.session.commit()