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

Job desrialization #7

Open
KABBOUCHI opened this issue Oct 25, 2024 · 2 comments
Open

Job desrialization #7

KABBOUCHI opened this issue Oct 25, 2024 · 2 comments

Comments

@KABBOUCHI
Copy link
Contributor

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(())
    }
}
@levkk
Copy link
Owner

levkk commented Nov 18, 2024

I'm wondering, how does typetag eliminate registering the job on the queue worker; do you have an example?

@KABBOUCHI
Copy link
Contributor Author

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<dyn Job> = 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants