2012-07-19 63 views
0

我在經典的ASP中使用IIS 7.5運行一個網站,並使用獨立的應用程序池(ApplicationPoolIdentity)。在這個網站內部,我還通過「WScript.Shell」對象運行方法運行非託管代碼(wkhtmltopdf.exe)。之後,我流的結果文件爲PDF當其他exe運行時應用程序池被困住

set wshell = CreateObject("WScript.Shell") 
wshell.run wkhtmltopdf.exe http://www.mypagehanging.com c:\myfile.pdf", 0, TRUE 
set wshell = nothing 

一切工作正常,但有時我的網站掛起。它完全卡住了。運行在應用程序池(iis apppool \ myapp)身份下的wktmltopdf.exe掛起。

這會導致我的所有網站掛起,因爲我運行程序選項bWaitOnReturn爲true。

我無法將此選項設置爲false,因爲我必須等待腳本在流式傳輸pdf之前完全執行。

無法找到wkhtmltopdf的超時選項。不明白爲什麼wkhtmltopdf掛了。但這可能是由我想呈現的網站而不是wkhtmltopdf造成的。

有什麼建議嗎?

回答

0

我已經解決了這個問題,通過殺死進程,如果它需要非常多的時間來運行。

這是我的代碼應該對某人有幫助。

在ASP文件

'Run this programm that will kill pdf generation if it hangs-execute for more than 10seconds 
wshell.run "KillProcess.exe -p wkhtmltopdf -t 10000",0,FALSE 
'Launch pdf generation with bwaitonreturn true 
wshell.run wkhtmltopdf.exe http://www.mypagehanging.com c:\inetpub\wwwroot\myfile.pdf", 0, TRUE 

'Stream the pdf ... 

爲KillProcess.exe

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using System.Diagnostics; 
using CommandLine; //http://commandline.codeplex.com 
using CommandLine.Text; //http://commandline.codeplex.com 

namespace KillProcess 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var options = new Options(); 
      ICommandLineParser parser = new CommandLineParser(); 

      if (parser.ParseArguments(args, options)) 
      { 
       double maxWaitTime = options.time; //ms 
       string processName = options.process; 
       bool exit; 
       do 
       { 
        exit = true; 
        foreach (Process p in GetProcesses(processName)) 
        { 
         exit = false; 
         try 
         { 
          if (p.StartTime.AddMilliseconds(maxWaitTime) < DateTime.Now) 
          { 
           p.Kill(); 
           Console.WriteLine("Killed Process: {0} ID: {1} started at {2} after {3}ms of execution time", p.ProcessName, p.Id, p.StartTime, options.time); 
          } 
         } 
         catch (Exception e) 
         { 
          Console.WriteLine(e.Message); 
         } 
        } 
        Thread.Sleep(100); 
       } while (!exit); 
       return; 
      } 
      else 
      { 
       Console.WriteLine(options.GetUsage()); 
       return; 
      } 
     } 

     static IEnumerable<Process> GetProcesses(string processName) 
     { 
      var processes = from p in Process.GetProcesses() 
          where p.ProcessName == processName 
          orderby p.ProcessName 
          select p; 
      return processes; 
     } 

     class Options : CommandLineOptionsBase 
     { 
      [Option("p", "process", Required = true, HelpText = "Name of the process-es to kill after some execution time (Warning ! This will kill all the processes that match the name !)")] 
      public string process { get; set; } 

      [Option("t", "time", Required = true, HelpText = "Maximum execution time allowed for the process to run. If the process has run for more than this amount of time it will be killed")] 
      public int time { get; set; } 

      [HelpOption] 
      public string GetUsage() 
      { 
       return HelpText.AutoBuild(this, (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current)); 
      } 
     } 
    } 
} 
源代碼
相關問題