Skip to content
Open
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
32 changes: 27 additions & 5 deletions asynq/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ impl ResultWriter {
}
}

#[derive(Clone)]
pub enum TaskPayloadContentType {
Bytes,
#[cfg(feature = "json")]
Json,
}

/// 表示要执行的工作单元的任务
/// Represents a task as a unit of work to be executed
///
Expand All @@ -98,6 +105,8 @@ pub struct Task {
/// 任务负载数据
/// Task payload data
pub payload: Vec<u8>,
/// Task payload content type
pub payload_content_type: TaskPayloadContentType,
/// 任务头信息
/// Task headers
pub headers: HashMap<String, String>,
Expand All @@ -116,12 +125,22 @@ pub struct Task {
}
impl Debug for Task {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Task")
let mut ds = f.debug_struct("Task");
let mut ds = ds
.field("task_type", &self.task_type)
.field("payload", &self.payload)
.field("headers", &self.headers)
.field("options", &self.options)
.finish()
.field("options", &self.options);
ds = match self.payload_content_type {
#[cfg(feature = "json")]
TaskPayloadContentType::Json => {
match serde_json::from_slice::<serde_json::Value>(&self.payload) {
Ok(value) => ds.field("payload", &value),
Err(_e) => ds.field("payload", &self.payload),
}
}
_ => ds.field("payload", &self.payload),
};
ds.finish()
}
}
impl Task {
Expand All @@ -138,6 +157,7 @@ impl Task {
Ok(Self {
task_type: task_type.to_string(),
payload: payload.to_vec(),
payload_content_type: TaskPayloadContentType::Bytes,
headers: Default::default(),
options: TaskOptions::default(),
result_writer: None,
Expand All @@ -158,7 +178,9 @@ impl Task {
/// Create a new task with JSON payload
pub fn new_with_json<T: AsRef<str>, P: Serialize>(task_type: T, payload: &P) -> Result<Self> {
let json_payload = serde_json::to_vec(payload)?;
Self::new(task_type, &json_payload)
let mut task = Self::new(task_type, &json_payload)?;
task.payload_content_type = TaskPayloadContentType::Json;
Ok(task)
}

/// 设置任务选项
Expand Down