server/hook.py
changeset 9283 5f2c5eb1a820
parent 9257 ce338133c92c
parent 9267 24d9b86dfa54
child 9340 b1e933b0e850
equal deleted inserted replaced
9265:614762cdc357 9283:5f2c5eb1a820
    39 update of an entity / or a relation in the repository) and server events (such
    39 update of an entity / or a relation in the repository) and server events (such
    40 as server startup or shutdown).  In a typical application, most of the hooks are
    40 as server startup or shutdown).  In a typical application, most of the hooks are
    41 defined over data events.
    41 defined over data events.
    42 
    42 
    43 Also, some :class:`~cubicweb.server.hook.Operation` may be registered by hooks,
    43 Also, some :class:`~cubicweb.server.hook.Operation` may be registered by hooks,
    44 which will be fired when the transaction is commited or rollbacked.
    44 which will be fired when the transaction is commited or rolled back.
    45 
    45 
    46 The purpose of data event hooks is usually to complement the data model as
    46 The purpose of data event hooks is usually to complement the data model as
    47 defined in the schema, which is static by nature and only provide a restricted
    47 defined in the schema, which is static by nature and only provide a restricted
    48 builtin set of dynamic constraints, with dynamic or value driven behaviours.
    48 builtin set of dynamic constraints, with dynamic or value driven behaviours.
    49 For instance they can serve the following purposes:
    49 For instance they can serve the following purposes:
   703       for all operations which had their 'precommit' event already fired to let
   703       for all operations which had their 'precommit' event already fired to let
   704       them revert things (including the operation which made the commit fail)
   704       them revert things (including the operation which made the commit fail)
   705 
   705 
   706     * `rollback`:
   706     * `rollback`:
   707 
   707 
   708       the transaction has been either rollbacked either:
   708       the transaction has been either rolled back either:
   709 
   709 
   710        * intentionaly
   710        * intentionaly
   711        * a 'precommit' event failed, in which case all operations are rollbacked
   711        * a 'precommit' event failed, in which case all operations are rolled back
   712          once 'revertprecommit'' has been called
   712          once 'revertprecommit'' has been called
   713 
   713 
   714     * `postcommit`:
   714     * `postcommit`:
   715 
   715 
   716       the transaction is over. All the ORM entities accessed by the earlier
   716       the transaction is over. All the ORM entities accessed by the earlier
   768         should revert pre-commit's changes but take care, they may have not
   768         should revert pre-commit's changes but take care, they may have not
   769         been all considered if it's this operation which failed
   769         been all considered if it's this operation which failed
   770         """
   770         """
   771 
   771 
   772     def rollback_event(self):
   772     def rollback_event(self):
   773         """the observed connections set has been rollbacked
   773         """the observed connections set has been rolled back
   774 
   774 
   775         do nothing by default
   775         do nothing by default
   776         """
   776         """
   777 
   777 
   778     def postcommit_event(self):
   778     def postcommit_event(self):
  1032 class CleanupNewEidsCacheOp(DataOperationMixIn, SingleLastOperation):
  1032 class CleanupNewEidsCacheOp(DataOperationMixIn, SingleLastOperation):
  1033     """on rollback of a insert query we have to remove from repository's
  1033     """on rollback of a insert query we have to remove from repository's
  1034     type/source cache eids of entities added in that transaction.
  1034     type/source cache eids of entities added in that transaction.
  1035 
  1035 
  1036     NOTE: querier's rqlst/solutions cache may have been polluted too with
  1036     NOTE: querier's rqlst/solutions cache may have been polluted too with
  1037     queries such as Any X WHERE X eid 32 if 32 has been rollbacked however
  1037     queries such as Any X WHERE X eid 32 if 32 has been rolled back however
  1038     generated queries are unpredictable and analysing all the cache probably
  1038     generated queries are unpredictable and analysing all the cache probably
  1039     too expensive. Notice that there is no pb when using args to specify eids
  1039     too expensive. Notice that there is no pb when using args to specify eids
  1040     instead of giving them into the rql string.
  1040     instead of giving them into the rql string.
  1041     """
  1041     """
  1042     data_key = 'neweids'
  1042     data_key = 'neweids'
  1043 
  1043 
  1044     def rollback_event(self):
  1044     def rollback_event(self):
  1045         """the observed connections set has been rollbacked,
  1045         """the observed connections set has been rolled back,
  1046         remove inserted eid from repository type/source cache
  1046         remove inserted eid from repository type/source cache
  1047         """
  1047         """
  1048         try:
  1048         try:
  1049             self.session.repo.clear_caches(self.get_data())
  1049             self.session.repo.clear_caches(self.get_data())
  1050         except KeyError:
  1050         except KeyError:
  1054     """on commit of delete query, we have to remove from repository's
  1054     """on commit of delete query, we have to remove from repository's
  1055     type/source cache eids of entities deleted in that transaction.
  1055     type/source cache eids of entities deleted in that transaction.
  1056     """
  1056     """
  1057     data_key = 'pendingeids'
  1057     data_key = 'pendingeids'
  1058     def postcommit_event(self):
  1058     def postcommit_event(self):
  1059         """the observed connections set has been rollbacked,
  1059         """the observed connections set has been rolled back,
  1060         remove inserted eid from repository type/source cache
  1060         remove inserted eid from repository type/source cache
  1061         """
  1061         """
  1062         try:
  1062         try:
  1063             eids = self.get_data()
  1063             eids = self.get_data()
  1064             self.session.repo.clear_caches(eids)
  1064             self.session.repo.clear_caches(eids)