0

我在带有隔离应用程序池 (ApplicationPoolIdentity) 的经典 ASP 中使用 IIS 7.5 运行网站。在这个网站内,我还通过“WScript.Shell”对象运行方法运行非托管代码(wkhtmltopdf.exe)。之后,我将结果文件流式传输为 pdf

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 引起的。

有什么建议么 ?

4

1 回答 1

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));
            }
        }
    }
}
于 2012-08-08T09:43:55.000 回答