自动更新服务
我现在已经在 Delphi 中编写了几个服务,但我想添加从 LAN unc 路径或从 http 服务器自动更新服务的功能。我一直在思考这个问题,我很想听听人们的想法。我可以创建一个线程来定期检查更新,但如何停止自动卸载和安装服务。我最初的想法是编写一个控制台应用程序来执行此操作并使用创建进程启动它,然后让服务停止并让控制台应用程序完成工作,在退出之前启动新版本的服务。这是一个好的策略还是我应该考虑其他策略。提前致谢
I have written several services in Delphi now, but I want to add the facility of auto updating the service either from a LAN unc path or from a http server. I have been pondering this and I am interested to hear peoples ideas. I can create a thread that will check for the update periodically, but how do I go about stopping the service uninstalling and installing automatically. My initial thoughts where to write a console app to do this and start it using create process, then let the service stop and the console app do the work, starting the new version of the service before it exits. Is this a good stratergy or shoul I consider something else. Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我按照你的建议去做。线程偶尔检查更新。如果存在,它会下载它并将其放入适当的位置。然后它会验证它是否健康(不希望它被损坏!)。最后,该线程启动另一个应用程序,其中包含参数告诉它要做什么,具体来说,是服务的名称、要替换的文件的位置以及要替换的文件。然后服务就等着了。
当更新程序应用程序启动时,它会暂停一会儿以确保服务完全稳定,然后使用服务控制 API 来停止服务。然后它会监视它,直到它消失。最后,它会暂停一下,以确保 Windows 确实完成了该文件的处理。然后,它开始重命名旧文件以将其移开(如果仍在使用,则会重试几次),然后将新文件复制到位。最后,它再次启动服务。然后更新程序退出。
这对于我的服务以及独立应用程序来说都非常可靠(更新程序应用程序具有不同的参数来了解哪种模式)。如果你小心的话,你可以使用完全相同的系统来更新更新程序,这很值得一看。
I do as you suggest. A thread checks occasionally for an update. If it is present, it downloads it and puts it into an appropriate place. It then verifies that it is wholesome (don't want it to be broken!). Finally, the thread then launches another app with parameters to tell it what to do, specifically, the name of the service, the location of the file to replace, and the file to replace it with. Then the service just waits.
When the updater app starts, it pauses a moment to make sure that the service is all stable, and then it uses the service control API to stop the service. It then monitors it until it is gone. Finally, it pauses a little to ensure that Windows has really finished with the file. Then it starts the process of renaming the old file to move it out of the way (if still in use, it retries a few times), and then copying the new file into place. And finally, it starts the service up again. Then the updater quits.
This has worked quite reliably for my services, and also standalone apps too (with different parameters for the updater app to know which mode). And if you are careful, you can update the updater using the exact same system, which is nice to watch.
我希望该服务是一个 shell,仅更新实际代码所在的另一个可执行文件或 DLL 文件。
在 shell 和子进程之间使用某种通信方法来强制关闭,然后让 shell 执行升级并重新启动子进程。
附带说明一下,这使得调试服务变得更加容易,并且您将能够直接运行子进程,而不必担心调试 Windows 服务所需的额外工作。
I would have the service be a shell that only updates another executable or DLL file where the real code is at.
Have some communication method between the shell and the child process to force a shutdown and then have the shell perform the upgrade and relaunch the child.
As a side note, this makes debugging the service much easier as well as you'll be able to run the child process directly without having to worry about the extra efforts required to debug windows services.
你的想法对我来说似乎很好,但是也要考虑到这一点:
- 将模块(主核心)添加到将被卸载的服务中,并在有更新可用时加载更新的模块(*.dll 文件)——此时服务应该将“任务”放入队列或其他内容中...
此外,您可以使用插件和/或脚本,例如 Pascal script 或 DWScript
your idea seems very good to me, however take this into consideration aswell:
- add module(the main core) to the service that will be unloaded and will load the updated module(*.dll file) when an update is available -- in this time the service should put the "tasks" in a queue or something...
additionally you can use plugins and/or scripts like Pascal script or DWScript
Windows 的最新版本(我认为是从 windows 10 开始)不允许服务启动其他程序。因此,您将需要其他程序来运行更新。它可能是其他服务。
Last versions of Windows (I think since windows 10) does not allow a service to start other programs. So you will need an other program to run the update. It could be an other service.