Created
June 6, 2019 09:12
-
-
Save oleksabor/9cffdc2882e356808c0b66e28b4e1a44 to your computer and use it in GitHub Desktop.
to reproduce https://github.com/dotnet/docfx/issues/4488 Requires LibLog 4.2, NLog, TopShelf, TopShelf.NLog packages. Works as console, as windowsService but fails when started as console in the azure build pipeline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Topshelf; | |
using windowsService1.Logging; | |
namespace windowsService1 | |
{ | |
class Program | |
{ | |
static ILog Log = LogProvider.GetCurrentClassLogger(); | |
static void Main(string[] args) | |
{ | |
var rc = HostFactory.Run(x => //1 | |
{ | |
x.UseNLog(); | |
x.Service<ConsoleRunner>(); | |
x.RunAsNetworkService(); //6 | |
x.SetDescription("Sample WindowsService Host"); //7 | |
x.SetDisplayName("TestStuff"); //8 | |
x.SetServiceName("TestStuff"); //9 | |
}); //10 | |
var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode()); //11 | |
Environment.ExitCode = exitCode; | |
Log.Info(() => $"exit code is {exitCode}"); | |
} | |
} | |
class ConsoleRunner : ServiceControl | |
{ | |
ILog Log = LogProvider.GetCurrentClassLogger(); | |
public bool Start(HostControl hostControl) | |
{ | |
var IsOutputToStdout = true; | |
var PdfCommandName = "wkhtmltopdf"; | |
var arguments = "--javascript-delay 3000 -q --no-outline --encoding utf-8 --user-style-sheet \"defaults/default-css.css\""; | |
var stdin = Environment.UserInteractive | |
? " \"D:/work/DirectClientDataSync/docfx_project/_site_pdf/_raw/_site_pdf/pdf/../articles/export.sample.html\" -" | |
: " \"E:/buildagent/network/_work/10/b/_site_pdf/_raw/_site_pdf/pdf/TOC.html\" -"; | |
var TimeoutInMilliseconds = 3000; | |
try | |
{ | |
using (var stream = new MemoryStream()) | |
Run(PdfCommandName, true, IsOutputToStdout, arguments, stdin, TimeoutInMilliseconds, stream); | |
return true; | |
} | |
catch (Exception e) | |
{ | |
Log.FatalException($"failed to start {PdfCommandName}", e); | |
return false; | |
} | |
} | |
public int Run(string PdfCommandName, bool IsInputFromStdin, bool IsOutputToStdout, string arguments, string stdin, int TimeoutInMilliseconds, Stream stream) | |
{ | |
using (var process = new Process | |
{ | |
StartInfo = new ProcessStartInfo | |
{ | |
UseShellExecute = false, | |
RedirectStandardOutput = IsOutputToStdout, | |
RedirectStandardInput = IsInputFromStdin, | |
WindowStyle = ProcessWindowStyle.Hidden, | |
FileName = PdfCommandName, | |
Arguments = arguments + (IsInputFromStdin ? " --read-args-from-stdin " : stdin), | |
} | |
}) | |
{ | |
Log.Debug($"Executing {process.StartInfo.FileName} {process.StartInfo.Arguments}"); | |
process.Start(); | |
if (IsInputFromStdin) | |
{ | |
Log.Debug($"wrinting to stdin {stdin}"); | |
using (var standardInput = process.StandardInput) | |
{ | |
standardInput.AutoFlush = true; | |
standardInput.Write(stdin); | |
} | |
} | |
if (IsOutputToStdout) | |
{ | |
using (var standardOutput = process.StandardOutput) | |
{ | |
standardOutput.BaseStream.CopyTo(stream); | |
} | |
} | |
var exited = process.WaitForExit(TimeoutInMilliseconds); | |
Log.Debug($"has got {process.StartInfo.FileName} output, stream:{stream.Length}Bytes"); | |
return process.ExitCode; | |
} | |
} | |
public bool Stop(HostControl hostControl) | |
{ | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment