You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Consider using https://github.com/dtolnay/typetag for bg jobs, to eliminate the need for manual desrialization and even registering the job on queue worker
#[derive(Default, Debug, Serialize, Deserialize)]
struct WelcomeEmail {
email: String,
user_name: String,
}
#[async_trait]+#[typetag::serde]
impl Job for WelcomeEmail {
/// Code in this function will be executed in the background.
- async fn execute(&self, args: serde_json::Value) -> Result<(), JobError> {+ async fn execute(&self) -> Result<(), JobError> {- let args: WelcomeEmail = serde_json::from_value(args)?;
// Send the email to the user
// with the given email address.
Ok(())
}
}
The text was updated successfully, but these errors were encountered:
Typetag works by using a global registry and enums, so as long as your Job implementations are included in the binary, deserialization should work seamlessly. For example:
let job:Box<dynJob> = serde_json::from_str(r#"{ "type": "send_email", "email": "test@example.com" }"#)?;
job.execute();
You can check out my implementation here for more details:
It might also be worth considering adding catch_unwind and a graceful shutdown mechanism if you haven't already. I’ve implemented these features in the job_queue repository as well.
Consider using https://github.com/dtolnay/typetag for bg jobs, to eliminate the need for manual desrialization and even registering the job on queue worker
The text was updated successfully, but these errors were encountered: