Repository navigation
Expand file tree
/
Copy pathraw_log.rs
More file actions
62 lines (56 loc) · 2.06 KB
/
Copy pathraw_log.rs
File metadata and controls
62 lines (56 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Fetch an Actor run's log in its raw (unprocessed) form.
//!
//! By default the API adds a per-line timestamp prefix to log lines. Passing
//! `LogOptions { raw: Some(true), ..Default::default() }` requests the unprocessed log instead.
//! The example fetches it as a whole with `get_with_options`, and streams it with
//! the log client's `stream_with_options`.
//!
//! Run with: `APIFY_TOKEN=... cargo run --example raw_log`
use apify_client::{ApifyClient, LogOptions};
use futures_util::StreamExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let token = std::env::var("APIFY_TOKEN").expect("set APIFY_TOKEN");
let client = ApifyClient::new(token);
// Run a short public Actor and wait for it to finish so it has a complete log.
let run = client
.actor("apify/hello-world")
.call::<serde_json::Value>(
None,
apify_client::ActorCallOptions {
wait_secs: Some(120),
log: apify_client::LogRedirect::Disabled,
..Default::default()
},
)
.await?;
println!("Run {} finished with status {:?}", run.id, run.status);
// Buffered raw log: fetch the whole log without the server-added per-line timestamps.
let raw_log = client
.log(&run.id)
.get_with_options(LogOptions {
raw: Some(true),
..Default::default()
})
.await?;
match raw_log {
Some(log) => println!("Fetched raw log ({} bytes)", log.len()),
None => println!("Run has no log"),
}
// Streaming raw log: stream the same unprocessed log chunk by chunk.
let mut stream = client
.run(&run.id)
.log()
.stream_with_options(LogOptions {
raw: Some(true),
..Default::default()
})
.await?
.ok_or("the log does not exist")?;
let mut streamed_bytes = 0usize;
while let Some(chunk) = stream.next().await {
streamed_bytes += chunk?.len();
}
println!("Streamed raw log ({streamed_bytes} bytes)");
Ok(())
}