-
Notifications
You must be signed in to change notification settings - Fork 220
Whhtml driver timeout #229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
6243137
0cf2ac7
b205686
ff03674
4a34671
5d3030e
25dfb5b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Changelog | ||
|
|
||
| All notable changes to this project will be documented in this file. | ||
|
|
||
| The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
| and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
|
||
| ## [1.8.0] - 2022-06-03 | ||
|
|
||
| ### Added | ||
|
|
||
| - Add timeout support https://github.com/webgio/Rotativa/issues/203 | ||
|
|
||
| ### Fixed | ||
|
|
||
| - fix when WkhtmlDriver exits with error code https://github.com/webgio/Rotativa/issues/189 | ||
| - fix using HttpContext.Current instead passed ControllerContext https://github.com/webgio/Rotativa/pull/178 | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| <package > | ||
| <metadata> | ||
| <id>Rotativa</id> | ||
| <version>1.7.3</version> | ||
| <version>1.8.0</version> | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i've decided it is Minor, cuz I've added backwards compatible functionality to the public API (ConvertTimeout which is not required) |
||
| <title>Rotativa</title> | ||
| <authors>Giorgio Bozio</authors> | ||
| <owners>Giorgio Bozio</owners> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Rotativa | ||
| { | ||
|
|
@@ -15,7 +17,33 @@ public abstract class WkhtmlDriver | |
| /// <param name="html">String containing HTML code that should be converted to PDF.</param> | ||
| /// <param name="wkhtmlExe"></param> | ||
| /// <returns>PDF as byte array.</returns> | ||
| protected static byte[] Convert(string wkhtmlPath, string switches, string html, string wkhtmlExe) | ||
| protected static byte[] Convert(string wkhtmlPath, string switches, string html, string wkhtmlExe, int? timeout = null) | ||
| { | ||
| if (!timeout.HasValue) | ||
| { | ||
| return ConvertExecute(wkhtmlPath, ref switches, ref html, wkhtmlExe); | ||
| } | ||
| else | ||
| { | ||
| var cancellationTokenSource = new CancellationTokenSource(); | ||
| var task = Task.Run(() => ConvertExecute(wkhtmlPath, ref switches, ref html, wkhtmlExe, cancellationTokenSource.Token)); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not as I could imagine best, but it works. I've tried to do it by calling I've tried to do it also by implementing events: but I failed to convert outputLine.Data to bytes[]. Thats cuz it is String, not Stream (as in proc.StandardOutput.BaseStream). I've tried provide some encoding to process output and convert it, but any way I tried to do it, the resulted PDF was broken. Finally I gave up and it that way |
||
| task.ConfigureAwait(false); | ||
| if (!task.Wait(timeout.Value)) | ||
| { | ||
| cancellationTokenSource.Cancel(); | ||
| throw new TimeoutException($"Timeout in converting given URL or HTML string to PDF after {timeout.Value}"); | ||
| } | ||
|
|
||
| if (task.IsFaulted) | ||
| { | ||
| throw task.Exception ?? new Exception("Failed in converting given URL or HTML string to PDF"); | ||
| } | ||
|
|
||
| return task.Result; | ||
| } | ||
| } | ||
|
|
||
| private static byte[] ConvertExecute(string wkhtmlPath, ref string switches, ref string html, string wkhtmlExe, CancellationToken cancellationToken = default) | ||
| { | ||
| // switches: | ||
| // "-q" - silent output, only errors - no progress messages | ||
|
|
@@ -44,6 +72,12 @@ protected static byte[] Convert(string wkhtmlPath, string switches, string html, | |
| CreateNoWindow = true | ||
| } | ||
| }; | ||
| cancellationToken.Register(() => | ||
| { | ||
| if (!proc.HasExited) | ||
| proc.Kill(); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to kill manually process when timeouted |
||
| }); | ||
|
|
||
| proc.Start(); | ||
|
|
||
| // generate PDF from given HTML string, not from URL | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some code refactor & create tests for checking timeout's