question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Job.next_run_time not available form the outside

See original GitHub issue

I’m working with the scheduler in a Django App (should not affect behavior)

I create a Job to Backup at specific Intervals and want to log the next execution time. Now i log it with:

logger.info(f"next Backup at {scheduler.get_job(job_id).next_run_time.strftime('%Y-%m-%d %H:%M:%S')}") 

this raises an attribute Error:

logger.info(f"next Backup at {scheduler.get_job(job_id).next_run_time.strftime('%Y-%m-%d %H:%M:%S')}") 

AttributeError: next_run_time

but when just logging the job i get a right output:

Backup.backup (trigger: interval[0:10:00], next run at: 2020-05-15 17:49:26 CEST)

when looking in the code i see that the job uses self.next_run_time

so the attribute is surely available, but not accessible as it seems in addition to that is the AttributeError incomplete just saying the attribute, not that this attribute does not exist, which seems pretty wierd…

complete relevant code below code below:

 def startSubroutine(self)->None:
        """
        Start the backup-subroutine
        """
        self.start_time = datetime.datetime.now()
        self.scheduler = BackgroundScheduler()
        interval = self.settings['interval']
        self.job_id = self.scheduler.add_job(self.backup, 'interval', **interval).id

        # prepare info string to log
        info_str = format_interval("Scheduled backup for ", interval)

        logger.info(info_str)
        # the critical line:
        logger.info(f"Next Backup at {self.scheduler.get_job(self.job_id).next_run_time.strftime('%Y-%m-%d %H:%M:%S')}")

        # start subroutine
        self.scheduler.start()

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
agronholmcommented, Aug 17, 2022

The job’s scheduled run time is available on v4.0 via apscheduler.current_job:

from apscheduler import current_job

def my_task():
    print("This job was scheduled to run at", current_job.get().scheduled_fire_time)

Note that the API may change before v4.0 final.

0reactions
MatthewShottoncommented, Aug 17, 2022

I know this is an old issue but dropping my solution here in case it helps anyone who googles this. I faced a similar problem and have found a solution which seems to work quite nicely.

It looks like next_run_time is not available until the job is scheduled (you can check the pending property on the job instance to see if it’s scheduled or not). The speed at which the job get’s scheduled depends on the scheduler you use.

An alternative way to get the next_run_time is to call add_job with an explicit Trigger object. Then you can call get_next_fire_time() on the Trigger object at any time to get the time the Trigger will next cause the job to run.

A minimal example:

import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger

def job_func():
	print("Doing a job")

scheduler = BlockingScheduler()

# Trigger every even minute
trigger = CronTrigger(hour="*", minute="*/2")

# Add the job
scheduler.add_job(job_func, trigger)

# Get the next time the job will trigger, you can call this at anytime.
next_fire_time = trigger.get_next_fire_time(None, datetime.datetime.now())

print("Job next running @ ", next_fire_time)

# Start the scheduler so the job will get scheduled.
scheduler.start()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nuxt instance unavailable on all pages · Issue #4287 - GitHub
Are you using useRuntimeConfig at all? Could you locate the code context around the line mentioned in server.mjs in the log you quoted...
Read more >
Moving from @nuxtjs/dotenv to runtime config
It's time to migrate from @nuxtjs/dotenv module to use our new runtime config which has been released as of Nuxt v2. 13.
Read more >
Why is runtimeConfig doesn't see environment variables in ...
If it's the bridge version, runtimeConfig cannot work. Cause I use @nuxt/bridge, not work. and then it worked on the 3.0.0-rc.3 version.
Read more >
Pushing Nuxt.js Static Generation One Step Further - — lihbr
So let's throw target: "static" into our nuxt.config.js file and call it ... There is one case we have not discussed that is...
Read more >
Introduction to Nuxt 3 modules - DEV Community ‍ ‍
As there is no Nuxt 3 module template repository nor an article how to ... a nuxt.config.js file as well but it will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found