Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to schedule a task at a specific time or with an interval?

Is there a way to run a task in rust, a thread at best, at a specific time or in an interval again and again?

So that I can run my function every 5 minutes or every day at 12 o'clock.

In Java there is the TimerTask, so I'm searching for something like that.

like image 743
proc Avatar asked Feb 06 '15 11:02

proc


People also ask

What is time interval on a schedule?

A time interval is the amount of time between two given points in time. An example of this is: "The time interval between three o'clock and four o'clock is one hour."

How do you schedule a task in airflow?

The scheduler uses the configured Executor to run tasks that are ready. Your DAGs will start executing once the scheduler is running successfully. The first DAG Run is created based on the minimum start_date for the tasks in your DAG. Subsequent DAG Runs are created according to your DAG's timetable.

How do I schedule a task from the command line?

Open Start. Search for Command Prompt, right-click the top result, and select the Run as administrator option. Type the following command to change the time to run the task 9:00am and press Enter:Syntax SCHTASKS /CHANGE /TN "FOLDERPATH\TASKNAME" /ST HH:MM Example SCHTASKS /CHANGE /TN "MyTasks\Notepad task" /ST 09:00.


1 Answers

You can use Timer::periodic to create a channel that gets sent a message at regular intervals, e.g.

use std::old_io::Timer;

let mut timer = Timer::new().unwrap();
let ticks = timer.periodic(Duration::minutes(5));
for _ in ticks.iter() {
    your_function();
}

Receiver::iter blocks, waiting for the next message, and those messages are 5 minutes apart, so the body of the for loop is run at those regular intervals. NB. this will use a whole thread for that single function, but I believe one can generalise to any fixed number of functions with different intervals by creating multiple timer channels and using select! to work out which function should execute next.

I'm fairly sure that running every day at a specified time, correctly, isn't possible with the current standard library. E.g. using a simple Timer::periodic(Duration::days(1)) won't handle the system clock changing, e.g. when the user moves timezones, or goes in/out of daylight savings.

like image 177
huon Avatar answered Sep 20 '22 13:09

huon