Example #1
0
def notify_event(event: EventBase, client: CatalogClient, operation_id=None):
    """
    Send an event or just log it if client is None
    Always log the event to info level
    """

    if client:
        try:
            client.add_event(event)
        except Exception as e:
            logger.warn("Error adding feed start event: {}".format(e))

    try:
        logger.info("Event: {} (operation_id={})".format(event.to_json(), operation_id))
    except:
        logger.exception("Error logging event")
Example #2
0
    def run_feeds_update(cls, json_obj=None, force_flush=False):
        """
        Creates a task and runs it, optionally with a thread if locking is enabled.

        :return:
        """
        error = None
        feeds = None

        with session_scope() as session:
            mgr = identities.manager_factory.for_session(session)
            system_user = mgr.get_system_credentials()

        catalog_client = CatalogClient(user=system_user[0], password=system_user[1])

        try:

            feeds = get_selected_feeds_to_sync(localconfig.get_config())
            if json_obj:
                task = cls.from_json(json_obj)
                if not task:
                    return None
                task.feeds = feeds
            else:
                task = FeedsUpdateTask(feeds_to_sync=feeds, flush=force_flush)

            # Create feed task begin event
            try:
                catalog_client.add_event(FeedSyncStart(groups=feeds if feeds else 'all'))
            except:
                log.exception('Ignoring event generation error before feed sync')

            result = []
            if cls.locking_enabled:
                # system_user = get_system_user_auth()
                run_target_with_lease(user_auth=system_user, lease_id='feed_sync', ttl=90, target=lambda: result.append(task.execute()))
                # A bit of work-around for the lambda def to get result from thread execution
                if result:
                    result = result[0]
            else:
                result = task.execute()

            return result
        except LeaseAcquisitionFailedError as ex:
            error = ex
            log.exception('Could not acquire lock on feed sync, likely another sync already in progress')
            raise Exception('Cannot execute feed sync, lock is held by another feed sync in progress')
        except Exception as e:
            error = e
            log.exception('Error executing feeds update')
            raise e
        finally:
            # log feed sync event
            try:
                if error:
                    catalog_client.add_event(FeedSyncFail(groups=feeds if feeds else 'all', error=error))
                else:
                    catalog_client.add_event(FeedSyncComplete(groups=feeds if feeds else 'all'))
            except:
                log.exception('Ignoring event generation error after feed sync')
Example #3
0
def emit_events(client: CatalogClient, analysis_events: list):
    for event in analysis_events:
        try:
            client.add_event(event)
        except:
            logger.error("Ignoring error sending event")