Commit 34cda659 by Torkel Ödegaard

fix: png rendering api & timeout parameter now works as it should AND default…

fix: png rendering api & timeout parameter now works as it should AND default render timeout increased from 30 to 60 seconds, fixes #8710
parent d630f3d5
...@@ -18,14 +18,18 @@ func RenderToPng(c *middleware.Context) { ...@@ -18,14 +18,18 @@ func RenderToPng(c *middleware.Context) {
Width: queryReader.Get("width", "800"), Width: queryReader.Get("width", "800"),
Height: queryReader.Get("height", "400"), Height: queryReader.Get("height", "400"),
OrgId: c.OrgId, OrgId: c.OrgId,
Timeout: queryReader.Get("timeout", "30"), Timeout: queryReader.Get("timeout", "60"),
Timezone: queryReader.Get("tz", ""), Timezone: queryReader.Get("tz", ""),
} }
pngPath, err := renderer.RenderToPng(renderOpts) pngPath, err := renderer.RenderToPng(renderOpts)
if err != nil { if err != nil {
c.Handle(500, "Failed to render to png", err) if err == renderer.ErrTimeout {
c.Handle(500, err.Error(), err)
}
c.Handle(500, "Rendering failed.", err)
return return
} }
......
package renderer package renderer
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
...@@ -28,6 +29,7 @@ type RenderOpts struct { ...@@ -28,6 +29,7 @@ type RenderOpts struct {
Timezone string Timezone string
} }
var ErrTimeout = errors.New("Timeout error. You can set timeout in seconds with &timeout url parameter")
var rendererLog log.Logger = log.New("png-renderer") var rendererLog log.Logger = log.New("png-renderer")
func isoTimeOffsetToPosixTz(isoOffset string) string { func isoTimeOffsetToPosixTz(isoOffset string) string {
...@@ -75,6 +77,11 @@ func RenderToPng(params *RenderOpts) (string, error) { ...@@ -75,6 +77,11 @@ func RenderToPng(params *RenderOpts) (string, error) {
renderKey := middleware.AddRenderAuthKey(params.OrgId) renderKey := middleware.AddRenderAuthKey(params.OrgId)
defer middleware.RemoveRenderAuthKey(renderKey) defer middleware.RemoveRenderAuthKey(renderKey)
timeout, err := strconv.Atoi(params.Timeout)
if err != nil {
timeout = 15
}
cmdArgs := []string{ cmdArgs := []string{
"--ignore-ssl-errors=true", "--ignore-ssl-errors=true",
"--web-security=false", "--web-security=false",
...@@ -84,6 +91,7 @@ func RenderToPng(params *RenderOpts) (string, error) { ...@@ -84,6 +91,7 @@ func RenderToPng(params *RenderOpts) (string, error) {
"height=" + params.Height, "height=" + params.Height,
"png=" + pngPath, "png=" + pngPath,
"domain=" + localDomain, "domain=" + localDomain,
"timeout=" + strconv.Itoa(timeout),
"renderKey=" + renderKey, "renderKey=" + renderKey,
} }
...@@ -117,17 +125,12 @@ func RenderToPng(params *RenderOpts) (string, error) { ...@@ -117,17 +125,12 @@ func RenderToPng(params *RenderOpts) (string, error) {
close(done) close(done)
}() }()
timeout, err := strconv.Atoi(params.Timeout)
if err != nil {
timeout = 15
}
select { select {
case <-time.After(time.Duration(timeout) * time.Second): case <-time.After(time.Duration(timeout) * time.Second):
if err := cmd.Process.Kill(); err != nil { if err := cmd.Process.Kill(); err != nil {
rendererLog.Error("failed to kill", "error", err) rendererLog.Error("failed to kill", "error", err)
} }
return "", fmt.Errorf("PhantomRenderer::renderToPng timeout (>%vs)", timeout) return "", ErrTimeout
case <-done: case <-done:
} }
......
...@@ -30,10 +30,12 @@ ...@@ -30,10 +30,12 @@
height: params.height || '400' height: params.height || '400'
}; };
var tries = 0; var timeoutMs = (parseInt(params.timeout) || 10) * 1000;
var waitBetweenReadyCheckMs = 50;
var totalWaitMs = 0;
page.open(params.url, function (status) { page.open(params.url, function (status) {
// console.log('Loading a web page: ' + params.url + ' status: ' + status); console.log('Loading a web page: ' + params.url + ' status: ' + status, timeoutMs);
page.onError = function(msg, trace) { page.onError = function(msg, trace) {
var msgStack = ['ERROR: ' + msg]; var msgStack = ['ERROR: ' + msg];
...@@ -59,7 +61,7 @@ ...@@ -59,7 +61,7 @@
return rootScope.panelsRendered >= panels; return rootScope.panelsRendered >= panels;
}); });
if (panelsRendered || tries === 1000) { if (panelsRendered || totalWaitMs > timeoutMs) {
var bb = page.evaluate(function () { var bb = page.evaluate(function () {
return document.getElementsByClassName("main-view")[0].getBoundingClientRect(); return document.getElementsByClassName("main-view")[0].getBoundingClientRect();
}); });
...@@ -73,13 +75,12 @@ ...@@ -73,13 +75,12 @@
page.render(params.png); page.render(params.png);
phantom.exit(); phantom.exit();
} } else {
else { totalWaitMs += waitBetweenReadyCheckMs;
tries++; setTimeout(checkIsReady, waitBetweenReadyCheckMs);
setTimeout(checkIsReady, 10);
} }
} }
setTimeout(checkIsReady, 200); setTimeout(checkIsReady, waitBetweenReadyCheckMs);
}); });
})(); })();
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