From e8e09917eb585a226a60b0a2d1c0b1074256e82b Mon Sep 17 00:00:00 2001 From: geofmureithi Date: Sat, 13 Jul 2024 20:41:20 +0300 Subject: [PATCH 1/3] fix: serde for sql request --- packages/apalis-core/src/task/attempt.rs | 4 +++- packages/apalis-sql/src/context.rs | 2 +- packages/apalis-sql/src/from_row.rs | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/apalis-core/src/task/attempt.rs b/packages/apalis-core/src/task/attempt.rs index ba557c4b..7c96e5e1 100644 --- a/packages/apalis-core/src/task/attempt.rs +++ b/packages/apalis-core/src/task/attempt.rs @@ -1,7 +1,9 @@ use std::sync::{atomic::AtomicUsize, Arc}; +use serde::{Deserialize, Serialize}; + /// A wrapper to keep count of the attempts tried by a task -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Attempt(Arc); impl Default for Attempt { diff --git a/packages/apalis-sql/src/context.rs b/packages/apalis-sql/src/context.rs index a0774993..4b295419 100644 --- a/packages/apalis-sql/src/context.rs +++ b/packages/apalis-sql/src/context.rs @@ -7,7 +7,7 @@ use std::{fmt, str::FromStr}; /// The context for a job is represented here /// Used to provide a context for a job with an sql backend -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct SqlContext { id: TaskId, status: State, diff --git a/packages/apalis-sql/src/from_row.rs b/packages/apalis-sql/src/from_row.rs index 00d748f7..b47f75c9 100644 --- a/packages/apalis-sql/src/from_row.rs +++ b/packages/apalis-sql/src/from_row.rs @@ -1,10 +1,11 @@ use apalis_core::task::task_id::TaskId; use apalis_core::{data::Extensions, request::Request, worker::WorkerId}; +use serde::{Deserialize, Serialize}; use sqlx::{Decode, Type}; use crate::context::SqlContext; /// Wrapper for [Request] -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct SqlRequest { req: T, context: SqlContext, From 9325c29926c4ae69d2d616053f89cf969c03aa7e Mon Sep 17 00:00:00 2001 From: geofmureithi Date: Sat, 13 Jul 2024 20:57:15 +0300 Subject: [PATCH 2/3] fix: serde for attempts --- packages/apalis-core/src/task/attempt.rs | 42 ++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/packages/apalis-core/src/task/attempt.rs b/packages/apalis-core/src/task/attempt.rs index 7c96e5e1..541239df 100644 --- a/packages/apalis-core/src/task/attempt.rs +++ b/packages/apalis-core/src/task/attempt.rs @@ -1,11 +1,47 @@ -use std::sync::{atomic::AtomicUsize, Arc}; +use std::sync::{atomic::{AtomicUsize, Ordering}, Arc}; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; /// A wrapper to keep count of the attempts tried by a task -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone)] pub struct Attempt(Arc); +// Custom serialization function +fn serialize(attempt: &Attempt, serializer: S) -> Result +where + S: Serializer, +{ + let value = attempt.0.load(Ordering::SeqCst); + serializer.serialize_u64(value as u64) +} + +// Custom deserialization function +fn deserialize<'de, D>(deserializer: D) -> Result +where + D: Deserializer<'de>, +{ + let value = u64::deserialize(deserializer)?; + Ok(Attempt(Arc::new(AtomicUsize::new(value as usize)))) +} + +impl Serialize for Attempt { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serialize(self, serializer) + } +} + +impl<'de> Deserialize<'de> for Attempt { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + deserialize(deserializer) + } +} + impl Default for Attempt { fn default() -> Self { Self(Arc::new(AtomicUsize::new(0))) From 36ad6ccd895d89cdc5b3adf7d3a78193777bc95a Mon Sep 17 00:00:00 2001 From: geofmureithi Date: Sat, 13 Jul 2024 21:05:44 +0300 Subject: [PATCH 3/3] lint: fmt --- packages/apalis-core/src/task/attempt.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/apalis-core/src/task/attempt.rs b/packages/apalis-core/src/task/attempt.rs index 541239df..3f4825a1 100644 --- a/packages/apalis-core/src/task/attempt.rs +++ b/packages/apalis-core/src/task/attempt.rs @@ -1,4 +1,7 @@ -use std::sync::{atomic::{AtomicUsize, Ordering}, Arc}; +use std::sync::{ + atomic::{AtomicUsize, Ordering}, + Arc, +}; use serde::{Deserialize, Deserializer, Serialize, Serializer};