如何确定服务的一个实例已关闭并启动新实例

发布于 2024-09-29 17:26:43 字数 466 浏览 1 评论 0原文

嘿 我遇到以下情况: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 技术交流群。

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

发布评论

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

评论(2

半岛未凉 2024-10-06 17:26:43

服务在 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.

╰沐子 2024-10-06 17:26:43

我的一个团队目前所做的事情是:

  • 在每台计算机上安装一个监视器实例(本身就是一个 Windows 服务)。
  • 该监视器(从中央数据库)读取本地服务列表它应该监控。
  • 它还读取它正在监视的每个本地服务的工作/非工作时间。
  • 然后,它监视每个服务的定期检测信号(以及其他状态消息)。
  • 如果本地服务在工作时间内未启动,监视器将启动它。
  • 如果本地服务正在运行但没有检测信号,监视器将重新启动它。
  • 监视器将每个服务的状态记录在数据库中,并根据需要发送信息/警告/警报消息。

What one of my teams does at the moment is this:

  • On each machine, install an instance of a monitor (itself a Windows service).
  • This monitor reads (from a central database) the list of local services that it should monitor.
  • It also reads the working/non-working hours for each local service that it's monitoring.
  • Then it monitors each service for regular heartbeats (and other status messages).
  • If a local service has not started during its working hours, the monitor starts it.
  • If a local service is running but not heartbeating, the monitor restarts it.
  • The monitor logs in the database the state of each service and sends info/warning/alert messages as appropriate.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文