Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: serde for sql request #371

Merged
merged 3 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion packages/apalis-core/src/task/attempt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
use std::sync::{atomic::AtomicUsize, Arc};
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};

use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// A wrapper to keep count of the attempts tried by a task
#[derive(Debug, Clone)]
pub struct Attempt(Arc<AtomicUsize>);

// Custom serialization function
fn serialize<S>(attempt: &Attempt, serializer: S) -> Result<S::Ok, S::Error>
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<Attempt, D::Error>
where
D: Deserializer<'de>,
{
let value = u64::deserialize(deserializer)?;
Ok(Attempt(Arc::new(AtomicUsize::new(value as usize))))
}

impl Serialize for Attempt {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serialize(self, serializer)
}
}

impl<'de> Deserialize<'de> for Attempt {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserialize(deserializer)
}
}

impl Default for Attempt {
fn default() -> Self {
Self(Arc::new(AtomicUsize::new(0)))
Expand Down
2 changes: 1 addition & 1 deletion packages/apalis-sql/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packages/apalis-sql/src/from_row.rs
Original file line number Diff line number Diff line change
@@ -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<T> {
req: T,
context: SqlContext,
Expand Down
Loading