refactor: use futures instead of tokio for media upload

This commit is contained in:
VC
2022-11-14 22:12:02 +01:00
parent 89de1cf7a3
commit ce84c05581
4 changed files with 41 additions and 45 deletions

View File

@@ -25,12 +25,11 @@ use rusqlite::Connection;
use std::sync::Arc;
use tokio::{spawn, sync::Mutex};
use futures::StreamExt;
/// This is where the magic happens
#[tokio::main]
pub async fn run(config: Config) {
// create the task vector for handling multiple accounts
let mut mtask = vec![];
// open the SQLite connection
let conn = Arc::new(Mutex::new(
Connection::open(&config.scootaloo.db_path).unwrap_or_else(|e| {
@@ -41,13 +40,14 @@ pub async fn run(config: Config) {
}),
));
for mastodon_config in config.mastodon.into_values() {
let mut stream = futures::stream::iter(config.mastodon.into_values())
.map(|mastodon_config| {
// create temporary value for each task
let scootaloo_cache_path = config.scootaloo.cache_path.clone();
let token = get_oauth2_token(&config.twitter);
let task_conn = conn.clone();
let task = spawn(async move {
spawn(async move {
info!("Starting treating {}", &mastodon_config.twitter_screen_name);
// retrieve the last tweet ID for the username
@@ -138,14 +138,11 @@ pub async fn run(config: Config) {
}
Ok::<(), ScootalooError>(())
});
// push each task into the vec task
mtask.push(task);
}
}).buffer_unordered(4);
// launch and wait for every handle
for handle in mtask {
match handle.await {
while let Some(result) = stream.next().await {
match result {
Ok(Err(e)) => eprintln!("Error within thread: {}", e),
Err(e) => eprintln!("Error with thread: {}", e),
_ => (),