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 = [
"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"
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"] }

View file

@ -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<Mutex<BTreeMap<TaskId, Task>>>,
}
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<Option<Task>, AsyncQueueError> {

View file

@ -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<T>(value: &T) -> Result<Self, serde_json::Error>
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<T: Into<String>>(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,
}

View file

@ -112,7 +112,7 @@ where
let mut worker: Worker<AppData, S> = 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]