pub struct ScheduledTask { /* private fields */ }
Expand description
A task to run on a schedule.
Implementations§
Source§impl ScheduledTask
impl ScheduledTask
Sourcepub fn new(
schedule: ScheduleAt,
task: Box<dyn Fn() -> Pin<Box<dyn Future<Output = ()>>> + Send + 'static>,
) -> Result<Self, &'static str>
pub fn new( schedule: ScheduleAt, task: Box<dyn Fn() -> Pin<Box<dyn Future<Output = ()>>> + Send + 'static>, ) -> Result<Self, &'static str>
Create a new scheduled task.
If the schedule includes a Duration less than 0
, returns an error.
Examples found in repository?
examples/schedule.rs (lines 10-13)
9fn main() {
10 let periodic_task = ScheduledTask::new(
11 ScheduleAt::Interval(Duration::seconds(4)),
12 task!(async { println!("You'll see me every four seconds") })
13 ).unwrap();
14
15 let onetime_task = ScheduledTask::new(
16 ScheduleAt::DateTime(Utc::now() + Duration::seconds(15)),
17 task!(task2())
18 ).unwrap();
19
20 let daily_task = ScheduledTask::new(
21 ScheduleAt::Daily(Time::from_hms(01, 23, 45)),
22 task!(async { println!("Hello"); })
23 ).unwrap();
24
25 let schedule = Schedule::builder()
26 .tasks([
27 periodic_task,
28 onetime_task,
29 daily_task,
30 ])
31 .wake_interval(Duration::seconds(1)).unwrap()
32 .build();
33
34 schedule.run();
35
36 // run() exits immediately, so we need to wait a bit.
37 std::thread::sleep(Duration::seconds(30).to_std().unwrap());
38}
Auto Trait Implementations§
impl Freeze for ScheduledTask
impl !RefUnwindSafe for ScheduledTask
impl Send for ScheduledTask
impl !Sync for ScheduledTask
impl Unpin for ScheduledTask
impl !UnwindSafe for ScheduledTask
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more