windows 您如何处理在 IIS 上运行的网站的计划任务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/382997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:45:22  来源:igfitidea点击:

How do you handle scheduled tasks for your websites running on IIS?

asp.netwindowsscheduled-tasks

提问by Matt Ephraim

I have a website that's running on a Windows server and I'd like to add some scheduled background tasks that perform various duties. For example, the client would like users to receive emails that summarize recent activity on the site.

我有一个在 Windows 服务器上运行的网站,我想添加一些执行各种职责的预定后台任务。例如,客户希望用户收到总结网站最近活动的电子邮件。

If sending out emails was the only task that needed to be performed, I would probably just set up a scheduled task that ran a script to send out those emails. However, for this particular site, the client would like a variety of different scheduled tasks to take place, some of them always running and some of them only running if certain conditions are met. Right now, they've given me an initial set of things they'd like to see implemented, but I know that in the future there will be more.

如果发送电子邮件是唯一需要执行的任务,我可能会设置一个运行脚本的计划任务来发送这些电子邮件。但是,对于此特定站点,客户端希望执行各种不同的计划任务,其中一些始终运行,而其中一些仅在满足特定条件时运行。现在,他们给了我一套他们希望看到实施的初步东西,但我知道将来会有更多。

What I am wondering is if there's a simple solution for Windows that would allow me to define the tasks that needed to be run and then have one scheduled task that ran daily and executed each of the scheduled tasks that had been defined. Is a batch file the easiest way to do this, or is there some other solution that I could use?

我想知道的是,是否有一个简单的 Windows 解决方案可以让我定义需要运行的任务,然后有一个每天运行的计划任务并执行每个已定义的计划任务。批处理文件是最简单的方法,还是我可以使用其他一些解决方案?

采纳答案by Kev

To keep life simple, I would avoid building one big monolithic exe and break the work to do into individual tasks and have a Windows scheduled task for each one. That way you can maintain the codebase more easily and change functionality at a more granular level.

为了简单起见,我会避免构建一个庞大的单体 exe,并将要完成的工作分解为单独的任务,并为每个任务设置一个 Windows 计划任务。这样您就可以更轻松地维护代码库并在更细粒度的级别更改功能。

You could, later down the line, build a windows service that dynamically loads plugins for each different task based on a schedule. This may be more re-usable for future projects.

稍后,您可以构建一个 Windows 服务,该服务根据计划为每个不同的任务动态加载插件。这对于未来的项目可能更易于重用。

But to be honest if you're on a deadline I'd apply the KISS principle and go with a scheduled task per task.

但老实说,如果你在截止日期前,我会应用 KISS 原则并为每个任务安排一个计划任务。

回答by JasonS

I would go with a Windows Service right out of the gates. This is going to be the most extensible method for your requirements, creating the service isn't going to add much to your development time, and it will probably save you time not too far down the road.

我会直接使用 Windows 服务。这将是满足您需求的最可扩展的方法,创建服务不会增加您的开发时间,而且它可能会在不久的将来为您节省时间。

回答by Oleg Yaroshevych

We use Windows Scheduler Service which launches small console application that just passes parameters to the Web Service.

我们使用 Windows 调度程序服务,它启动只将参数传递给 Web 服务的小型控制台应用程序。

For example, if user have scheduled reports #388 and #88, scheduled task is created with command line looking like this:

例如,如果用户有计划报告 #388 和 #88,计划任务是使用命令行创建的,如下所示:

c:\launcher\app.exe report:388 report:88

c:\launcher\app.exe 报告:388 报告:88

When scheduler fires, this app just executes web method on web service, for example, InternalService.SendReport(int id).

当调度程序触发时,这个应用程序只是在网络服务上执行网络方法,例如,InternalService.SendReport(int id)。

Usually you already have all required business logic available in your Web application. This approach allows to use it with minimal efforts, so there is no need to create any complex .exe or windows service with pluggable modules, etc.

通常,您的 Web 应用程序中已经拥有所有必需的业务逻辑。这种方法允许以最少的努力使用它,因此无需使用可插拔模块等创建任何复杂的 .exe 或 windows 服务。

回答by Ross Presser

The problem with doing the operations from the scheduled EXE, rather than from inside a web page, is that the operations may benefit from, or even outright require, resources that the web page would have -- IIS cache and an ORM cache are two things that come to mind. In the case of ORM, making database changes outside the web app context may even be fatal. My preference is to schedule curl.exe to request the web page from localhost.

从预定的 EXE 执行操作而不是从网页内部执行操作的问题在于,这些操作可能受益于甚至完全需要网页将拥有的资源——IIS 缓存和 ORM 缓存是两件事想到的。在 ORM 的情况下,在 Web 应用程序上下文之外进行数据库更改甚至可能是致命的。我的偏好是安排 curl.exe 从本地主机请求网页。

回答by Mehrdad Afshari

Use the Windows Scheduled Tasks or create a Windows Service that does the scheduling itself.

使用 Windows 计划任务或创建自己执行计划的 Windows 服务。