2

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?

keramat
  • 4,328
  • 6
  • 25
  • 38
Basya
  • 1,477
  • 1
  • 12
  • 22

3 Answers3

5

Putting this here for anyone who is trying to find different attrs of their Job object. Since the object doesn't utilize __dict__ use __slots__ instead. This will show you all the different attributes of the Job Object just as __dict__ would.

>>> scheduler.get_jobs()[0].__slots__
('_scheduler', '_jobstore_alias', 'id', 'trigger', 'executor', 'func', 'func_ref', 'args', 'kwargs', 'name', 'misfire_grace_time', 'coalesce', 'max_instances', 'next_run_time', '__weakref__')

Then with this information you can then retrieve the attrs

>>> scheduler.get_jobs()[0].trigger
<IntervalTrigger (interval=datetime.timedelta(seconds=1200), start_date='2021-08-06 10:01:19 CDT', timezone='America/Chicago')>
>>> scheduler.get_jobs()[0].max_instances
1
cdraper
  • 147
  • 2
  • 7
  • Does the `__slots__` exist also for scheduler? How could I see the attributes of scheduler? Thanks. – vpap Mar 18 '22 at 14:59
  • I would like to get some sort of scheduler pid so I can stop him as needed. – vpap Mar 18 '22 at 15:10
  • @vpap Ideally you would do so from python itself, I can't think of a reason that you would need the pid of the scheduler. If anything, you should use the shutdown class method on the scheduler object in order to stop it. i.e. `scheduler.shutdown()` – cdraper Mar 18 '22 at 16:56
  • I am starting the scheduler from a web UI and unfortunately after any scheduler initialization and calling `start()` basically there's no direct way to track it. For now, I am solving this problem by using its id w/ `id(scheduler)`. – vpap Mar 18 '22 at 18:07
  • Thanks. This looks interesting; I'm not working with APScheduler for years already though, so I can not confirm it. – Basya Jan 18 '23 at 16:32
3

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': '*'}]
Basya
  • 1,477
  • 1
  • 12
  • 22
3

APScheduler .get_jobs() method returns a list of job instances.

For example, you can print information about all currently scheduled jobs with something like the following:

for job in scheduler.get_jobs():
    print("name: %s, trigger: %s, next run: %s, handler: %s" % (
          job.name, job.trigger, job.next_run_time, job.func))

Note that job names can repeat across different jobs.

You can find here the API reference for the job class. It doesn't explicitly list the job class members, but they match the variables in kwargs.

jjmontes
  • 24,679
  • 4
  • 39
  • 51