From 0fbc67052a2a1945ca4bababe699053758bf0b18 Mon Sep 17 00:00:00 2001 From: Rafael Caricio Date: Sat, 11 Mar 2023 18:13:48 +0100 Subject: [PATCH] Don't generate default task unique hashes --- Cargo.toml | 5 +---- src/store.rs | 10 +--------- src/task.rs | 14 +++----------- src/worker_pool.rs | 4 ++-- 4 files changed, 7 insertions(+), 26 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dc3832a..32b314f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" authors = [ "Rafael Caricio ", ] -description = "Async background job processing library with Diesel and Tokio" +description = "Async background task processing library with Tokio and Postgres" repository = "https://code.caric.io/rafaelcaricio/backie" edition = "2021" license = "MIT" @@ -15,13 +15,10 @@ rust-version = "1.67" doctest = false [dependencies] -cron = "0.12" chrono = "0.4" -hex = "0.4" log = "0.4" serde = { version = "1", features = ["derive"] } serde_json = "1" -sha2 = "0.10" anyhow = "1" thiserror = "1" uuid = { version = "1.1", features = ["v4", "serde"] } diff --git a/src/store.rs b/src/store.rs index fa46218..de82f50 100644 --- a/src/store.rs +++ b/src/store.rs @@ -100,19 +100,11 @@ pub mod test_store { use std::sync::Arc; use tokio::sync::Mutex; - #[derive(Clone)] + #[derive(Default, Clone)] pub struct MemoryTaskStore { tasks: Arc>>, } - impl MemoryTaskStore { - pub fn new() -> Self { - MemoryTaskStore { - tasks: Arc::new(Mutex::new(BTreeMap::new())), - } - } - } - #[async_trait::async_trait] impl TaskStore for MemoryTaskStore { async fn pull_next_task(&self, queue_name: &str) -> Result, AsyncQueueError> { diff --git a/src/task.rs b/src/task.rs index a64945b..ebe553c 100644 --- a/src/task.rs +++ b/src/task.rs @@ -5,7 +5,6 @@ use chrono::Utc; use diesel::prelude::*; use diesel_derive_newtype::DieselNewType; use serde::Serialize; -use sha2::{Digest, Sha256}; use std::borrow::Cow; use std::fmt; use std::fmt::Display; @@ -41,15 +40,8 @@ impl Display for TaskId { pub struct TaskHash(Cow<'static, str>); impl TaskHash { - pub fn default_for_task(value: &T) -> Result - where - T: Serialize, - { - let value = serde_json::to_value(value)?; - let mut hasher = Sha256::new(); - hasher.update(serde_json::to_string(&value)?.as_bytes()); - let result = hasher.finalize(); - Ok(TaskHash(Cow::from(hex::encode(result)))) + pub fn new>(hash: T) -> Self { + TaskHash(Cow::Owned(hash.into())) } } @@ -174,7 +166,7 @@ pub struct CurrentTask { impl CurrentTask { pub(crate) fn new(task: &Task) -> Self { Self { - id: task.id.clone(), + id: task.id, retries: task.retries, created_at: task.created_at, } diff --git a/src/worker_pool.rs b/src/worker_pool.rs index 2fcfd6b..000c98a 100644 --- a/src/worker_pool.rs +++ b/src/worker_pool.rs @@ -112,7 +112,7 @@ where let mut worker: Worker = Worker::new( self.task_store.clone(), queue_name.clone(), - retention_mode.clone(), + *retention_mode, self.task_registry.clone(), self.application_data_fn.clone(), Some(rx.clone()), @@ -284,7 +284,7 @@ mod tests { } async fn memory_store() -> MemoryTaskStore { - MemoryTaskStore::new() + MemoryTaskStore::default() } #[tokio::test]