Lifetime of Reports
51
For such cases, it is possible to supply an optional lifetime for a report. That is, a time in seconds for
which the report should be cached by the publishing service. When such a value is supplied, if a sub-
scription arrives within that time, it will be sent a copy of that report. If a value of "-1" is supplied for
the lifetime, it will effectively cache the report indefinitely.
# publish and cache indefinitely
self.publishReport("system.status","idle",-1)
# publish and cache for 60 seconds
self.publishReport("system.action","twiddle thumb",60)
# publish but don’t cache
self.publishReport("system.thought","bored")
A cached report will only be discarded if a new report is published against the same subject, or the
lifetime specified expires. If a new report published against the same subject has no lifetime associated
with it, the cached report will be discarded, but the new report will not be cached. Note that with this
mechanism, only the last report published on a specific subject will ever be cached when a lifetime
value is provided.
To make the implementation as simple as possible, a report which has been cached against a subject
with a finite lifetime and which has expired, will only be discarded when a new report with the same
subject name is published, or a new subscription arrives which would have matched the subject. This
is done to avoid having to setup internal timers to trigger destruction of the report at the moment it ex-
pired.
A consequence of this approach however, is that a report may consume resources unnecessarily be-
yond the lifetime which it was supposed to exist. If this becomes an issue, it is possible for a service
agent to periodically purge any expired reports itself. This can be done by calling the member function
"purgeReports()".
class Publisher(netsvc.Service):
def __init__(self):
netsvc.Service.__init__(self,"publisher")
# purge expired reports every 15 minutes
self.scheduleAction(self.purgeReports,"*/15****")
In addition to being able to explicitly purge expired reports for performance reasons, a service agent
may also prematurely expire and purge reports which are older than a certain time. The member func-
tion for this is "expireReports()" and accepts a subject pattern and optional age in seconds. The
age defaults to "0" which would result in any cached report matching the subject pattern being imme-
diately expired and purged. If a non zero value for age is supplied, only reports which were older than
that age would be expired and purged. To apply this to all cached reports, regardless of subject, the
"expireAllReports()" member function can be used.