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) {
Width: queryReader.Get("width", "800"),
Height: queryReader.Get("height", "400"),
OrgId: c.OrgId,
Timeout: queryReader.Get("timeout", "30"),
Timeout: queryReader.Get("timeout", "60"),
Timezone: queryReader.Get("tz", ""),
}
pngPath, err := renderer.RenderToPng(renderOpts)
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
}
......
package renderer
import (
"errors"
"fmt"
"io"
"os"
......@@ -28,6 +29,7 @@ type RenderOpts struct {
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")
func isoTimeOffsetToPosixTz(isoOffset string) string {
......@@ -75,6 +77,11 @@ func RenderToPng(params *RenderOpts) (string, error) {
renderKey := middleware.AddRenderAuthKey(params.OrgId)
defer middleware.RemoveRenderAuthKey(renderKey)
timeout, err := strconv.Atoi(params.Timeout)
if err != nil {
timeout = 15
}
cmdArgs := []string{
"--ignore-ssl-errors=true",
"--web-security=false",
......@@ -84,6 +91,7 @@ func RenderToPng(params *RenderOpts) (string, error) {
"height=" + params.Height,
"png=" + pngPath,
"domain=" + localDomain,
"timeout=" + strconv.Itoa(timeout),
"renderKey=" + renderKey,
}
......@@ -117,17 +125,12 @@ func RenderToPng(params *RenderOpts) (string, error) {
close(done)
}()
timeout, err := strconv.Atoi(params.Timeout)
if err != nil {
timeout = 15
}
select {
case <-time.After(time.Duration(timeout) * time.Second):
if err := cmd.Process.Kill(); err != nil {
rendererLog.Error("failed to kill", "error", err)
}
return "", fmt.Errorf("PhantomRenderer::renderToPng timeout (>%vs)", timeout)
return "", ErrTimeout
case <-done:
}
......
......@@ -30,10 +30,12 @@
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) {
// 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) {
var msgStack = ['ERROR: ' + msg];
......@@ -59,7 +61,7 @@
return rootScope.panelsRendered >= panels;
});
if (panelsRendered || tries === 1000) {
if (panelsRendered || totalWaitMs > timeoutMs) {
var bb = page.evaluate(function () {
return document.getElementsByClassName("main-view")[0].getBoundingClientRect();
});
......@@ -73,13 +75,12 @@
page.render(params.png);
phantom.exit();
}
else {
tries++;
setTimeout(checkIsReady, 10);
} else {
totalWaitMs += waitBetweenReadyCheckMs;
setTimeout(checkIsReady, waitBetweenReadyCheckMs);
}
}
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