如何实现每小时运行一次但也可以从 .aspx 页面触发的作业?

发布于 2024-07-15 04:51:58 字数 300 浏览 8 评论 0原文

我需要一种经常运行的方法来进行一些数据库处理。 但是,我可能需要它可以由网站管理员触发。 但我不希望此方法同时运行多次,因为这可能会导致其访问数据库的方式出现问题。

例如,我可以...

创建一个在计时器上运行该方法的单例类,并在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

流绪微梦 2024-07-22 04:51:59

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.

初见你 2024-07-22 04:51:59

只在数据库中设置一个标志并检查它以确定作业是否正在运行怎么样? 在我看来似乎更简单。

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.

七婞 2024-07-22 04:51:59

编写单例的规范方法最终不是线程安全的。 特别是在 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.

别挽留 2024-07-22 04:51:59

我们已经完成了类似的事情,方法是使用 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

夜清冷一曲。 2024-07-22 04:51:59

设置应用程序范围的变量以表示进程正在运行。 这应该比将变量存储在数据库中容易一点,对吧?

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?

你怎么敢 2024-07-22 04:51:58

计时器和锁(旨在同步对数据库的访问)在网络上是一个坏主意; 您可能在不同的服务器上有零个、一个或多个应用程序池。 它们可以随时回收,直到需要时才会旋转。 基本上,这不会阻止您从多个来源锤击数据库。

就我个人而言,我很想编写一个服务来完成这项工作(数据库轮询或通过 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).

誰認得朕 2024-07-22 04:51:58

我会这样做

  1. 构建一个正常的 ASP.NET 页面,该页面执行处理
  2. StealBorrow LFSR Consultings 在数据库中设置一个标志,用于检查进程当前是否正在运行
  3. 使用普通的 cronjob 或 Windows 任务计划程序定期调用网页。

单身人士并不邪恶,他们只是很容易受到虐待。

I would do it this way

  1. Build a normal ASP.NET page which does the processing
  2. StealBorrow LFSR Consultings idea for a flag in the DB which does the work of checking if the process is currently running
  3. Use normal cronjob or windows task scheduler to call the web page on a regular basis.

And Singletons aren't evil they just get abused easily.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文