Search code examples
pythonapscheduler

How to interpret list of jobs returned from get_jobs in APScheduler?


I am trying to figure out whether APScheduler is the tool for my project's needs.

I can add a job.

I can get a list of jobs -- but I am having trouble interpreting the results. I want to have my program translate this returned job list into a format needed by other parts of the program. I need to extract the trigger parameters.

Let's say I'm using a cron trigger. I'll make a simple one for now:

new_job = self.scheduler.add_job(jobfunc,'cron', minute='*/5', args=["minute"]

Later, I want to get the jobs and then extract the basic information (this is a cron trigger, that it is scheduled for 'minute'=*/5) -- basically I want to reconstruct the parameters with which I created the trigger.

I have looked at the jobs returned in the debugger, and I see that each job has a "trigger" member which has within it the information I need -- but I am not sure how to access it programmatically.

I would like to do something like this:

    jobs=self.scheduler.get_jobs()
    schedules=[]
    for job in jobs:
        schedules.append(<do something with job.trigger.fields>)

Where the element appended to schedules would look something like:

{'minute':'*/5'}

The documentation says:

To get a machine processable list of the scheduled jobs, you can use the get_jobs().

The question is, how should my 'machine' process this list?


Solution

  • OK, thanks to a previously asked question on the APScheduler Google group, I have a solution!

    The details have to be accessed through the fields, as I thought. The key to get the information from the individual field is to convert the field value to a string.

    Here is an example which works:

     schedules = []
     for job in self.scheduler.get_jobs():
         jobdict = {}
         for f in job.trigger.fields:
             curval = str(f)
             jobdict[f.name] = curval
    
         schedules.append(jobdict)
    

    For a schedule with one job added as:

    new_job = self.scheduler.add_job(jobfunc,'cron', second='*/5', args=["second"])
    

    The resulting list comes out like this:

    [{'week': '*', 'hour': '*', 'day_of_week': '*', 'month': '*', 'second': '*/5', 'year': '*', 'day': '*', 'minute': '*'}]