-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from CoLearn-Dev/wait-task
- add wait_task - fix read_or_wait
- Loading branch information
Showing
5 changed files
with
52 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crate::colink_proto::*; | ||
use prost::Message; | ||
use tracing::debug; | ||
|
||
type Error = Box<dyn std::error::Error + Send + Sync + 'static>; | ||
|
||
impl crate::application::CoLink { | ||
pub async fn wait_task(&self, task_id: &str) -> Result<(), Error> { | ||
let task_key = format!("_internal:tasks:{}", task_id); | ||
let start_timestamp = match self | ||
.read_entries(&[StorageEntry { | ||
key_name: task_key.clone(), | ||
..Default::default() | ||
}]) | ||
.await | ||
{ | ||
Ok(res) => { | ||
let task: Task = Message::decode(&*res[0].payload).unwrap(); | ||
if task.status == "finished" { | ||
return Ok(()); | ||
} | ||
get_timestamp(&res[0].key_path) + 1 | ||
} | ||
Err(_) => 0, | ||
}; | ||
let queue_name = self.subscribe(&task_key, Some(start_timestamp)).await?; | ||
let mut subscriber = self.new_subscriber(&queue_name).await?; | ||
loop { | ||
let data = subscriber.get_next().await?; | ||
debug!("Received [{}]", String::from_utf8_lossy(&data)); | ||
let message: SubscriptionMessage = Message::decode(&*data).unwrap(); | ||
if message.change_type != "delete" { | ||
let task: Task = Message::decode(&*message.payload).unwrap(); | ||
if task.status == "finished" { | ||
break; | ||
} | ||
} | ||
} | ||
self.unsubscribe(&queue_name).await?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn get_timestamp(key_path: &str) -> i64 { | ||
let pos = key_path.rfind('@').unwrap(); | ||
key_path[pos + 1..].parse().unwrap() | ||
} |