def collate_content(request): """Invoke the collation process - trigger post-publication""" ident_hash = request.matchdict['ident_hash'] try: binder = export_epub.factory(ident_hash) except export_epub.NotFound: raise httpexceptions.HTTPNotFound() if not isinstance(binder, cnxepub.Binder): raise httpexceptions.HTTPBadRequest( '{} is not a book'.format(ident_hash)) settings = request.registry.settings with psycopg2.connect(settings[config.CONNECTION_STRING]) as db_conn: with db_conn.cursor() as cursor: remove_collation(binder.ident_hash, cursor=cursor) id, version = split_ident_hash(ident_hash) if version: cursor.execute("""\ UPDATE modules SET stateid = 5 WHERE ident_hash(uuid, major_version, minor_version) = %s """, (ident_hash,)) else: cursor.execute("""\ UPDATE modules SET stateid = 5 where module_ident in (select module_ident from latest_modules WHERE uuid = %s """, (id,))
def process(cursor, module_ident, ident_hash, includes): logger.debug('Processing module_ident={} ident_hash={}'.format( module_ident, ident_hash)) set_post_publications_state(cursor, module_ident, 'Processing') try: binder = export_epub.factory(ident_hash) except export_epub.NotFound: logger.error('ident_hash={} module_ident={} not found'.format( ident_hash, module_ident)) update_module_state(cursor, module_ident, 'errored') set_post_publications_state( cursor, module_ident, 'Failed/Error', 'ident_hash={} not found'.format(ident_hash)) return cursor.execute( """\ SELECT submitter, submitlog FROM modules WHERE ident_hash(uuid, major_version, minor_version) = %s""", (ident_hash, )) publisher, message = cursor.fetchone() remove_collation(ident_hash, cursor=cursor) collate(binder, publisher, message, cursor=cursor, includes=includes) logger.debug('Finished processing module_ident={} ident_hash={}'.format( module_ident, ident_hash)) update_module_state(cursor, module_ident, 'current') set_post_publications_state(cursor, module_ident, 'Done/Success')
def process(cursor, module_ident, ident_hash, includes): logger.debug('Processing module_ident={} ident_hash={}'.format( module_ident, ident_hash)) set_post_publications_state(cursor, module_ident, 'Processing') try: binder = export_epub.factory(ident_hash) except export_epub.NotFound: logger.error('ident_hash={} module_ident={} not found' .format(ident_hash, module_ident)) update_module_state(cursor, module_ident, 'errored') set_post_publications_state( cursor, module_ident, 'Failed/Error', 'ident_hash={} not found'.format(ident_hash)) return cursor.execute("""\ SELECT submitter, submitlog FROM modules WHERE uuid || '@' || concat_ws('.', major_version, minor_version) = %s""", (ident_hash,)) publisher, message = cursor.fetchone() remove_collation(ident_hash, cursor=cursor) collate(binder, publisher, message, cursor=cursor, includes=includes) logger.debug('Finished processing module_ident={} ident_hash={}'.format( module_ident, ident_hash)) update_module_state(cursor, module_ident, 'current') set_post_publications_state(cursor, module_ident, 'Done/Success')
def collate_content(request): """Invoke the collation process - trigger post-publication""" ident_hash = request.matchdict['ident_hash'] try: binder = export_epub.factory(ident_hash) except export_epub.NotFound: raise httpexceptions.HTTPNotFound() if not isinstance(binder, cnxepub.Binder): raise httpexceptions.HTTPBadRequest( '{} is not a book'.format(ident_hash)) settings = request.registry.settings with psycopg2.connect(settings[config.CONNECTION_STRING]) as db_conn: with db_conn.cursor() as cursor: remove_collation(binder.ident_hash, cursor=cursor) id, version = split_ident_hash(ident_hash) if version: cursor.execute("""\ UPDATE modules SET stateid = 5 WHERE uuid = %s AND concat_ws('.', major_version, minor_version) = %s """, (id, version,)) else: cursor.execute("""\ UPDATE modules SET stateid = 5 where module_ident in (select module_ident from latest_modules WHERE uuid = %s """, (id,))
def baking_processor(module_ident, ident_hash, cursor=None): try: binder = export_epub.factory(ident_hash) except: logger.exception('Logging an uncaught exception during baking' 'ident_hash={} module_ident={}'.format( ident_hash, module_ident)) # FIXME If the top module doesn't exist, this is going to fail. update_module_state(cursor, module_ident, 'errored', None) raise finally: logger.debug('Finished exporting module_ident={} ident_hash={}'.format( module_ident, ident_hash)) cursor.execute( """\ SELECT submitter, submitlog FROM modules WHERE ident_hash(uuid, major_version, minor_version) = %s""", (ident_hash, )) publisher, message = cursor.fetchone() remove_baked(ident_hash, cursor=cursor) recipe_ids = _get_recipe_ids(module_ident, cursor) state = 'current' for recipe_id in recipe_ids: try: bake(binder, recipe_id, publisher, message, cursor=cursor) except Exception as exc: if state == 'current' and recipe_ids[1] is not None: state = 'fallback' continue else: state = 'errored' # TODO rollback to pre-removal of the baked content?? cursor.connection.rollback() logger.exception('Uncaught exception during baking') update_module_state(cursor, module_ident, state, recipe_id) raise finally: logger.debug('Finished module_ident={} ident_hash={} ' 'with a final state of \'{}\'.'.format( module_ident, ident_hash, state)) update_module_state(cursor, module_ident, state, recipe_id) break
def baking_processor(self, module_ident, ident_hash, cursor=None): if self.request.retries == 0: cursor.execute("""\ SELECT module_ident, ident_hash(uuid, major_version, minor_version) FROM modules NATURAL JOIN modulestates WHERE uuid = %s AND statename IN ('post-publication', 'processing') ORDER BY major_version DESC, minor_version DESC""", (utils.split_ident_hash(ident_hash)[0],)) latest_module_ident = cursor.fetchone() if latest_module_ident: if latest_module_ident[0] != module_ident: logger.debug("""\ More recent version (module_ident={} ident_hash={}) in queue. \ Move this message (module_ident={} ident_hash={}) \ to the deferred (low priority) queue""" .format(latest_module_ident[0], latest_module_ident[1], module_ident, ident_hash)) raise self.retry(queue='deferred') else: # In case we can't find the latest version being baked, we'll # continue with baking this one pass logger.debug('Starting baking module_ident={} ident_hash={}' .format(module_ident, ident_hash)) recipe_ids = _get_recipe_ids(module_ident, cursor) state = 'current' if recipe_ids[0] is None: remove_baked(ident_hash, cursor=cursor) logger.debug('Finished unbaking module_ident={} ident_hash={} ' 'with a final state of \'{}\'.' .format(module_ident, ident_hash, state)) update_module_state(cursor, module_ident, state, None) return try: binder = export_epub.factory(ident_hash) except: # noqa: E722 logger.exception('Logging an uncaught exception during baking' 'ident_hash={} module_ident={}' .format(ident_hash, module_ident)) # FIXME If the top module doesn't exist, this is going to fail. update_module_state(cursor, module_ident, 'errored', None) raise finally: logger.debug('Exported module_ident={} ident_hash={}' .format(module_ident, ident_hash)) cursor.execute("""\ SELECT submitter, submitlog FROM modules WHERE ident_hash(uuid, major_version, minor_version) = %s""", (ident_hash,)) publisher, message = cursor.fetchone() remove_baked(ident_hash, cursor=cursor) for recipe_id in recipe_ids: try: bake(binder, recipe_id, publisher, message, cursor=cursor) except Exception: if state == 'current' and recipe_ids[1] is not None: state = 'fallback' continue else: state = 'errored' # TODO rollback to pre-removal of the baked content?? cursor.connection.rollback() logger.exception('Uncaught exception during baking') update_module_state(cursor, module_ident, state, recipe_id) raise else: logger.debug('Finished baking module_ident={} ident_hash={} ' 'with a final state of \'{}\'.' .format(module_ident, ident_hash, state)) update_module_state(cursor, module_ident, state, recipe_id) break