Skip to content

Commit

Permalink
Avoid ugly failures on featured image generation errors
Browse files Browse the repository at this point in the history
Give better feedback of the problem and store a copy of the whole page to help on debugging.
  • Loading branch information
hvelarde committed Nov 14, 2018
1 parent 3e1fab9 commit 239766b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
7 changes: 7 additions & 0 deletions src/collective/behavior/featuredimage/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
from collective.behavior.featuredimage.config import PROJECTNAME

import logging


logger = logging.getLogger(PROJECTNAME)
50 changes: 35 additions & 15 deletions src/collective/behavior/featuredimage/subscriber.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,61 @@
# coding: utf-8
from collective.behavior.featuredimage import _
from collective.behavior.featuredimage.logger import logger
from cStringIO import StringIO
from PIL import Image
from plone import api
from plone.namedfile.file import NamedBlobImage
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

import signal
import transaction


def _get_screenshot(page):
"""Get screenshot of Fetured Image"""
def _get_screenshot(page, request):
"""Get a screenshot of the fetured image. Do not fail in case of
errors, but store an image of the whole page.
"""
browser = webdriver.PhantomJS()
browser.implicitly_wait(1) # seconds

# Use minimun image size while don't break image proportion
browser.set_window_size(1300, 1300)
browser.get(page)
data = browser.get_screenshot_as_png()
el = browser.find_element_by_id('featuredimage')
location, size = el.location, el.size

# XXX: quit() does not terminate PhantomJS process
# https://github.com/SeleniumHQ/selenium/issues/767
browser.service.process.send_signal(signal.SIGTERM)
browser.quit()
location = size = None
try:
el = browser.find_element_by_id('featuredimage')
location, size = el.location, el.size
raise NoSuchElementException
except NoSuchElementException:
logger.error('An error ocurred while generating the featured image')
msg = _(
'featured_image_generation_error',
default=u'An error ocurred while generating the featured image. '
u'Please revert the workflow state. '
u'The image generated can be used for further analysis.')
api.portal.show_message(msg, request=request, type='error')
finally:
# XXX: quit() does not terminate PhantomJS process
# https://github.com/SeleniumHQ/selenium/issues/767
browser.service.process.send_signal(signal.SIGTERM)
browser.quit()

# crop image
im = Image.open(StringIO(data))
im = im.crop((
location['x'], location['y'],
location['x'] + size['width'],
location['y'] + size['height']
))
if location and size:
# crop image
im = im.crop((
location['x'], location['y'],
location['x'] + size['width'],
location['y'] + size['height']
))
output = StringIO()
im.save(output, 'PNG', optimize=True)
data = output.getvalue()
output.close()

return data


Expand All @@ -50,7 +70,7 @@ def update_featuredimage(context, event):
# without this phantomjs look the old page
transaction.commit()
# get screenshot
data = _get_screenshot(page)
data = _get_screenshot(page, context.REQUEST)
# save image
image = NamedBlobImage()
image._setData(data)
Expand Down

0 comments on commit 239766b

Please sign in to comment.