# HG changeset patch # User Aurelien Campeas # Date 1270748196 -7200 # Node ID 6d182c7d4392467138608f747b25b4e2cb3f18a7 # Parent 73bdc50d6af1ffb27631b30af8efbf076183eb1a [doc/book] begin chapter on Hooks/Operations diff -r 73bdc50d6af1 -r 6d182c7d4392 doc/book/en/development/devrepo/hooks.rst --- a/doc/book/en/development/devrepo/hooks.rst Thu Apr 08 18:05:41 2010 +0200 +++ b/doc/book/en/development/devrepo/hooks.rst Thu Apr 08 19:36:36 2010 +0200 @@ -2,31 +2,103 @@ .. _hooks: -Hooks -===== +Hooks and Operations +==================== + +Principles +---------- -XXX FILLME +Paraphrasing the `emacs`_ documentation, let us say that hooks are an +important mechanism for customizing an application. A hook is +basically a list of functions to be called on some well-defined +occasion (This is called `running the hook`). + +.. _`emacs`: http://www.gnu.org/software/emacs/manual/html_node/emacs/Hooks.html + +In CubicWeb, hooks are classes subclassing the Hook class in +`server/hook.py`, implementing their own `call` method, and defined +over pre-defined `events`. + +There are two families of events: data events and server events. In a +typical application, most of the Hooks are defined over data +events. There can be a lot of them. -*Hooks* are executed before or after updating an entity or a relation in the -repository. +The purpose of data hooks is to complement the data model as defined +in the schema.py, which is static by nature, with dynamic or value +driven behaviours. It is functionally equivalent to a `database +trigger`_, except that database triggers definitions languages are not +standardized, hence not portable (for instance, PL/SQL works with +Oracle and PostgreSQL but not SqlServer nor Sqlite). + +.. _`database trigger`: http://en.wikipedia.org/wiki/Database_trigger + +Data hooks can serve the following purposes: + +* enforcing constraints that the static schema cannot express + (spanning several entities/relations, exotic cardinalities, etc.) -Their prototypes are as follows: +* implement computed attributes (an example could be the maintenance + of a relation representing the transitive closure of another relation) + +Operations are Hook-like objects that are created by Hooks and +scheduled to happen just before (or after) the `commit` event. Hooks +being fired immediately on data operations, it is sometime necessary +to delay the actual work down to a time where all other Hooks have run +and the application state converges towards consistency. Also while +the order of execution of Hooks is data dependant (and thus hard to +predict), it is possible to force an order on Operations. + +Events +------ - * after_add_entity (session, entity) - * after_update_entity (session, entity) - * after_delete_entity (session, eid) - * before_add_entity (session, entity) - * before_update_entity (session, entity) - * before_delete_entity (session, eid) +Hooks are mostly defined and used to handle `dataflow`_ operations. It +means as data gets in (mostly), specific events are issued and the +Hooks matching these events are called. + +.. _`dataflow`: http://en.wikipedia.org/wiki/Dataflow + +Below comes a list of the dataflow events related to entities operations: + +* before_add_entity + +* before_update_entity + +* before_delete_entity + +* after_add_entity + +* after_update_entity + +* after_delete_entity + +These define ENTTIES HOOKS. RELATIONS HOOKS are defined +over the following events: + +* after_add_relation - * after_add_relation (session, fromeid, rtype, toeid) - * after_delete_relation (session, fromeid, rtype, toeid) - * before_add_relation (session, fromeid, rtype, toeid) - * before_delete_relation (session, fromeid, rtype, toeid) +* after_delete_relation + +* before_add_relation + +* before_delete_relation + +This is an occasion to remind us that relations support the add/delete +operation, but no delete. + +Non data events also exist. These are called SYSTEM HOOKS. + +* server_startup - * server_startup - * server_shutdown +* server_shutdown + +* server_maintenance + +* server_backup - * session_open - * session_close +* server_restore + +* session_open +* session_close + + diff -r 73bdc50d6af1 -r 6d182c7d4392 server/hook.py --- a/server/hook.py Thu Apr 08 18:05:41 2010 +0200 +++ b/server/hook.py Thu Apr 08 19:36:36 2010 +0200 @@ -30,7 +30,6 @@ Session hooks (eg session_open, session_close) have no special attribute. - :organization: Logilab :copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr