Skip to content

Commit

Permalink
Rename agent_id to agent_name
Browse files Browse the repository at this point in the history
  • Loading branch information
ThetaSinner committed Jun 17, 2024
1 parent 306902c commit 8023546
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ Before you can run this, you'll need to provide the agent behaviour hook. Add th
fn agent_behaviour(
ctx: &mut AgentContext<HolochainRunnerContext, HolochainAgentContext>,
) -> HookResult {
println!("Hello from, {}", ctx.agent_id());
println!("Hello from, {}", ctx.agent_name());
std::thread::sleep(std::time::Duration::from_secs(1));
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions bindings/runner/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ where
{
let admin_ws_url = ctx.runner_context().get_connection_string().to_string();
let app_ws_url = ctx.runner_context().get().app_ws_url();
let agent_id = ctx.agent_id().to_string();
let agent_name = ctx.agent_name().to_string();
let reporter = ctx.runner_context().reporter();

let (installed_app_id, cell_id, app_client) = ctx
Expand All @@ -157,7 +157,7 @@ where
.map_err(handle_api_err)?;
log::debug!("Generated agent pub key: {:}", key);

let installed_app_id = format!("{}-app", agent_id).to_string();
let installed_app_id = format!("{}-app", agent_name).to_string();
let app_info = client
.install_app(InstallAppPayload {
source: AppBundleSource::Path(app_path),
Expand Down
26 changes: 13 additions & 13 deletions bindings/trycp_runner/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,22 @@ where
{
let run_id = ctx.runner_context().get_run_id().to_string();
let client = ctx.get().trycp_client();
let agent_id = ctx.agent_id().to_string();
let agent_name = ctx.agent_name().to_string();

let (app_port, cell_id, credentials) = ctx
.runner_context()
.executor()
.execute_in_place(async move {
let agent_key = client
.generate_agent_pub_key(agent_id.clone(), None)
.generate_agent_pub_key(agent_name.clone(), None)
.await?;

let content = std::fs::read(app_path)?;

let installed_app_id = format!("{}-app", agent_id).to_string();
let installed_app_id = format!("{}-app", agent_name).to_string();
let app_info = client
.install_app(
agent_id.clone(),
agent_name.clone(),
InstallAppPayload {
source: AppBundleSource::Bundle(AppBundle::decode(&content)?),
agent_key,
Expand All @@ -136,7 +136,7 @@ where
.await?;

let enable_result = client
.enable_app(agent_id.clone(), app_info.installed_app_id.clone(), None)
.enable_app(agent_name.clone(), app_info.installed_app_id.clone(), None)
.await?;
if !enable_result.errors.is_empty() {
return Err(anyhow::anyhow!(
Expand All @@ -146,12 +146,12 @@ where
}

let app_port = client
.attach_app_interface(agent_id.clone(), None, AllowedOrigins::Any, None, None)
.attach_app_interface(agent_name.clone(), None, AllowedOrigins::Any, None, None)
.await?;

let issued = client
.issue_app_auth_token(
agent_id.clone(),
agent_name.clone(),
IssueAppAuthenticationTokenPayload {
installed_app_id,
expiry_seconds: 30,
Expand Down Expand Up @@ -179,7 +179,7 @@ where

let credentials = client
.authorize_signing_credentials(
agent_id.clone(),
agent_name.clone(),
AuthorizeSigningCredentialsPayload {
cell_id: cell_id.clone(),
functions: None, // Equivalent to all functions
Expand Down Expand Up @@ -237,7 +237,7 @@ where
static MIN_PEERS: OnceLock<usize> = OnceLock::new();

let client = ctx.get().trycp_client();
let agent_id = ctx.agent_id().to_string();
let agent_name = ctx.agent_name().to_string();

let min_peers = *MIN_PEERS.get_or_init(|| {
std::env::var("MIN_PEERS")
Expand All @@ -250,7 +250,7 @@ where
.execute_in_place(async move {
let start_discovery = Instant::now();
for _ in 0..wait_for.as_secs() {
let agent_list = client.agent_info(agent_id.clone(), None, None).await?;
let agent_list = client.agent_info(agent_name.clone(), None, None).await?;

if agent_list.len() >= min_peers {
break;
Expand All @@ -261,7 +261,7 @@ where

println!(
"Discovery for agent {} took: {}s",
agent_id,
agent_name,
start_discovery.elapsed().as_secs()
);

Expand Down Expand Up @@ -291,12 +291,12 @@ where
SV: UserValuesConstraint,
{
let client = ctx.get().trycp_client();
let agent_id = ctx.agent_id().to_string();
let agent_name = ctx.agent_name().to_string();

ctx.runner_context()
.executor()
.execute_in_place(async move {
client.shutdown(agent_id.clone(), None, None).await?;
client.shutdown(agent_name.clone(), None, None).await?;
Ok(())
})?;

Expand Down
14 changes: 7 additions & 7 deletions framework/runner/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl<RV: UserValuesConstraint> RunnerContext<RV> {
#[derive(Debug)]
pub struct AgentContext<RV: UserValuesConstraint, V: UserValuesConstraint> {
agent_index: usize,
agent_id: String,
agent_name: String,
runner_context: Arc<RunnerContext<RV>>,
shutdown_listener: DelegatedShutdownListener,
value: V,
Expand All @@ -111,13 +111,13 @@ pub struct AgentContext<RV: UserValuesConstraint, V: UserValuesConstraint> {
impl<RV: UserValuesConstraint, V: UserValuesConstraint> AgentContext<RV, V> {
pub(crate) fn new(
agent_index: usize,
agent_id: String,
agent_name: String,
runner_context: Arc<RunnerContext<RV>>,
shutdown_listener: DelegatedShutdownListener,
) -> Self {
Self {
agent_index,
agent_id,
agent_name,
runner_context,
shutdown_listener,
value: Default::default(),
Expand All @@ -129,11 +129,11 @@ impl<RV: UserValuesConstraint, V: UserValuesConstraint> AgentContext<RV, V> {
self.agent_index
}

/// A value generated by the runner that you can use to identify yourself making requests.
/// A value generated by the runner that you can use to identify yourself when making requests.
///
/// This value is unique within the runner but it is *not* unique across multiple runners.
pub fn agent_id(&self) -> &str {
&self.agent_id
/// This value is unique within the runner but *not* unique across multiple runners.
pub fn agent_name(&self) -> &str {
&self.agent_name
}

/// A handle to the runner context for the scenario.
Expand Down
16 changes: 8 additions & 8 deletions framework/runner/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,22 @@ pub fn run<RV: UserValuesConstraint, V: UserValuesConstraint>(
// For the behaviour implementation to listen for shutdown and respond appropriately
let delegated_shutdown_listener = shutdown_handle.new_listener();

let agent_id = format!("agent-{}", agent_index);
let agent_name = format!("agent-{}", agent_index);

handles.push(
std::thread::Builder::new()
.name(agent_id.clone())
.name(agent_name.clone())
.spawn(move || {
// TODO synchronize these setups so that the scenario waits for all of them to complete before proceeding.
let mut context = AgentContext::new(
agent_index,
agent_id.clone(),
agent_name.clone(),
runner_context,
delegated_shutdown_listener,
);
if let Some(setup_agent_fn) = setup_agent_fn {
if let Err(e) = setup_agent_fn(&mut context) {
log::error!("Agent setup failed for agent {}: {:?}", agent_id, e);
log::error!("Agent setup failed for agent {}: {:?}", agent_name, e);

// Attempt to run the shutdown hook if the agent setup was cancelled.
if e.is::<ShutdownSignalError>() {
Expand All @@ -139,7 +139,7 @@ pub fn run<RV: UserValuesConstraint, V: UserValuesConstraint>(
if let Err(e) = teardown_agent_fn(&mut context) {
log::error!(
"Agent teardown failed for agent {}: {:?}",
agent_id,
agent_name,
e
);
}
Expand All @@ -155,7 +155,7 @@ pub fn run<RV: UserValuesConstraint, V: UserValuesConstraint>(
if let Some(behaviour) = agent_behaviour_fn {
loop {
if cycle_shutdown_receiver.should_shutdown() {
log::debug!("Stopping agent {}", agent_id);
log::debug!("Stopping agent {}", agent_name);
break;
}

Expand All @@ -168,7 +168,7 @@ pub fn run<RV: UserValuesConstraint, V: UserValuesConstraint>(
Err(e) if e.is::<AgentBailError>() => {
// A single agent has failed, we don't want to stop the whole
// scenario so warn and exit the loop.
log::warn!("Agent {} bailed: {:?}", agent_id, e);
log::warn!("Agent {} bailed: {:?}", agent_name, e);
behaviour_ran_to_complete = false;
break;
}
Expand All @@ -181,7 +181,7 @@ pub fn run<RV: UserValuesConstraint, V: UserValuesConstraint>(

if let Some(teardown_agent_fn) = teardown_agent_fn {
if let Err(e) = teardown_agent_fn(&mut context) {
log::error!("Agent teardown failed for agent {}: {:?}", agent_id, e);
log::error!("Agent teardown failed for agent {}: {:?}", agent_name, e);
}
}

Expand Down
10 changes: 5 additions & 5 deletions scenarios/remote_call_rate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ fn agent_setup(
reset_trycp_remote(ctx)?;

let client = ctx.get().trycp_client();
let agent_id = ctx.agent_id().to_string();
let agent_name = ctx.agent_name().to_string();

ctx.runner_context()
.executor()
.execute_in_place(async move {
client
.configure_player(agent_id.clone(), CONDUCTOR_CONFIG.to_string(), None)
.configure_player(agent_name.clone(), CONDUCTOR_CONFIG.to_string(), None)
.await?;

client
.startup(agent_id.clone(), Some("warn".to_string()), None)
.startup(agent_name.clone(), Some("warn".to_string()), None)
.await?;

Ok(())
Expand All @@ -53,7 +53,7 @@ fn agent_behaviour(
) -> HookResult {
let client = ctx.get().trycp_client();

let agent_id = ctx.agent_id().to_string();
let agent_name = ctx.agent_name().to_string();
let app_port = ctx.get().app_port();
let cell_id = ctx.get().cell_id();
let next_remote_call_peer = ctx.get_mut().scenario_values.remote_call_peers.pop();
Expand All @@ -68,7 +68,7 @@ fn agent_behaviour(
// No more agents available to call, get a new list.
// This is also the initial condition.
let mut new_peer_list = client
.agent_info(agent_id, None, None)
.agent_info(agent_name, None, None)
.await?
.into_iter()
.map(|info| AgentPubKey::from_raw_36(info.agent.0.clone()))
Expand Down

0 comments on commit 8023546

Please sign in to comment.