You are not logged in Log in Join
You are here: Home » Members » lstaffor » Event Scheduler for Zope » ZScheduler » View Document

Log in
Name

Password

 

ZScheduler

ZScheduler (Zope Scheduled Methods Product)

Provides a way to execute methods at specified times (similar to Unix cron). Use for: email reminders; periodic garbage collection; ageing of objects; updating syndicated content from other sites; running processes in the background (e.g. sending multiple emails, paging technical support) (just schedule with a time of now); load leveling (deferring tasks to a time of expected lighter load); an intelligent queue based email system that can retry later if your SMTP host is down; packing your ZODB regularly; rebuilding or reindexing Catalogs; removing old Sessions (from SQLSession or FSSession); building more general event products (with conditional events, dependencies, etc.).

Classes

You use ZScheduler by creating an object of the class "ZEvent". You can add a ZEvent to any folder. A ZEvent is a DTML method that has a time associated with it.

The time associated with an event is an object of the Zope DateTime class. See ../DateTime/DateTime.py for allowable input formats (explained in English within a Python comment). A disabled event has the null time which is defined as the special time value DateTime(0). For the purpose of entering a null time in the management forms, an empty time field is converted to DateTime(0).

Any output produced by a ZEvent is written to the log. A ZEvent does not have to produce output, but if it does produce output, it may need to set the response headers. For example:

Content-Type: text/plain

Bang!

Note the empty line after the response header.

The management screen for ZEvents contains a tab labeled "Trigger" which helps with debugging by test-firing the event, without regard to the scheduled firing time.

You can provide added value to events by subclassing the ZEvent class. In particular, "reschedule", which is called only when the event is triggered, can be overridden to provide repetitive events. Its interface is compatible with DTMLMethods:

  • input: REQUEST.ZEventOldTime a DateTime object
  • output: REQUEST.ZEventTime a DateTime object

For example, define a ZClass (let's call it "Nag" just for example) that derives from ZEvent. Give it a DTML method called "reschedule". The name must be "reschedule" so that it overrides the "reschedule" method of ZEvent. The new "reschedule" method consists of the following DTML statement:

<dtml-call "REQUEST.set(ZEventTime, REQUEST.ZEventOldTime + 1.0/24)">

...which reschedules the event to execute one hour after it was last scheduled to execute. Then create one or more instances of Nag, each with DTML code to do what you want. Each Nag (instance of the Nag class) will do its thing hour after hour after hour.... Note the use of floating point arithmetic in DateTime calculations. Writing 1.0/24 instead of 1/24 forces the type of the result to be floating point.

Permissions

The following permissions are defined by this product:

  • AddZEvents

People who have this permission have the power to execute Zope methods on your webserver repeatedly and rapidly; so, don't give this permission to just anyone. Think of scheduled events as time bombs. Whom do you trust to play with time bombs near your server?

ZEvents by default have the role "Anonymous". If you want a ZEvent to perform some management function in Zope that requires other roles, you must assign proxy roles to the ZEvent or to the methods it invokes.

Installation

Standard Zope product. Store release package in the Zope root directory. Gunzip and untar, which places files in lib/python/Products/ZScheduler subdirectory. (You may see a spurious error message to the effect that "decompression OK, trailing garbage ignored".) Set the environment variable STUPID_LOG_FILE to the name of a file for collecting log entries about each event; for example, on MS Windows:

  • set STUPID_LOG_FILE=C:\Zope\var\zopeapp.log

If you use a Zope hosting service that let's you manage products thru FTP but does not give you the telnet access needed to set environment variables, investigate the FrozenLogger product.

For Zope version 2.2.0b1 and earlier, you need to install this patch to Client.py (in two places) if your Zope listens for HTTP on a port other than 80:

#headers['Host']=split(urlparse(url)[1],:)[0]

headers['Host']=urlparse(url)[1]

Restart Zope.

Infrastructure

Creates an object "Schedule" in the root folder. Schedule is a ZCatalog which indexes ZEvents. The catalog is created (if it doesn't already exist) when Zope loads the product. You cannot directly modify Schedule or delete it as long as this product is installed.

Creates a thread "Dispatcher" which converts the passage of time into an ordered series of calls to the events in the Schedule.

Contact

For more information or to get the latest version, visit the ZScheduler's home base http://www.zope.org/Members/lstaffor/

Please use the ZScheduler ZWiki, for discussion.