重新启动与相关服务的服务? [英] Restart a service with dependent services?

查看:123
本文介绍了重新启动与相关服务的服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始一个 CSHARP,例如,并适当注意到相关的SO问题(的Restart从C#和的无法重新启动服务C#)和其他各种问题有关的重新启动只是一个服务的,我不知道什么是最好的方法是对的重新启动服务与相关服务 的(例如:消息队列,上信息队列触发器依赖,或 IIS ,上 FTP发布万维网发布依赖)。 MMC管理单元中执行此自动的,但code似乎没有提供相同的功能(至少不容易)。

Starting with a csharp-example and duly noting related SO questions ( Restart a windows services from C# and cannot restart a Service C#) and various other questions relating to restarting just one service, I'm wondering what the best method is for restarting a service with dependent services (e.g. Message Queuing, on which Message Queuing Triggers depends, or IIS, on which FTP Publishing and World Wide Web Publishing depend). The mmc snap-in does this automagically, but the code doesn't seem to provide the same functionality (at least not as easily).

<一个href="http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.stop.aspx">MSDN文档停止说:如果任何服务依赖于该服务为他们的运营,他们将停止此服务之前,必须停止该DependentServices属性包含了组依赖它的服务,和<一href="http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.dependentservices.aspx"><$c$c>DependentServices返回服务的阵列。假设 StartService() StopService()遵循的例子,并概述了公约,如上面的(除非他们接受引用 ServiceControllers 时间跨度直接),我开始:

MSDN documentation for Stop says "If any services depend on this service for their operation, they will be stopped before this service is stopped. The DependentServices property contains the set of services that depend on this one," and DependentServices returns an array of services. Assuming StartService() and StopService() follow the conventions outlined in the examples and such referenced above (except that they accept ServiceControllers and TimeSpans directly), I started with:

public static void RestartServiceWithDependents(ServiceController service, TimeSpan timeout)
{
    ServiceController[] dependentServices = service.DependentServices;

    RestartService(service, timeout); // will stop dependent services, see note below* about timeout...

    foreach (ServiceController dependentService in dependentServices)
    {
        StartService(dependentService, timeout);
    }
}

但是,如果该服务依赖嵌套(递归)或循环(如果这甚至有可能...) - 如果服务A 依赖于服务B1 服务B2 服务C1 依赖于服务B1 ,似乎重新启动服务A 用这种方法将停止服务C1 ,但不会重新启动......

But what if the service dependencies are nested (recursive) or cyclical (if that's even possible...) - if Service A is depended on by Service B1 and Service B2 and Service C1 depends on Service B1, it seems 'restarting' Service A by this method would stop Service C1 but wouldn't restart it...

为使这个例子画面更清晰,我会按照该模型在服务MMC管理单元中:

To make this example picture clearer, I'll follow the model in the services mmc snap-in:

The following system components depend on [Service A]:
  - Service B1
    - Service C1
  - Service B2

有没有更好的办法去了解这一点,还是会只需要递归步入停止每个相关服务,然后重新启动它们都将重新启动主售后服务?

Is there a better way to go about this, or would it just have to recursively step into and stop each dependent service and then restart them all after it restarts the main service?

此外,将相关的,但是的当前已停止的服务列在DependentServices?如果是这样,这是不是重新启动它们反正?如果是这样,我们应该控制的呢?这似乎只是让混乱和混乱......

Additionally, will dependent but currently stopped services be listed under DependentServices? If so, wouldn't this restart them anyways? If so, should we control that as well? This just seems to get messier and messier...

*注:我知道了暂停没有被完全正确应用在这里(总超时可能是很多很多次超过预期),但现在这不是的问题,我很担心 - 如果你要修复它,罚款,但不只是说'超时的破......

*Note: I realize the timeout isn't being applied completely correctly here (overall timeout could be many many times longer than expected), but for now that's not the issue I'm concerned about - if you want to fix it, fine, but don't just say 'timeout's broken...'

更新:经过一番preliminary测试,我发现(/确认)以下行为:

Update: After some preliminary testing, I've discovered (/confirmed) the following behaviors:

  • 停止服务(如服务A )的其他服务(如服务B1 )是否取决于将停止其他服务(包括嵌套的依赖,如服务C1
  • <一个href="http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.dependentservices.aspx"><$c$c>DependentServices 确实的包括在所有状态(运行,停止等)相关的服务,而且还包括嵌套的相关性,即 Service_A.DependentServices 将包含 {服务B1,服务C1,服务B2} (按照这个顺序,如 C1 依赖于 B1 )。
  • 在开始依赖于其他服务(如服务B1 依赖于服务A )也将启动必要的服务。
  • Stopping a service (e.g. Service A) that other services (e.g. Service B1) depend on will stop the other services (including "nested" dependencies such as Service C1)
  • DependentServices does include dependent services in all states (Running, Stopped, etc.), and it also includes nested dependencies, i.e. Service_A.DependentServices would contain {Service B1, Service C1, Service B2} (in that order, as C1 depends on B1).
  • Starting a service that depends on others (e.g. Service B1 depends on Service A) will also start the requisite services.

在code以上,因此可以简化(至少部分),只停留在主要服务(这将停止所有相关服务),然后重新启动最相关的服务(如服务C1 服务B2 )(或只是重新启动全的相关服务 - 这将跳过已经开始了的),但实际上只是推迟主要服务的启动瞬间,直到依赖的一个抱怨是,这样并不能真正帮助。

The code above can therefore be simplified (at least in part) to just stop the main service (which will stop all dependent services) and then restarting the most-dependent services (e.g. Service C1 and Service B2) (or just restarting "all" the dependent services - it will skip the ones already started), but that really just defers the starting of the main service momentarily until one of the dependencies complain about it, so that doesn't really help.

看起来现在好像刚刚重新启动所有依赖关系是最简单的方式,但忽略了(现在)管理的是已经停止和服务,例如...

Looks for now like just restarting all the dependencies is the simplest way, but that ignores (for now) managing services that are already stopped and such...

推荐答案

好了,终于实现了这个。我已经张贴作为一个单独的答案,因为我已经得出这样的结论,在原始更新我的问题,这是之前的第一个答案公布。

Alright, finally implemented this. I've posted it as a separate answer as I had already come to this conclusion in the original update to my question, which was posted prior to the first answer.

同样, StartService() StopService() RestartService()方法遵循的例子概述和这种已经在问题本身所引用的约定(即它们包裹启动/停止行为,避免已经启动/停止型例外)的增编,如果服务传入(如低于的情况下),刷新()上调用该服务检查其<$ C之前$ C>状态。

Again, the StartService(), StopService() and RestartService() methods follow the conventions outlined in the examples and such already referenced in the question itself (i.e. they wrap Start/Stop behavior to avoid "already started/stopped"-type exceptions) with the addendum that if a Service is passed in (as is the case below), Refresh() is called on that service before checking its Status.

public static void RestartServiceWithDependents(ServiceController service, TimeSpan timeout)
{
    int tickCount1 = Environment.TickCount; // record when the task started

    // Get a list of all services that depend on this one (including nested
    //  dependencies)
    ServiceController[] dependentServices = service.DependentServices;

    // Restart the base service - will stop dependent services first
    RestartService(service, timeout);

    // Restore dependent services to their previous state - works because no
    //  Refresh() has taken place on this collection, so while the dependent
    //  services themselves may have been stopped in the meantime, their
    //  previous state is preserved in the collection.
    foreach (ServiceController dependentService in dependentServices)
    {
        // record when the previous task "ended"
        int tickCount2 = Environment.TickCount;
        // update remaining timeout
        timeout.Subtract(TimeSpan.FromMilliseconds(tickCount2 - tickCount1));
        // update task start time
        tickCount1 = tickCount2;
        switch (dependentService.Status)
        {
            case ServiceControllerStatus.Stopped:
            case ServiceControllerStatus.StopPending:
                // This Stop/StopPending section isn't really necessary in this
                //  case as it doesn't *do* anything, but it's included for
                //  completeness & to make the code easier to understand...
                break;
            case ServiceControllerStatus.Running:
            case ServiceControllerStatus.StartPending:
                StartService(dependentService, timeout);
                break;
            case ServiceControllerStatus.Paused:
            case ServiceControllerStatus.PausePending:
                StartService(dependentService, timeout);
                // I don't "wait" here for pause, but you can if you want to...
                dependentService.Pause();
                break;
        }
    }
}

这篇关于重新启动与相关服务的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