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

Background task #74

Open
akelyasir opened this issue Jan 18, 2024 · 10 comments
Open

Background task #74

akelyasir opened this issue Jan 18, 2024 · 10 comments
Labels
enhancement New feature or request

Comments

@akelyasir
Copy link

Hi @photino

How can we add background jobs for the queries made. In order not to keep any request waiting. For example, we have sent an e-mail and to complete the process by adding it to the background jobs without waiting for it to arrive.

@photino
Copy link
Collaborator

photino commented Jan 19, 2024

We currently support the cron tasks running in the background. It seems that you need a single immediately executed task. We may add this feature in the next release.

@photino
Copy link
Collaborator

photino commented Jan 19, 2024

@akelyasir Does the tokio::task::spawn work in your case?

@akelyasir
Copy link
Author

Hi @photino I would also like to make the background job repeat more than once in case it fails, and I will also record the status and results of the operations that create a model for background jobs.

I would also like to make the background job repeat more than once in case it fails, and I will also record the status and results of the operations that create a model for background jobs.

I found https://github.com/geofmureithi-zz/apalis for Actix. It can be referenced or integrated.

But I couldn't find exactly where to start.

@photino photino added the enhancement New feature or request label Jan 19, 2024
@akelyasir
Copy link
Author

Hi @photino

For example, if an function that we will call in controller we have created will take a long time, to run this function in the background and finish the controller operation as complate.

`

async fn example_function() {}

async fn example_controller(req: Request) -> Result {
  .....
 ......
  // Run background
  example_function();
....
 Ok(res.into)
}

`

How is it appropriate to carry out such an operation.

I thought of adding it to the redis or database as a task and running the functions of the incomplate task with cron, but I couldn't figüre out how to call the functions.

@photino
Copy link
Collaborator

photino commented Jan 21, 2024

@akelyasir Another possile solution is using a queue. You can create a global channel to receive the data (such as in the form of CloudEvent) from the controller and then run the process logic asynchronously.

@photino
Copy link
Collaborator

photino commented Jan 21, 2024

@akelyasir I think what you need is an event-driven approach. You may also refer to https://news.ycombinator.com/item?id=38349716

@photino
Copy link
Collaborator

photino commented Jan 21, 2024

I thought of adding it to the redis or database as a task and running the functions of the incomplate task with cron, but I couldn't figüre out how to call the functions.

This approach is also OK. You can save the necessary data to the database, such as the id, name, email address and status to be sent an email to. In the cron job, you first filter the users by the status and then run the process logic asynchronously.

@akelyasir
Copy link
Author

@photino Thank you for your help. I will investigate what you have said.

@akelyasir
Copy link
Author

@photino Can you give an example about Cloudevent?

@photino
Copy link
Collaborator

photino commented Jan 24, 2024

@photino Can you give an example about Cloudevent?

CloudEvent is a vendor-neutral specification for defining the format of event data. In zino, you can define an event such as

let mut event = CloudEvent::new(id, source, event_type);
event.set_data(json!({
     "sender": "alice",
     "receiver": "bob",
     "message": "test",
});

In your task queue, you can subscribe the events with a specified event-type and process the logic when there is a new event.

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

No branches or pull requests

2 participants