如何实现每小时运行一次但也可以从 .aspx 页面触发的作业?
我需要一种经常运行的方法来进行一些数据库处理。 但是,我可能需要它可以由网站管理员触发。 但我不希望此方法同时运行多次,因为这可能会导致其访问数据库的方式出现问题。
例如,我可以...
创建一个在计时器上运行该方法的单例类,并在 global.asax 文件中实例化它。 然后,由于它是单例,我可以从普通的 .aspx 页面调用它,并在需要时调用该方法。 我可能需要使用 C# 的“锁定”功能来检查该方法是否已经在运行。
我最近听到有人说单身人士是“邪恶的”,但这似乎是最合适的。 你怎么认为? 提前致谢。
I need a method to run every so often that does some database processing. However, I may need it to be triggerable by an admin on the site. But I don't want this method being run more than once at the same time, as this could cause issues with the way it hits the database.
For example, could I...
Create a singleton class that runs the method on a timer, and instantiate it in the global.asax file. Then, since it's a singleton, I can call it from my normal .aspx pages and call the method whenever I want. I would probably need to use that "lock" feature of C# to check to see if the method is already running.
I heard some talk lately that Singletons are "evil", but this seems like the perfect fit for it. What do you think? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Joel Spolsky 在 SO 播客之一中提到的另一个选项,我相信是 #20 之类的。 是在应用程序启动时设置一个空的缓存对象,并具有特定的到期日期,并在 CacheItemRemovedCallback 调用页面或执行一些工作,然后重置空缓存对象。
我可能严重错误地引用了他的话,所以我建议您聆听或亲自查看文字记录。
Another option which Joel Spolsky mentioned in one of the SO Podcasts, i believe it was #20 something. Is to set an empty Cache object on application start with a certain expiration date, and in the CacheItemRemovedCallback make a call out to page or do some work and then reset the empty cache object.
I'm probably horribly mis-quoting him, so I recommend you listen or look through the transcripts for yourself.
只在数据库中设置一个标志并检查它以确定作业是否正在运行怎么样? 在我看来似乎更简单。
What about just setting up a flag in the database and checking that to determine if the job is running or not? Seems simpler IMO.
编写单例的规范方法最终不是线程安全的。 特别是在 webby 环境中,线程甚至不需要位于同一台机器上!
如果您真的想要做一个“单例”,请将其视为仅部署到一台计算机的服务。 然后使用数据库的事务语义(如 Marc Gravell 建议的那样)来同步锁。
The canonical way to write a singleton ends up not being thread safe. Especially in a webby environment, where threads needn't even be on the same machine!
If you really want to do a "singleton", think of it as a service that you only ever deploy to one machine. Then use the transactional semantics of your database like Marc Gravell suggests to synchronize the locks.
我们已经完成了类似的事情,方法是使用 Web 服务进行后端处理,然后编写一个桌面应用程序以根据我们需要的任何时间表调用它。 然后,我们可以在服务器上运行该应用程序,或者管理员可以直接从他们的 PC 运行它来触发作业。
编辑:在我看到您的修订版后,您不希望它们同时运行,我们通常只是使用数据库标志来控制它,就像其他一些人所说的那样,没什么花哨的,但它完成了工作
We've done similar things by using a Web Service to do the backend processing, then writing a Desktop App to call it on whatever schedule we need. We can then run that app on a server, or an admin can run it directly from their PC to trigger the job.
Edit: After I saw your revision that you don't want them to run simulatenously, we have usually just controlled that with a database flag like a few others have said, nothing fancy but it gets the job done
设置应用程序范围的变量以表示进程正在运行。 这应该比将变量存储在数据库中容易一点,对吧?
Set an Application wide variable to denote that the process is running. That should be a little easier than storing the variable in the database, right?
计时器和锁(旨在同步对数据库的访问)在网络上是一个坏主意; 您可能在不同的服务器上有零个、一个或多个应用程序池。 它们可以随时回收,直到需要时才会旋转。 基本上,这不会阻止您从多个来源锤击数据库。
就我个人而言,我很想编写一个服务来完成这项工作(数据库轮询或通过 WCF 等),或者使用数据库(SP 或类似的) - 在表行中设置一个标志来表示“进行中”,在数据库中执行工作,并清除标志(重复尝试在进行时立即退出)。
Timers and locks (that are intended to synchronize access to the database) are a bad idea on the web; you may have zero, one or many app-pools on different servers. They may recycle at any time, and won't be spun up until needed. Basically, this won't prevent you hammering the db from multiple sources.
Personally, I'd be tempted to either write a service to do this work (either db-polling, or via WCF etc), or use the db (a SP or similar) - set a flag in a table-row to say "in progress", do the work at the db, and clear the flag (duplicate attempts exit immediately while in progress).
我会这样做
StealBorrow LFSR Consultings 在数据库中设置一个标志,用于检查进程当前是否正在运行单身人士并不邪恶,他们只是很容易受到虐待。
I would do it this way
StealBorrow LFSR Consultings idea for a flag in the DB which does the work of checking if the process is currently runningAnd Singletons aren't evil they just get abused easily.