Defining tasks for workers
When it comes to running our tasks, we need fields so we can pass them in as inputs to the task being run. Our tasks also need a run
function so we can choose when to run tasks as running a task takes a long time. We can define a basic addition task in our src/tasks/add.rs
file with the following code:
use std::{thread, time}; use serde::{Serialize, Deserialize}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct AddTask { pub one: i32, pub two: i32 } impl AddTask { pub fn run(self) -> i32 { let duration = time::Duration::from_secs(20); thread::sleep(duration); return self.one + self.two } }
None of this code should be a shock. We will implement the Serialize
and Deserialize
traits so we can insert the...