Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugger Client in core #804

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ otel = ["dep:opentelemetry", "dep:opentelemetry_sdk", "dep:opentelemetry-otlp",
"dep:opentelemetry-prometheus", "dep:hyper", "dep:hyper-util", "dep:http-body-util"]
tokio-console = ["console-subscriber"]
ephemeral-server = ["dep:flate2", "dep:reqwest", "dep:tar", "dep:zip"]
debug-plugin = ["dep:reqwest"]

[dependencies]
anyhow = "1.0"
Expand Down
71 changes: 71 additions & 0 deletions core/src/debug_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//! This module contains code for an http client that is used for the VSCode debug plugin.

use prost::Message;
use reqwest;
use std::time::Duration;
use temporal_sdk_core_protos::temporal::api::history::v1::History;

const CLIENT_NAME: &str = "temporal-core";
const CLIENT_VERSION: &str = "0.1.0";

/// Struct representing the internal debug client
twin-drill marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Clone)]
pub struct DebugClient {
/// URL for the local instance of the debugger server
debugger_url: String,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be an actual Url type which we already depend on https://docs.rs/url/latest/url/struct.Url.html (which also has safe ways to construct paths like adding /history)


/// reqwest::Client to avoid re-creating a client for every request.
twin-drill marked this conversation as resolved.
Show resolved Hide resolved
client: reqwest::Client,
}

impl DebugClient {
/// Create a new instance of a DebugClient with the specified url.
pub fn new(url: String) -> DebugClient {
DebugClient {
debugger_url: url,
client: reqwest::Client::new(),
}
}

/// Get the history from the instance of the debug plugin server, then return the protobinary if successful.
twin-drill marked this conversation as resolved.
Show resolved Hide resolved
pub async fn get_history(&self) -> Result<History, anyhow::Error> {
let url = self.debugger_url.clone() + "/history";
let resp = self
.client
.get(url)
.header("Temporal-Client-Name", CLIENT_NAME)
.header("Temporal-Client-Version", CLIENT_VERSION)
.send()
.await?;

let bytes = resp.bytes().await?;
Ok(History::decode(bytes)?) // decode_length_delimited() does not work
twin-drill marked this conversation as resolved.
Show resolved Hide resolved
}

/// Post to current-wft-started to communicate with debug plugin server
pub async fn post_wft_started(
&self,
event_id: &i64,
) -> Result<reqwest::Response, anyhow::Error> {
let url = self.debugger_url.clone() + "/current-wft-started";
Ok(self
.client
.get(url)
.header("Temporal-Client-Name", CLIENT_NAME)
.header("Temporal-Client-Version", CLIENT_VERSION)
.timeout(Duration::from_secs(5))
.json(event_id)
.send()
.await?)
}
}

#[cfg(test)]
mod tests {
// this test wont work without a local instance of the debugger running.
// #[tokio::test]
// async fn test_debug_client_delete() {
// let dc = DebugClient::new("http://127.0.0.1:51563".into());
// dc.get_history().await.unwrap();
// }
twin-drill marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 2 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ extern crate tracing;
extern crate core;

mod abstractions;
#[cfg(feature = "debug-plugin")]
pub mod debug_client;
#[cfg(feature = "ephemeral-server")]
pub mod ephemeral_server;
mod internal_flags;
Expand Down
Loading