运行组件

本文档演示了如何在开发期间将组件直接添加到组件实例树中,并在运行时与这些组件进行交互。

如果您使用以下框架之一构建组件,请改为参阅相应的指南:

概念

在运行组件之前,您应该了解以下概念:

  • 组件实例会经历四种主要的生命周期状态:创建、启动、停止和销毁。
  • - - 0. - static 每个实例都包含组件 nameurl
  • 组件网址由组件框架解析,通常解析为软件包内的资源。

如需详细了解组件的执行,请参阅组件生命周期

组件实例

运行组件的第一步是向树中添加新的组件实例。 capabilities

探索静态组件

静态组件在树中声明为另一个组件实例的子项。您可以使用 ffx component show 确定静态组件实例的名称和组件网址:

ffx component show COMPONENT_NAME

COMPONENT_NAME 替换为组件的名称。以下示例展示了 pkg-resolver 组件的命令输出:

$ ffx component show pkg-resolver
               Moniker: /core/pkg-resolver
                   URL: fuchsia-pkg://fuchsia.com/pkg-resolver#meta/pkg-resolver.cm
                  Type: CML static component
       Component State: Resolved
       Execution State: Running
...

无法在运行时创建或销毁静态组件实例。

管理动态组件

动态组件是在运行时在集合内创建的。您可以使用 ffx component create 创建新的组件实例,在现有集合中提供目标名称,并提供用于解析组件的组件网址:

ffx component create TARGET_MONIKER COMPONENT_URL

TARGET_MONIKER 替换为现有集合内新组件的目标名称,并将 COMPONENT_URL 替换为提供组件的位置。例如,以下命令会在 ffx-laboratory 集合内创建一个名为 hello-world 的新组件实例:

$ ffx component create /core/ffx-laboratory:hello-world fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cm
URL: fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cm
Moniker: /core/ffx-laboratory:hello-world
Creating component instance...

同样,使用 ffx component destroy 提供相应名称来销毁动态组件实例:

ffx component destroy TARGET_MONIKER

TARGET_MONIKER 替换为要销毁的组件的名称。以下示例会销毁上面创建的 hello-world 组件:

$ ffx component destroy /core/ffx-laboratory:hello-world
Moniker: /core/ffx-laboratory:hello-world
Destroying component instance...

组件执行

一旦组件实例存在于树中,您就可以使用 ffx component 启动和停止目标实例。

启动实例

使用 ffx component start 明确启动组件实例:

ffx component start TARGET_MONIKER

TARGET_MONIKER 替换为要启动的组件的名称。以下示例会启动之前创建的 hello-world 组件:

$ ffx component start /core/ffx-laboratory:hello-world
Moniker: /core/ffx-laboratory:hello-world
Starting component instance...

停止实例

使用 ffx component stop 通过名称终止正在运行的组件实例的执行:

ffx component stop TARGET_MONIKER

TARGET_MONIKER 替换为要停止的组件的名称。以下示例会停止到上面启动的 hello-world 组件:

$ ffx component stop /core/ffx-laboratory:hello-world
Moniker: /core/ffx-laboratory:hello-world
Stopping component instance...

运行组件

ffx component run 命令提供了有关在开发过程中运行基本组件的快速入门。这是 ffx component create 后跟 ffx component start 的快捷方式:

ffx component run TARGET_MONIKER COMPONENT_URL

TARGET_MONIKER 替换为现有集合内新组件的目标名称,并将 COMPONENT_URL 替换为提供组件的位置。例如,以下命令会在 ffx-laboratory 集合内创建一个名为 hello-world-rust 的新组件实例:

$ ffx component run /core/ffx-laboratory:hello-world-rust fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cm
URL: fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cm
Moniker: /core/ffx-laboratory:hello-world-rust
Creating component instance...
Starting component instance...

上面的示例相当于运行以下各个 ffx 命令:

$ ffx component create /core/ffx-laboratory:hello-world-rust fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cm
$ ffx component start /core/ffx-laboratory:hello-world-rust

更新组件的方法

当您更改组件时,通常需要更新在设备上运行的该组件的其中一个实例。例如,您可以更改组件的二进制文件,然后重启组件以使用新二进制文件运行。或者,您也可以更改其清单以添加新的功能路由,并希望使这些功能路由在设备上可用。

ffx component reload 命令是最快速、最完整的组件重新加载方法。但这并不是唯一方法,了解其他方法可以为特殊用例实现更精确的操作。

下文对这些方法进行了总结并进行了详细说明。

摘要

