From 7f750ae692834572e5b06f8482ff85c1f627d873 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 12 Jan 2015 16:45:24 +0000 Subject: [PATCH] add an error callback to the pdf renderer use page load and render timeouts of 30 seconds to detect hanging pages --- .../ide/pdfng/directives/pdfRenderer.coffee | 70 ++++++++++++++++--- .../ide/pdfng/directives/pdfViewer.coffee | 2 + 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee index 15a45c8908..c04af476ea 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee @@ -19,9 +19,11 @@ define [ @navigateFn = @options.navigateFn @spinner = new pdfSpinner @resetState() - + @errorCallback = @options.errorCallback + @pdfjs.catch (exception) => + # console.log 'ERROR in get document', exception + @errorCallback(exception) resetState: () -> - @page = [] @complete = [] @timeout = [] @pageLoad = [] @@ -34,9 +36,8 @@ define [ pdfDocument.numPages getPage: (pageNum) -> - # with promise caching - return @page[pageNum] if @page[pageNum]? - @page[pageNum] = @document.then (pdfDocument) -> + @document.then (pdfDocument) -> + # console.log 'got pdf document, now getting Page', pageNum pdfDocument.getPage(pageNum) getPdfViewport: (pageNum, scale) -> @@ -44,6 +45,8 @@ define [ @document.then (pdfDocument) -> pdfDocument.getPage(pageNum).then (page) -> viewport = page.getViewport scale + , (error) -> + console.log 'ERROR', error getDestinations: () -> @document.then (pdfDocument) -> @@ -57,15 +60,20 @@ define [ pdfDocument.getDestinations() return @destinations.then (all) -> all[dest] + , (error) -> + console.log 'ERROR', error - @document.then (pdfDocument) -> - pdfDocument.getDestination(dest) - + # @document.then (pdfDocument) -> + # pdfDocument.getDestination(dest) + # , (error) -> + # console.log 'ERROR', error getPageIndex: (ref) -> @document.then (pdfDocument) -> pdfDocument.getPageIndex(ref).then (idx) -> idx + , (error) -> + console.log 'ERROR', error getScale: () -> @scale @@ -101,6 +109,7 @@ define [ @triggerRenderQueue() processRenderQueue: () -> + return if @shuttingDown return if @jobs > 0 current = @renderQueue.shift() return unless current? @@ -118,17 +127,37 @@ define [ completeRef = @complete renderTaskRef = @renderTask + # console.log 'started page load', pagenum + + timedOut = false + timer = $timeout () => + # console.log 'page load timed out', pagenum + timedOut = true + @spinner.stop(element.canvas) + # @jobs = @jobs - 1 + # @triggerRenderQueue(0) + this.errorCallback?('timeout') + , 30*1000 @pageLoad[pagenum] = @getPage(pagenum) + @pageLoad[pagenum].then (pageObject) => + # console.log 'in page load success', pagenum + $timeout.cancel(timer) @renderTask[pagenum] = @doRender element, pagenum, pageObject @renderTask[pagenum].then () => # complete + # console.log 'render task success', pagenum completeRef[pagenum] = true @removeCompletedJob renderTaskRef, pagenum , () => + # console.log 'render task failed', pagenum # rejected @removeCompletedJob renderTaskRef, pagenum + .catch (error) -> + # console.log 'in page load error', pagenum, 'timedOut=', timedOut + $timeout.cancel(timer) + # console.log 'ERROR', error doRender: (element, pagenum, page) -> self = this @@ -183,17 +212,42 @@ define [ element.canvas.replaceWith(canvas) + # console.log 'staring page render', pagenum + result = page.render { canvasContext: ctx viewport: viewport } + timedOut = false + + timer = $timeout () -> + # console.log 'page render timed out', pagenum + timedOut = true + result.cancel() + , 30*1000 + result.then () -> + # console.log 'page rendered', pagenum + $timeout.cancel(timer) canvas.removeClass('pdfng-rendering') page.getTextContent().then (textContent) -> textLayer.setTextContent textContent + , (error) -> + console.log 'ERROR', error page.getAnnotations().then (annotations) -> annotationsLayer.setAnnotations annotations + , (error) -> + console.log 'ERROR', error + .catch (error) -> + # console.log 'page render failed', pagenum, error + $timeout.cancel(timer) + if timedOut + # console.log 'calling ERROR callback - was timeout' + self.errorCallback?('timeout') + else if error != 'cancelled' + # console.log 'calling ERROR callback' + self.errorCallback?(error) return result diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index 7f3ac48250..e3d5df0adf 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -35,6 +35,8 @@ define [ $scope.$apply() progressCallback: (progress) -> $scope.$emit 'progress', progress + errorCallback: (error) -> + $scope.$emit 'pdf:error', error }) # we will have all the main information needed to start display