如何确定服务的一个实例已关闭并启动新实例
嘿 我遇到以下情况:WinService 正在处理队列中的一些内容,写入其他表(在处理期间),完成后删除该条目并移至下一个条目。
我需要一种机制来确定实例是否处于活动状态并正在运行,如果不启动或唤醒第二个实例。
到目前为止我的想法是有两个实例正在进行,每个实例都尝试从队列中获取内容并处理它(尝试 - 看看它是否可以 - 就像没有设置 keepAliveFlag 或记录的 lastUpdateDate 太长前)。当他们处理同一条记录时可能会出现这种情况——这是缺点。
对于上述提出的解决方案,当实例尝试写入 keepAliveFlag 但未成功停止时,我还需要一种机制。
除了这个解决方案之外,还有一个解决方案,即实例不断地将内容写入数据库,第二个实例检查是否写入了新内容,如果没有则唤醒或启动第二个实例。但可能会出现几个问题:如果前任主人醒来会发生什么?一般来说,第二种情况似乎更难正确实施。
现在的问题是:实现这个的正确模式是什么?
Hey
I have the following situation: a WinService which is processing some stuff from a queue, writing to other tables (during processing) and after it finishes it deletes the entry and moves to the next one.
I would need a mechanism to determine if the instance is alive and going and if not to start or wake up a second instance.
What I've thought so far is to have two instances going on where each tries to pick up stuff from the queue and process it (tries- as in see if it can- like no keepAliveFlag is set or lastUpdateDate of a record is too long ago). There could be the situation when they process the same record- this is the disadvantage.
For this above proposed solution, I would also need a sort of mechanism in the case when an instance tries to write a keepAliveFlag and does not succeed to stop.
Aside from this solution, there is also the solution for the instance to constantly write stuff into db and the second instance to check if new stuff is written and if not wake up or launch the second instance. But several problems may appear: what happens if the former master wakes up? And in general this second scenario seems harder to implement correctly.
Now the question: what would be the correct pattern to implement this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
服务在 Windows 系统中是单例的。支持多个实例的唯一方法是安装多个具有不同服务名称的相同二进制文件。
您可以使用
[ServiceController][1]
类及其 状态属性 确定当前状态并采取相应措施。Services are singleton in a Windows system. The only way to support multiple instances would be to install multiple identical binaries with different service names.
You can monitor the status of any given service using the
[ServiceController][1]
class, using its Status property to determine current state and act accordingly.我的一个团队目前所做的事情是:
What one of my teams does at the moment is this: