Don't generate default task unique hashes

This commit is contained in:
Rafael Caricio 2023-03-11 18:13:48 +01:00
parent 894f928c01
commit 0fbc67052a
Signed by: rafaelcaricio
GPG key ID: 3C86DBCE8E93C947
4 changed files with 7 additions and 26 deletions

View file

@ -4,7 +4,7 @@ version = "0.1.0"
authors = [ authors = [
"Rafael Caricio <rafael@caricio.com>", "Rafael Caricio <rafael@caricio.com>",
] ]
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" repository = "https://code.caric.io/rafaelcaricio/backie"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
@ -15,13 +15,10 @@ rust-version = "1.67"
doctest = false doctest = false
[dependencies] [dependencies]
cron = "0.12"
chrono = "0.4" chrono = "0.4"
hex = "0.4"
log = "0.4" log = "0.4"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
sha2 = "0.10"
anyhow = "1" anyhow = "1"
thiserror = "1" thiserror = "1"
uuid = { version = "1.1", features = ["v4", "serde"] } uuid = { version = "1.1", features = ["v4", "serde"] }

View file

@ -100,19 +100,11 @@ pub mod test_store {
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::Mutex; use tokio::sync::Mutex;
#[derive(Clone)] #[derive(Default, Clone)]
pub struct MemoryTaskStore { pub struct MemoryTaskStore {
tasks: Arc<Mutex<BTreeMap<TaskId, Task>>>, tasks: Arc<Mutex<BTreeMap<TaskId, Task>>>,
} }
impl MemoryTaskStore {
pub fn new() -> Self {
MemoryTaskStore {
tasks: Arc::new(Mutex::new(BTreeMap::new())),
}
}
}
#[async_trait::async_trait] #[async_trait::async_trait]
impl TaskStore for MemoryTaskStore { impl TaskStore for MemoryTaskStore {
async fn pull_next_task(&self, queue_name: &str) -> Result<Option<Task>, AsyncQueueError> { async fn pull_next_task(&self, queue_name: &str) -> Result<Option<Task>, AsyncQueueError> {

View file

@ -5,7 +5,6 @@ use chrono::Utc;
use diesel::prelude::*; use diesel::prelude::*;
use diesel_derive_newtype::DieselNewType; use diesel_derive_newtype::DieselNewType;
use serde::Serialize; use serde::Serialize;
use sha2::{Digest, Sha256};
use std::borrow::Cow; use std::borrow::Cow;
use std::fmt; use std::fmt;
use std::fmt::Display; use std::fmt::Display;
@ -41,15 +40,8 @@ impl Display for TaskId {
pub struct TaskHash(Cow<'static, str>); pub struct TaskHash(Cow<'static, str>);
impl TaskHash { impl TaskHash {
pub fn default_for_task<T>(value: &T) -> Result<Self, serde_json::Error> pub fn new<T: Into<String>>(hash: T) -> Self {
where TaskHash(Cow::Owned(hash.into()))
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))))
} }
} }
@ -174,7 +166,7 @@ pub struct CurrentTask {
impl CurrentTask { impl CurrentTask {
pub(crate) fn new(task: &Task) -> Self { pub(crate) fn new(task: &Task) -> Self {
Self { Self {
id: task.id.clone(), id: task.id,
retries: task.retries, retries: task.retries,
created_at: task.created_at, created_at: task.created_at,
} }

View file

@ -112,7 +112,7 @@ where
let mut worker: Worker<AppData, S> = Worker::new( let mut worker: Worker<AppData, S> = Worker::new(
self.task_store.clone(), self.task_store.clone(),
queue_name.clone(), queue_name.clone(),
retention_mode.clone(), *retention_mode,
self.task_registry.clone(), self.task_registry.clone(),
self.application_data_fn.clone(), self.application_data_fn.clone(),
Some(rx.clone()), Some(rx.clone()),
@ -284,7 +284,7 @@ mod tests {
} }
async fn memory_store() -> MemoryTaskStore { async fn memory_store() -> MemoryTaskStore {
MemoryTaskStore::new() MemoryTaskStore::default()
} }
#[tokio::test] #[tokio::test]