我用 C# 和 Quartz.NET 创建的 Windows 服务的提示和技巧
我目前正在进行一个项目,该项目正在创建一个 Windows 服务,该服务本质上是在多个路径上移动文件。作业可能是每 60 秒从 FTP 服务器获取与正则表达式匹配的所有文件并将它们传输到网络路径,等等。这些作业存储在 SQL 数据库中。
目前,该服务采用控制台应用程序的形式,以便于开发。作业是使用 ASP.NET 页面添加的,并且可以使用另一个 ASP.NET 页面进行编辑。
不过,我有一些问题,一些与 Quartz.NET 有关,还有一些一般问题。
Quartz.NET:1
:这是我遇到的最大问题。由于我暂时将应用程序开发为控制台应用程序,因此我必须在所有文件/页面上创建一个新的 Quartz.NET 调度程序。这导致了多个令人困惑的错误,但我只是不知道如何在一个全局文件中启动调度程序,并在我的 ASP.NET 页面中访问这些文件(例如,我可以将详细信息放入网格视图中进行编辑)
2 :我的经理建议我可以考虑在 Quartz.NET 中进行多个“配置”。我的意思是,在任何给定时间,管理员都可以更改应用程序配置,以便仅运行专门选择的应用程序。在 Quartz.NET 中执行此操作最简单的方法是什么?
一般:
1:在此应用程序中至关重要的一件事是确保文件已被移动并且它实际上位于目标路径上(移动后原始文件被删除,因此如果文件在未移动时被删除,那将是灾难性的)实际上没有被复制!)。我还需要确保文件内容在初始路径和目标路径上匹配,以便让您放心复制的内容是正确的。我目前正在通过 MD5 哈希初始文件、复制文件并在删除它之前确保该文件存在于服务器上来执行此操作。然后我对服务器上的文件进行哈希处理并确保哈希值匹配。有更简单的方法吗?我担心散列可能会给系统带来压力。
2:这与上述问题相关,但并不那么重要,因为甚至我的经理都不知道我会如何做到这一点,但我很乐意实现这一点。如果在写入文件时执行作业,则会出现问题,这可能会传输写入一半的文件,从而使其完全无用,而且也很糟糕,因为初始文件会在写入时被破坏。正在写入!有没有办法检查这个?
I have a project ongoing at the moment which is create a Windows Service that essentially moves files around multiple paths. A job may be to, every 60 seconds, get all files matching a regular expression from an FTP server and transfer them to a Network Path, and so on. These jobs are stored in an SQL database.
Currently, the service takes the form of a console application, for ease of development. Jobs are added using an ASP.NET page, and can be editted using another ASP.NET page.
I have some issues though, some relating to Quartz.NET and some general issues.
Quartz.NET:
1: This is the biggest issue I have. Seeing as I'm developing the application as a console application for the time being, I'm having to create a new Quartz.NET scheduler on all my files/pages. This is causing multiple confusing errors, but I just don't know how to institate the scheduler in one global file, and access these in my ASP.NET pages (so I can get details into a grid view to edit, for example)
2: My manager would suggested I could look into having multiple 'configurations' inside Quartz.NET. By this, I mean that at any given time, an administrator can change the applications configuration so that only specifically chosen applications run. What'd be the easiest way of doing this in Quartz.NET?
General:
1: One thing that that's crucial in this application is assurance that the file has been moved and it's actually on the target path (after the move the original file is deleted, so it would be disastrous if the file is deleted when it hasn't actually been copied!). I also need to make sure that the files contents match on the initial path, and the target path to give peace of mind that what has been copied is right. I'm currently doing this by MD5 hashing the initial file, copying the file, and before deleting it make sure that the file exists on the server. Then I hash the file on the server and make sure the hashes match up. Is there a simpler way of doing this? I'm concerned that the hashing may put strain on the system.
2: This relates to the above question, but isn't as important as not even my manager has any idea how I'd do this, but I'd love to implement this. An issue would arise if a job is executed when a file is being written to, which may be that a half written file will be transferred, thus making it totally useless, and it would also be bad as the the initial file would be destroyed while it's being written to! Is there a way of checking of this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所发现的,在 ASP.NET 中运行 Quartz 调度程序会带来许多问题。查看 Marko Lahma 对有关在 ASP.NET Web 应用程序中运行调度程序的问题的答复:
Quartz.Net 调度程序在本地工作,但不在远程主机上工作
就防止作业之间的竞争条件而言(例如,尝试删除尚未实际复制到远程主机的文件)文件系统),您需要实现的是某种作业链:
http:// quartznet.sourceforge.net/faq.html#howtochainjobs
过去,我使用 TriggerListeners 和 JobListeners 来完成与您需要的类似的操作。基本上,您注册事件侦听器,等待执行某些作业,直到另一个作业完成后。测试这些侦听器并了解触发这些事件时发生的情况非常重要。您很容易发现自己实现的解决方案在开发中似乎工作正常(误报),但在生产中却无法工作,而不了解调度程序如何以及何时执行与异步作业执行相关的某些操作。
祝你好运!调度器很有趣!
As you've discovered, running the Quartz scheduler inside an ASP.NET presents many problems. Check out Marko Lahma's response to your question about running the scheduler inside of an ASP.NET web app:
Quartz.Net scheduler works locally but not on remote host
As far as preventing race conditions between your jobs (eg. trying to delete a file that hasn't actually been copied to the file system yet), what you need to implement is some sort of job-chaining:
http://quartznet.sourceforge.net/faq.html#howtochainjobs
In the past I've used the TriggerListeners and JobListeners to do something similar to what you need. Basically, you register event listeners that wait to execute certain jobs until after another job is completed. It's important that you test out those listeners, and understand what's happening when those events are fired. You can easily find yourself implementing a solution that seems to work fine in development (false positive) and then fails to work in production, without understanding how and when the scheduler does certain things with regards to asynchronous job execution.
Good luck! Schedulers are fun!