執行元件

本文件將說明如何在開發期間直接將元件新增至元件例項樹狀結構,並在執行階段與這些元件互動。

Fuchsia 在如果您使用下列任一架構建構元件,請改為參閱對應的指南:

概念

請先瞭解下列概念,再執行元件:

如要進一步瞭解元件執行作業,請參閱「元件生命週期」一文。

元件執行個體

執行元件的首要步驟,就是在樹狀結構中新增元件執行個體。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 元件 destroy/create/start 銷毀,然後啟動
ffx 元件 run --recreate 銷毀,然後啟動
ffx 元件 停止/啟動 停止和啟動,但不會銷毀
  • 「更新套件」表示在重新載入套件時會更新程式碼。
  • 「更新資訊清單」表示重新載入資訊清單快取,更新 FIDL 檔案中包含的路由和其他資訊。
  • 「保留資源」是指儲存空間等資源會由 destroy 指令釋放,但會改為保留。

重新載入

更新元件的程式碼和資訊清單,同時保留資源:

ffx component reload TARGET_MONIKER

這個指令會先關閉元件,然後重新載入並重新啟動。這項指令會更新套件和資訊清單,但不會摧毀元件或釋出資源。

reload 指令會保留元件的資源,例如儲存空間。如果在特定狀態下,初始化、取得或重新建立資源以進行偵錯的速度較慢,這項保存功能就很實用。

當銷毀元件需要耗費大量資源時 (例如需要關閉及重新啟動工作階段或目標裝置/模擬器),重新載入的速度也會更快。

刪除/建立/啟動

如要完全重新載入元件並放棄已取得的資源,您可以先刪除現有的元件執行個體,然後重新啟動。您可以使用下列功能:

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

這個序列會重新載入套件和資訊清單,因此程式碼和能力變更會更新。不過,銷毀元件也會釋放其使用的任何資源。如果您希望從完全重新啟動的元件開始,這項完整重設功能可能會是您想要的。

執行 --recreate

與 destroy/create/start 序列類似的方便指令是搭配 --recreate 使用的 run 指令:

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-laboratorytransient 集合。這個集合中的元件例項會在停止後繼續保留。如要摧毀這個集合中的元件執行個體,請使用 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...