gavo.base.cron module

A cron-like facility to regularly run some functions.

Most of the apparatus in here is not really for user consumption. There’s a singleton of the queue created below, and the methods of that singleton are exposed as module-level functions.

To make the jobs actually execute, the running program has to call registerSchedulerFunction(schedulerFunction). Only the first registration is relevant. The schedulerFunction has the signature sf(delay, callable) and has to arrange for callable to be called delay seconds in the future; twisted’s reactor.callLater works like this.

However, you should arrange for previous callLaters to be canceled when a new one comes in. There is no management to make sure only one queue reaper runs at any time (it doesn’t hurt much if more than one run, but it’s a waste of resources).

class gavo.base.cron.AbstractJob(name, callable)[source]

Bases: object

A job run in a queue.

These have a name and a run() method; use their reportCronFailure(message) method to deliver error messages (of course, you can also just log; reportCronFailure will in typically send a mail). Concrete jobs have to implement a getNextWakeupTime(gmtime) -> gmtime method; they probably have to redefine __init__; the must up-call.

getNextWakeupTime(curTime)[source]

returns the UTC unix epoch seconds when this job is next supposed to run, starting from curTime.

lastStarted = None
reportCronFailure(message)[source]
run()[source]

runs callable under somewhat reliable circumstances.

class gavo.base.cron.IntervalJob(interval, name, callable)[source]

Bases: AbstractJob

A job that’s executed roughly every interval seconds.

interval can be negative, in which case the job is scheduled for (almost) immediate execution.

getNextWakeupTime(curTime)[source]

returns the UTC unix epoch seconds when this job is next supposed to run, starting from curTime.

class gavo.base.cron.Queue[source]

Bases: object

A cron-job queue.

This is really a heap sorted by the time the job is next supposed to run.

clearScheduleFunction()[source]
getQueueRepr()[source]

returns a sequence of (startDateTime local, job name) pairs.

This is for inspection/debug purposes.

registerScheduleFunction(scheduleFunction)[source]
repeatAt(times, name, callable)[source]

schedules callable to be run every day at times.

times is a list of (day-of-month, day-of-week, hour, minute) tuples. day-of-month and/or day-of-week are 1-based and may be None (if both are non-none, day-of-week wins).

name must be a unique identifier for the “job”. jobs with identical names overwrite each other.

callable will be run in the main thread, so it must finish quickly or it will block the server.

runEvery(seconds, name, callable)[source]

schedules callable to be run every seconds.

name must be a unique identifier for the “job”. jobs with identical names overwrite each other.

callable will be run in the main thread, so it must finish quickly or it will block the server.

class gavo.base.cron.TimedJob(times, name, callable)[source]

Bases: AbstractJob

A job that’s run roughly daily at some wallclock (UTC) times.

times is a list of (day-of-month, day-of-week, hour, minute) tuples. day-of-month and/or day-of-week are 1-based and may be None (if both are non-none, day-of-week wins).

getNextWakeupTime(curTime)[source]

returns the UTC unix epoch seconds when this job is next supposed to run, starting from curTime.

gavo.base.cron.clearScheduleFunction()
gavo.base.cron.isCensored(job)[source]

a hook to that can return true to suppress jobs.

This is used with serve debug.

gavo.base.cron.registerScheduleFunction(scheduleFunction)
gavo.base.cron.repeatAt(times, name, callable)

schedules callable to be run every day at times.

times is a list of (day-of-month, day-of-week, hour, minute) tuples. day-of-month and/or day-of-week are 1-based and may be None (if both are non-none, day-of-week wins).

name must be a unique identifier for the “job”. jobs with identical names overwrite each other.

callable will be run in the main thread, so it must finish quickly or it will block the server.

gavo.base.cron.runEvery(seconds, name, callable)

schedules callable to be run every seconds.

name must be a unique identifier for the “job”. jobs with identical names overwrite each other.

callable will be run in the main thread, so it must finish quickly or it will block the server.

gavo.base.cron.sendMailToAdmin(subject, message, debug=False)[source]

tries to send a mail to the configured administrator.

This relies on a functional mail infrastructure on the local host.