Skip to main content

post_archiver_utils/task/
sync.rs

1use plyne::{FnEevent, Output};
2use post_archiver::{AuthorId, PostId, error::Result, importer::{UnsyncAuthor, UnsyncPost}, manager::PostArchiverManager};
3use tokio::sync::Mutex;
4
5use crate::{progress::ProgressSet, task::Manager};
6
7pub type SyncPostEvent = FnEevent<UnsyncPost, Result<PostId>>;
8
9pub async fn sync_post_task(
10    mut sync_pipeline: Output<SyncPostEvent>, 
11    manager: &Manager,
12    progress: &ProgressSet,
13) {
14    let progress = progress.add("posts");
15
16    while let Some((post, ret)) = sync_pipeline.recv().await {
17        let mut manager = manager.lock().await;
18        let tx = manager.transaction().expect("Failed to start transaction");
19        let result = tx.import_post(post, true);
20        tx.commit().expect("Failed to commit transaction");
21        ret.send(result.map(|(id, _, _)| id)).ok();
22        progress.inc(1);
23    }
24}
25
26pub type SyncAuthorEvent = FnEevent<UnsyncAuthor, Result<AuthorId>>;
27pub async fn sync_author_task(mut sync_pipeline: Output<SyncAuthorEvent>, manager: &Mutex<PostArchiverManager>) {
28    while let Some((author, ret)) = sync_pipeline.recv().await {
29        let mut manager = manager.lock().await;
30        let tx = manager.transaction().expect("Failed to start transaction");
31        let result = tx.import_author(author);
32        tx.commit().expect("Failed to commit transaction");
33        ret.send(result.into()).ok();
34    }
35}