From 8d93fddb91df722b59ad852871e75ea235136a33 Mon Sep 17 00:00:00 2001 From: Sean Lynch <42618346+swlynch99@users.noreply.github.com> Date: Fri, 6 Sep 2024 13:43:55 -0700 Subject: [PATCH 1/2] feat(cli): Add new `durable events` command This prints the events that have been emitted by the task, in order. --- Cargo.lock | 130 ++++++++++++++++++++++++------- crates/durable-cli/Cargo.toml | 3 +- crates/durable-cli/src/events.rs | 70 +++++++++++++++++ crates/durable-cli/src/main.rs | 3 + 4 files changed, 178 insertions(+), 28 deletions(-) create mode 100644 crates/durable-cli/src/events.rs diff --git a/Cargo.lock b/Cargo.lock index b9aff0c..cfcba5f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -158,7 +158,7 @@ checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -169,7 +169,7 @@ checksum = "a27b8a3a6e1a44fa4c8baf1f653e4172e81486d4941f2237e20dc2d0cf4ddff1" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -326,6 +326,12 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +[[package]] +name = "bytecount" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ce89b21cab1437276d2650d57e971f9d548a2d9037cc231abdc0562b97498ce" + [[package]] name = "byteorder" version = "1.5.0" @@ -445,7 +451,7 @@ dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -774,7 +780,7 @@ dependencies = [ "proc-macro2", "quote", "strsim", - "syn", + "syn 2.0.77", ] [[package]] @@ -785,7 +791,7 @@ checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" dependencies = [ "darling_core", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -817,7 +823,7 @@ dependencies = [ "darling", "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -882,7 +888,7 @@ version = "0.1.2" dependencies = [ "anyhow", "prettyplease", - "syn", + "syn 2.0.77", "wit-bindgen-core", "wit-bindgen-rust", ] @@ -899,6 +905,7 @@ dependencies = [ "serde", "serde_json", "sqlx", + "tabled", "tokio", "tracing-subscriber", ] @@ -1039,7 +1046,7 @@ dependencies = [ "proc-macro2", "quote", "sqlx-macros-core", - "syn", + "syn 2.0.77", ] [[package]] @@ -1337,7 +1344,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -2163,7 +2170,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -2190,6 +2197,17 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" +[[package]] +name = "papergrid" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7419ad52a7de9b60d33e11085a0fe3df1fbd5926aa3f93d3dd53afbc9e86725" +dependencies = [ + "bytecount", + "fnv", + "unicode-width", +] + [[package]] name = "parking" version = "2.2.0" @@ -2257,7 +2275,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -2327,7 +2345,31 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "479cf940fbbb3426c32c5d5176f62ad57549a0bb84773423ba8be9d089f5faba" dependencies = [ "proc-macro2", - "syn", + "syn 2.0.77", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn 1.0.109", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", ] [[package]] @@ -2359,7 +2401,7 @@ dependencies = [ "itertools 0.13.0", "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -2742,7 +2784,7 @@ checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -2980,7 +3022,7 @@ dependencies = [ "quote", "sqlx-core", "sqlx-macros-core", - "syn", + "syn 2.0.77", ] [[package]] @@ -3003,7 +3045,7 @@ dependencies = [ "sqlx-mysql", "sqlx-postgres", "sqlx-sqlite", - "syn", + "syn 2.0.77", "tempfile", "tokio", "url", @@ -3148,6 +3190,17 @@ version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.77" @@ -3195,6 +3248,29 @@ dependencies = [ "libc", ] +[[package]] +name = "tabled" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c9303ee60b9bedf722012ea29ae3711ba13a67c9b9ae28993838b63057cb1b" +dependencies = [ + "papergrid", + "tabled_derive", +] + +[[package]] +name = "tabled_derive" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf0fb8bfdc709786c154e24a66777493fb63ae97e3036d914c8666774c477069" +dependencies = [ + "heck 0.4.1", + "proc-macro-error", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "tap" version = "1.0.1" @@ -3246,7 +3322,7 @@ checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -3301,7 +3377,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -3465,7 +3541,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -3557,9 +3633,9 @@ checksum = "52ea75f83c0137a9b98608359a5f1af8144876eb67bcb1ce837368e906a9f524" [[package]] name = "unicode-width" -version = "0.1.13" +version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d" +checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" [[package]] name = "unicode-xid" @@ -3673,7 +3749,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn", + "syn 2.0.77", "wasm-bindgen-shared", ] @@ -3707,7 +3783,7 @@ checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -3886,7 +3962,7 @@ dependencies = [ "anyhow", "proc-macro2", "quote", - "syn", + "syn 2.0.77", "wasmtime-component-util", "wasmtime-wit-bindgen", "wit-parser", @@ -4016,7 +4092,7 @@ checksum = "3850e3511d6c7f11a72d571890b0ed5f6204681f7f050b9de2690e7f13123fed" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -4376,7 +4452,7 @@ dependencies = [ "heck 0.5.0", "indexmap 2.5.0", "prettyplease", - "syn", + "syn 2.0.77", "wasm-metadata", "wit-bindgen-core", "wit-component", @@ -4483,7 +4559,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] diff --git a/crates/durable-cli/Cargo.toml b/crates/durable-cli/Cargo.toml index 8fc4d14..268f5bc 100644 --- a/crates/durable-cli/Cargo.toml +++ b/crates/durable-cli/Cargo.toml @@ -18,11 +18,12 @@ durable-client = { workspace = true } anyhow = "1.0.86" clap = { version = "4.5.11", features = ["env", "derive"] } futures-util = "0.3.30" +log = "0.4.22" serde = "1.0.204" serde_json = { version = "1.0.120", features = ["raw_value"] } +tabled = "0.16.0" tokio = { version = "1.39.1", features = ["full"] } tracing-subscriber = { version = "0.3.18", features = ["fmt", "env-filter"] } -log = "0.4.22" [dependencies.sqlx] version = "0.8.0" diff --git a/crates/durable-cli/src/events.rs b/crates/durable-cli/src/events.rs new file mode 100644 index 0000000..9034777 --- /dev/null +++ b/crates/durable-cli/src/events.rs @@ -0,0 +1,70 @@ +use sqlx::postgres::PgListener; +use tabled::settings::formatting::AlignmentStrategy; +use tabled::settings::object::Segment; +use tabled::settings::{Alignment, Margin, Modify, Padding, Style}; +use tabled::{Table, Tabled}; + +use crate::CommonOptions; + +/// Print events that have been emitted by a task. +#[derive(Debug, clap::Parser)] +pub struct Events { + /// The id of the task we want to see the logs for. + pub task: i64, +} + +#[derive(Tabled)] +struct Event { + index: i64, + label: String, + value: String, +} + +impl Events { + pub async fn run(self, options: &CommonOptions) -> anyhow::Result<()> { + let pool = options.pool().await?; + + let mut listener = PgListener::connect_with(&pool).await?; + listener.listen("durable:task-complete").await?; + + let exists = sqlx::query_scalar!("SELECT id FROM durable.task WHERE id = $1", self.task) + .fetch_optional(&pool) + .await? + .is_some(); + + if !exists { + anyhow::bail!("unable to find task with id {}", self.task); + } + + let events = sqlx::query_as!( + Event, + r#" + SELECT + index, + label, + value::text as "value!" + FROM durable.event + WHERE task_id = $1 + ORDER BY index ASC + "#, + self.task, + ) + .fetch_all(&mut listener) + .await?; + + let mut table = Table::new(events); + table + .with( + Modify::new(Segment::all()) + .with(Alignment::left()) + .with(AlignmentStrategy::PerLine), + ) + .with(Style::blank()) + .with(Margin::new(0, 0, 0, 0)) + .with(Padding::new(0, 0, 0, 0)); + + println!("{table}"); + + Ok(()) + } +} diff --git a/crates/durable-cli/src/main.rs b/crates/durable-cli/src/main.rs index 138db7b..4aebf4f 100644 --- a/crates/durable-cli/src/main.rs +++ b/crates/durable-cli/src/main.rs @@ -6,6 +6,7 @@ use sqlx::ConnectOptions; use tokio::sync::OnceCell; use tracing_subscriber::prelude::*; +mod events; mod launch; mod logs; mod notify; @@ -23,6 +24,7 @@ struct Args { enum Commands { Launch(self::launch::Launch), Logs(self::logs::Logs), + Events(self::events::Events), Notify(self::notify::Notify), } @@ -38,6 +40,7 @@ async fn main() -> anyhow::Result<()> { match args.command { Commands::Launch(cmd) => cmd.run(&args.common).await, Commands::Logs(cmd) => cmd.run(&args.common).await, + Commands::Events(cmd) => cmd.run(&args.common).await, Commands::Notify(cmd) => cmd.run(&args.common).await, } } From 49e787c67facecfa1b9cd6f0994efca018d483cb Mon Sep 17 00:00:00 2001 From: Sean Lynch <42618346+swlynch99@users.noreply.github.com> Date: Fri, 6 Sep 2024 13:48:41 -0700 Subject: [PATCH 2/2] Update saved sqlx queries --- ...2c9c657ede5cb2dd677fa2d89755a011cea46.json | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 .sqlx/query-fba8b548ce5922b59916c3801602c9c657ede5cb2dd677fa2d89755a011cea46.json diff --git a/.sqlx/query-fba8b548ce5922b59916c3801602c9c657ede5cb2dd677fa2d89755a011cea46.json b/.sqlx/query-fba8b548ce5922b59916c3801602c9c657ede5cb2dd677fa2d89755a011cea46.json new file mode 100644 index 0000000..7670eea --- /dev/null +++ b/.sqlx/query-fba8b548ce5922b59916c3801602c9c657ede5cb2dd677fa2d89755a011cea46.json @@ -0,0 +1,34 @@ +{ + "db_name": "PostgreSQL", + "query": "\n SELECT\n index,\n label,\n value::text as \"value!\"\n FROM durable.event\n WHERE task_id = $1\n ORDER BY index ASC\n ", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "index", + "type_info": "Int4" + }, + { + "ordinal": 1, + "name": "label", + "type_info": "Text" + }, + { + "ordinal": 2, + "name": "value!", + "type_info": "Text" + } + ], + "parameters": { + "Left": [ + "Int8" + ] + }, + "nullable": [ + false, + false, + null + ] + }, + "hash": "fba8b548ce5922b59916c3801602c9c657ede5cb2dd677fa2d89755a011cea46" +}