Commit 10508b91 authored by Denis Laxalde's avatar Denis Laxalde
Browse files

[hooks,sobjects] Do not fail in hooks because of CKAN synchronization errors

Just log an error instead (except in "test" mode as this will be otherwise
hardly debuggable). Add a specific exception that could be catched when using
services directly.

Closes #4869939.
parent dd318b930f1d
...@@ -21,7 +21,8 @@ from cubicweb.predicates import adaptable, score_entity, PartialPredicateMixIn ...@@ -21,7 +21,8 @@ from cubicweb.predicates import adaptable, score_entity, PartialPredicateMixIn
from cubicweb.server import hook from cubicweb.server import hook
from cubes.ckanpublish.sobjects import (delete_dataset, from cubes.ckanpublish.sobjects import (delete_dataset,
delete_dataset_resource) delete_dataset_resource,
CKANSyncError)
from cubes.ckanpublish.utils import ckan_instance_configured from cubes.ckanpublish.utils import ckan_instance_configured
...@@ -56,12 +57,20 @@ class CKANDatasetOp(hook.DataOperationMixIn, hook.Operation): ...@@ -56,12 +57,20 @@ class CKANDatasetOp(hook.DataOperationMixIn, hook.Operation):
def precommit_event(self): def precommit_event(self):
for eid in self.get_data(): for eid in self.get_data():
if self.cnx.deleted_in_transaction(eid): event = ('deletion' if self.cnx.deleted_in_transaction(eid)
datasetid = self.cnx.entity_from_eid(eid).ckan_dataset_id else 'synchronization')
delete_dataset(self.cnx.vreg.config, eid, datasetid) try:
self.info('deleted CKAN dataset %s', datasetid) if event == 'deletion':
else: datasetid = self.cnx.entity_from_eid(eid).ckan_dataset_id
self.cnx.call_service('ckanpublish.sync_dataset', eid=eid) delete_dataset(self.cnx.vreg.config, eid, datasetid)
self.info('deleted CKAN dataset %s', datasetid)
else:
self.cnx.call_service('ckanpublish.sync_dataset', eid=eid)
except CKANSyncError as exc:
self.error('%s of CKAN dataset linked to entity #%d failed: %s',
event, eid, str(exc))
if self.cnx.vreg.config.mode == 'test':
raise
class DeleteCKANResourceHook(hook.Hook): class DeleteCKANResourceHook(hook.Hook):
...@@ -132,8 +141,17 @@ class CKANResourceOp(hook.DataOperationMixIn, hook.Operation): ...@@ -132,8 +141,17 @@ class CKANResourceOp(hook.DataOperationMixIn, hook.Operation):
def precommit_event(self): def precommit_event(self):
for eid in self.get_data(): for eid in self.get_data():
resourceid = self.cnx.entity_from_eid(eid).ckan_resource_id resourceid = self.cnx.entity_from_eid(eid).ckan_resource_id
if self.cnx.deleted_in_transaction(eid) and resourceid is not None: event = ('deletion' if (self.cnx.deleted_in_transaction(eid)
delete_dataset_resource(self.cnx.vreg.config, eid, resourceid) and resourceid is not None)
self.info('deleted resource %s', resourceid) else 'synchronization')
else: try:
self.cnx.call_service('ckanpublish.sync_resource', eid=eid) if event == 'deletion':
delete_dataset_resource(self.cnx.vreg.config, eid, resourceid)
self.info('deleted resource %s', resourceid)
else:
self.cnx.call_service('ckanpublish.sync_resource', eid=eid)
except CKANSyncError as exc:
self.error('%s of CKAN resource linked to entity #%d failed: %s',
event, eid, str(exc))
if self.cnx.vreg.config.mode == 'test':
raise
...@@ -18,20 +18,22 @@ ...@@ -18,20 +18,22 @@
from requests.exceptions import RequestException from requests.exceptions import RequestException
from cubicweb import ValidationError
from cubicweb.server import Service from cubicweb.server import Service
from cubicweb.predicates import adaptable from cubicweb.predicates import adaptable
from cubes.ckanpublish.utils import (ckan_instance_configured, ckan_post, from cubes.ckanpublish.utils import (ckan_instance_configured, ckan_post,
CKANPostError) CKANPostError)
class CKANSyncError(Exception):
"""Error during synchronization to CKAN instance"""
def _ckan_action(config, eid, action, **kwargs): def _ckan_action(config, eid, action, **kwargs):
"""Run `ckan_post` and eventually raise ValidationError.""" """Run `ckan_post` and eventually raise CKANSyncError."""
try: try:
return ckan_post(config, action, **kwargs) return ckan_post(config, action, **kwargs)
except (CKANPostError, RequestException) as exc: except (CKANPostError, RequestException) as exc:
raise ValidationError(eid, {'ckan_dataset_id': unicode(exc)}) raise CKANSyncError(str(exc))
def create_dataset(config, eid, data): def create_dataset(config, eid, data):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment