Skip to content

Commit

Permalink
feat: allow the start graph action to return a failure error (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn authored Jan 8, 2025
1 parent 204a11e commit 79c5c55
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typedef struct ten_extension_t ten_extension_t;

typedef struct ten_addon_create_extension_done_ctx_t {
ten_list_t *results;
ten_string_t extension_name;
ten_error_t err;
} ten_addon_create_extension_done_ctx_t;

Expand All @@ -28,7 +29,8 @@ TEN_RUNTIME_PRIVATE_API ten_addon_store_t *ten_extension_get_global_store(void);
TEN_RUNTIME_PRIVATE_API void ten_addon_unregister_all_extension(void);

TEN_RUNTIME_PRIVATE_API ten_addon_create_extension_done_ctx_t *
ten_addon_create_extension_done_ctx_create(ten_list_t *results);
ten_addon_create_extension_done_ctx_create(ten_list_t *results,
const char *extension_name);

TEN_RUNTIME_PRIVATE_API void ten_addon_create_extension_done_ctx_destroy(
ten_addon_create_extension_done_ctx_t *self);
Expand Down
6 changes: 5 additions & 1 deletion core/src/ten_runtime/addon/extension/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,15 @@ void ten_addon_on_create_extension_instance_ctx_destroy(
}

ten_addon_create_extension_done_ctx_t *
ten_addon_create_extension_done_ctx_create(ten_list_t *results) {
ten_addon_create_extension_done_ctx_create(ten_list_t *results,
const char *extension_name) {
ten_addon_create_extension_done_ctx_t *self =
TEN_MALLOC(sizeof(ten_addon_create_extension_done_ctx_t));
TEN_ASSERT(self, "Failed to allocate memory.");

self->results = results;
ten_string_init_from_c_str(&self->extension_name, extension_name,
strlen(extension_name));
ten_error_init(&self->err);

return self;
Expand All @@ -80,6 +83,7 @@ void ten_addon_create_extension_done_ctx_destroy(
ten_addon_create_extension_done_ctx_t *self) {
TEN_ASSERT(self, "Should not happen.");

ten_string_deinit(&self->extension_name);
ten_error_deinit(&self->err);

TEN_FREE(self);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "ten_runtime/ten_env/internal/return.h"
#include "ten_runtime/ten_env/ten_env.h"
#include "ten_utils/container/list_ptr.h"
#include "ten_utils/lib/string.h"
#include "ten_utils/log/log.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/macro/mark.h"
Expand All @@ -50,6 +51,10 @@ static void on_addon_create_extension_done(ten_env_t *ten_env,
ten_env_get_attach_to(ten_env) == TEN_ENV_ATTACH_TO_EXTENSION_GROUP,
"Invalid argument.");

ten_addon_create_extension_done_ctx_t *ctx =
(ten_addon_create_extension_done_ctx_t *)cb_data;
TEN_ASSERT(ctx, "Should not happen.");

ten_extension_group_t *extension_group = ten_env_get_attached_target(ten_env);
TEN_ASSERT(extension_group &&
ten_extension_group_check_integrity(extension_group, true),
Expand All @@ -61,15 +66,12 @@ static void on_addon_create_extension_done(ten_env_t *ten_env,
TEN_ASSERT(ten_extension_check_integrity(extension, true),
"Invalid argument.");

ten_addon_create_extension_done_ctx_t *ctx =
(ten_addon_create_extension_done_ctx_t *)cb_data;
TEN_ASSERT(ctx, "Should not happen.");
TEN_LOGI("Success to create extension %s",
ten_string_get_raw_str(&ctx->extension_name));

ten_list_t *results = ctx->results;
TEN_ASSERT(results, "Should not happen.");

ten_addon_create_extension_done_ctx_destroy(ctx);

ten_list_push_ptr_back(results, extension, NULL);

if (ten_list_size(results) ==
Expand All @@ -86,6 +88,9 @@ static void on_addon_create_extension_done(ten_env_t *ten_env,
} else {
// Failed to create the extension, failure path.

TEN_LOGI("Failed to create extension %s",
ten_string_get_raw_str(&ctx->extension_name));

ten_extension_context_t *extension_context =
extension_group->extension_context;
TEN_ASSERT(extension_context && ten_extension_context_check_integrity(
Expand Down Expand Up @@ -114,6 +119,8 @@ static void on_addon_create_extension_done(ten_env_t *ten_env,
NULL);
ten_env_send_cmd(ten_env, stop_graph_cmd, NULL, NULL, NULL);
}

ten_addon_create_extension_done_ctx_destroy(ctx);
}

static void on_addon_destroy_instance_done(ten_env_t *ten_env,
Expand Down Expand Up @@ -166,6 +173,11 @@ static void ten_builtin_extension_group_on_create_extensions(
ten_env_on_create_extensions_done(ten_env, results, NULL);
ten_list_destroy(results);

TEN_LOGI(
"%s is a group without any extensions, so it is considered that all "
"extensions have been successfully created.",
ten_string_get_raw_str(&self->name));

// This extension group is empty, so it can be considered that all the
// required extensions have been successfully created.
return;
Expand All @@ -184,7 +196,8 @@ static void ten_builtin_extension_group_on_create_extensions(
ten_string_t *extension_instance_name = &extension_name_info->instance_name;

ten_addon_create_extension_done_ctx_t *ctx =
ten_addon_create_extension_done_ctx_create(results);
ten_addon_create_extension_done_ctx_create(
results, ten_string_get_raw_str(extension_instance_name));

bool res = ten_addon_create_extension(
ten_env, ten_string_get_raw_str(extension_addon_name),
Expand Down

0 comments on commit 79c5c55

Please sign in to comment.