RunToCompletionRunToCompletion

从 7.1 版开始,Service Fabric 支持适用于容器应用程序和来宾可执行文件应用程序的 RunToCompletion 语义。Starting with version 7.1, Service Fabric supports RunToCompletion semantics for containers and guest executable applications. 这些语义使应用程序和服务一完成任务就退出,不需始终运行应用程序和服务。These semantics enable applications and services which complete a task and exit, in contrast to, always running applications and services.

继续阅读本文之前,建议先熟悉 Service Fabric 应用程序模型Service Fabric 托管模型Before proceeding with this article, we recommend getting familiar with the Service Fabric application model and the Service Fabric hosting model.

备注

使用 Reliable Services 编程模型编写的服务目前不支持 RunToCompletion 语义。RunToCompletion semantics are currently not supported for services written using the Reliable Services programming model.

RunToCompletion 语义和规范RunToCompletion semantics and specification

导入 ServiceManifest时,可以将 RunToCompletion 语义指定为 ExecutionPolicy。RunToCompletion semantics can be specified as an ExecutionPolicy when importing the ServiceManifest. 指定的策略由构成该 ServiceManifest 的所有 CodePackage 继承。The specified policy is inherited by all the CodePackages comprising the ServiceManifest. 以下 ApplicationManifest.xml 代码片段提供了一个示例。The following ApplicationManifest.xml snippet provides an example.

<ServiceManifestImport>
  <ServiceManifestRef ServiceManifestName="RunToCompletionServicePackage" ServiceManifestVersion="1.0"/>
  <Policies>
    <ExecutionPolicy Type="RunToCompletion" Restart="OnFailure"/>
  </Policies>
</ServiceManifestImport>

ExecutionPolicy 允许以下两个属性:ExecutionPolicy allows the following two attributes:

  • 类型: RunToCompletion 当前是此属性的唯一允许值。Type: RunToCompletion is currently the only allowed value for this attribute.
  • Restart: 此属性指定在失败时应用于构成该 ServicePackage 的 Codepackage 的重启策略。Restart: This attribute specifies the restart policy that is applied to CodePackages comprising the ServicePackage, on failure. 以“非零退出代码”退出的 CodePackage 被视为已失败。A CodePackage exiting with a non-zero exit code is considered to have failed. 此属性的允许值为 OnFailureNever,默认值为 OnFailureAllowed values for this attribute are OnFailure and Never with OnFailure being the default.

当重启策略设置为 OnFailure 时,如果有任何 CodePackage 失败 (退出代码为非零值) ,则会重启,在反复失败的情况下会进行回退。With restart policy set to OnFailure, if any CodePackage fails (non-zero exit code), it is restarted, with back-offs between repeated failures. 当重启策略设置为 Never 时,如果有任何 CodePackage 失败,则 DeployedServicePackage 的部署状态会被标记为“失败”,但其他 CodePackage 可以继续执行。With restart policy set to Never, if any CodePackage fails, the deployment status of the DeployedServicePackage is marked as Failed but other CodePackages are allowed to continue execution. 如果构成该 ServicePackage 的所有 CodePackage 都运行至成功完成 (退出代码为 0) ,则 DeployedServicePackage 的部署状态会被标记为“RanToCompletion”。If all the CodePackages comprising the ServicePackage run to successful completion (exit code 0), the deployment status of the DeployedServicePackage is marked as RanToCompletion.

使用了 RunToCompletion 语义的完整示例Complete example using RunToCompletion semantics

让我们看一个使用了 RunToCompletion 语义的完整示例。Let's look at a complete example using RunToCompletion semantics.

重要

以下示例假定你熟悉如何创建使用 Service Fabric 和 Docker 的 Windows 容器应用程序The following example assumes familiarity with creating Windows container applications using Service Fabric and Docker.

此示例引用了 mcr.microsoft.com/windows/nanoserver:1809。This example references mcr.microsoft.com/windows/nanoserver:1809. Windows Server 容器并非在所有主机 OS 版本间都兼容。Windows Server containers are not compatible across all versions of a host OS. 若要了解详细信息,请参阅 Windows 容器版本兼容性To learn more, see Windows Container Version Compatibility.

以下 ServiceManifest.xml 描述了一个 ServicePackage,其中包含表示容器的两个 Codepackage。The following ServiceManifest.xml describes a ServicePackage consisting of two CodePackages, which represent containers. RunToCompletionCodePackage1 直接将消息记录到 stdout 并退出。RunToCompletionCodePackage1 just logs a message to stdout and exits. RunToCompletionCodePackage2 将在一段时间内对环回地址执行 ping 操作,然后以退出代码 012 退出。RunToCompletionCodePackage2 pings the loopback address for a while and then exits with an exit code of either 0, 1 or 2.