命令 说明 更新软件包 更新清单 保留资源
ffx 组件重新加载 包括停止、更新和启动
ffx 组件 销毁/创建/启动 销毁,然后启动
ffx 组件运行 - 重新创建 销毁,然后启动
ffx 组件 停止/开始 并且启动和停止 不会销毁
  • “更新软件包”是指在重新加载软件包时更新代码。
  • “更新清单”表示重新加载清单缓存,更新 FIDL 文件中包含的路由和其他信息。
  • “保留资源”意味着将由销毁命令释放的资源(如存储空间)会被保留。

重新加载

使用以下命令更新组件的代码和清单,同时保留资源:

ffx component reload TARGET_MONIKER

此命令会先关闭组件,然后重新加载并重启。该命令会更新软件包和清单,而不会销毁组件或释放资源。

reload 命令可保留组件的资源,例如存储空间。如果为了进行调试而初始化、获取或重新创建处于特定状态的资源的速度很慢,则这种保留非常有用。

如果销毁成本高昂(例如,当需要关闭并重启会话或目标设备/模拟器时),重新加载速度也会更快。

销毁/创建/启动

如需完全重新加载组件并舍弃获取的资源,您可以先销毁现有的组件实例,然后再重启。使用:

$ ffx component destroy TARGET_MONIKER
$ ffx component create TARGET_MONIKER COMPONENT_URL
$ ffx component start TARGET_MONIKER

此序列将重新加载软件包和清单,因此代码和功能更改也会随之更新。不过,销毁组件也会释放其使用的所有资源。如果您的目标是从完全重新初始化的组件开始,则可能需要完全重置。

运行 --recreate

一个类似于销毁/创建/启动序列的便捷命令是包含 --recreaterun 命令:

ffx component run TARGET_MONIKER COMPONENT_URL --recreate

停止/开始

虽然它主要不是执行更新的方法,但其副作用是停止组件,然后再启动组件,则只会对组件进行部分更新。

$ ffx component stop TARGET_MONIKER
$ ffx component start TARGET_MONIKER

假设 ffx serve 等软件包管理器正在运行,将加载并运行组件代码的最新版本。但是,由于 Fuchsia 组件框架中缓存的工作方式,清单不会更新。该清单包含组件的路由信息以及 *.cm 文件中定义的其他信息。因此,如果您更改组件的功能路由,停止和启动组件将不会获取这些更改。

ffx-laboratory

ffx-laboratory 是一个组件集合,用于为开发提供一组受限的功能。此集合中的组件提供了以下功能:

ffx-laboratory 是一个 transient 集合。此集合中的组件实例即使在停止后也将保留。如需销毁此集合中的组件实例,请使用 ffx component destroy 命令。

问题排查

本部分介绍您在开发期间运行组件时可能会遇到的常见问题。

无法解析该组件

使用 ffx component startffx component run 时,如果组件框架无法解析组件实例,您可能会遇到以下错误:

$ ffx component run /core/ffx-laboratory:hello-world fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm
URL: fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm
Moniker: /core/ffx-laboratory:hello-world
Creating component instance...
Starting component instance...
Lifecycle protocol could not bind to component instance: InstanceCannotResolve

当组件网址无法解析为有效的组件清单时,就会发生这种情况。

如需解决此问题,请验证以下内容:

组件实例已存在

使用 ffx component createffx component run 时,如果组件实例已存在,您可能会遇到以下错误:

$ ffx component run /core/ffx-laboratory:hello-world fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm
URL: fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm
Moniker: /core/ffx-laboratory:hello-world
Creating component instance...
Component instance already exists. Use --recreate to destroy and recreate a new instance, or --name to create a new instance with a different name.

当目标名称已被其他组件实例使用时,就会发生这种情况。

如需解决此问题,请使用 ffx component destroy 命令手动销毁该实例:

$ ffx component destroy /core/ffx-laboratory:hello-world
Moniker: /core/ffx-laboratory:hello-world
Destroying component instance...

如果您使用的是 ffx component run,请添加 --recreate 标志以销毁并重新创建实例:

$ ffx component run /core/ffx-laboratory:hello-world fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm --recreate
URL: fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm
Moniker: /core/ffx-laboratory:hello-world
Creating component instance...
Component instance already exists. Destroying...
Recreating component instance...
Starting component instance...

或者,添加 --name 标志以创建一个具有不同名称的新实例:

$ ffx component run /core/ffx-laboratory:hello-world fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm --name hello-world-2
URL: fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm
Moniker: /core/ffx-laboratory:hello-world-2
Creating component instance...
Starting component instance...