<?xml version="1.0" encoding="UTF-8"?>
<ServiceManifest Name="WindowsRunToCompletionServicePackage" Version="1.0" xmlns="http://schemas.microsoft.com/2011/01/fabric" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Description>Windows RunToCompletion Service</Description>
  <ServiceTypes>
    <StatelessServiceType ServiceTypeName="WindowsRunToCompletionServiceType"  UseImplicitHost="true"/>
  </ServiceTypes>
  <CodePackage Name="RunToCompletionCodePackage1" Version="1.0">
    <EntryPoint>
      <ContainerHost>
        <ImageName>mcr.microsoft.com/windows/nanoserver:1809</ImageName>
        <Commands>/c,echo Hi from RunToCompletionCodePackage1 &amp;&amp; exit 0</Commands>
        <EntryPoint>cmd</EntryPoint>
      </ContainerHost>
    </EntryPoint>
  </CodePackage>

  <CodePackage Name="RunToCompletionCodePackage2" Version="1.0">
    <EntryPoint>
      <ContainerHost>
        <ImageName>mcr.microsoft.com/windows/nanoserver:1809</ImageName>
        <Commands>/v,/c,ping 127.0.0.1 &amp;&amp; set /a exitCode=%random% % 3 &amp;&amp; exit !exitCode!</Commands>
        <EntryPoint>cmd</EntryPoint>
      </ContainerHost>
    </EntryPoint>
  </CodePackage>
</ServiceManifest>

下面的 ApplicationManifest.xml 描述了一个基于上述 ServiceManifest.xml 的应用程序。The following ApplicationManifest.xml describes an application based on the ServiceManifest.xml discussed above. 它为 WindowsRunToCompletionServicePackage 指定了 RunToCompletion ExecutionPolicy,使用的重启策略为 OnFailureIt specifies RunToCompletion ExecutionPolicy for WindowsRunToCompletionServicePackage with a restart policy of OnFailure. 当激活 WindowsRunToCompletionServicePackage 时,构成它的那些 CodePackage 会启动。Upon activation of WindowsRunToCompletionServicePackage, its constituent CodePackages will be started. RunToCompletionCodePackage1 在首次激活时应该会成功退出。RunToCompletionCodePackage1 should exit successfully on the first activation. 但是,RunToCompletionCodePackage2 可能会失败 (退出代码为非零值) 。在这种情况下,它会重启,因为重启策略为 OnFailureHowever, RunToCompletionCodePackage2 can fail (non-zero exit code), in which case it will be restarted since the restart policy is OnFailure.

<?xml version="1.0" encoding="UTF-8"?>
<ApplicationManifest ApplicationTypeName="WindowsRunToCompletionApplicationType" ApplicationTypeVersion="1.0" xmlns="http://schemas.microsoft.com/2011/01/fabric" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <Description>Windows RunToCompletion Application</Description>

  <ServiceManifestImport>
    <ServiceManifestRef ServiceManifestName="WindowsRunToCompletionServicePackage" ServiceManifestVersion="1.0"/>
    <Policies>
      <ExecutionPolicy Type="RunToCompletion" Restart="OnFailure"/>
    </Policies>
  </ServiceManifestImport>

  <DefaultServices>
    <Service Name="WindowsRunToCompletionService" ServicePackageActivationMode="ExclusiveProcess">
      <StatelessService ServiceTypeName="WindowsRunToCompletionServiceType" InstanceCount="1">
        <SingletonPartition />
      </StatelessService>
    </Service>
  </DefaultServices>
</ApplicationManifest>

查询 DeployedServicePackage 的部署状态Querying deployment status of a DeployedServicePackage

可以通过 PowerShell 使用 Get-ServiceFabricDeployedServicePackage 或通过 C# 使用 FabricClient API GetDeployedServicePackageListAsync(String, Uri, String) 来查询 DeployedServicePackage 的部署状态Deployment status of a DeployedServicePackage can be queried from PowerShell using Get-ServiceFabricDeployedServicePackage or from C# using FabricClient API GetDeployedServicePackageListAsync(String, Uri, String)

使用 RunToCompletion 语义时的注意事项Considerations when using RunToCompletion semantics

对于当前的 RunToCompletion 支持,应注意以下几点。The following points should be noted for the current RunToCompletion support.

  • 只有容器应用程序和来宾可执行文件应用程序支持这些语义。These semantics are only supported for containers and guest executable applications.
  • 不允许对包含 RunToCompletion 语义的应用程序执行升级方案。Upgrade scenarios for applications with RunToCompletion semantics are not allowed. 必要时,用户应删除并重新创建此类应用程序。Users should delete and recreate such applications, if necessary.
  • 故障转移事件可能会导致 Codepackage 在成功完成后在同一节点上或群集的其他节点上重新执行。Failover events can cause CodePackages to re-execute after successful completion, on the same node, or other nodes of the cluster. 故障转移事件的示例包括:节点重启和节点上的 Service Fabric 运行时升级。Examples of failover events are, node restarts and Service Fabric runtime upgrades on a node.

后续步骤Next steps

请参阅以下文章,了解相关信息。See the following articles for related information.