0

Schedule the program in Rust

 3 years ago
source link: https://blog.knoldus.com/schedule-the-program-in-rust/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Reading Time: 3 minutes

Hello, folks! your wait is over, we have come up with a new blog. In this blog, we will discuss how we can schedule any rust program by which it can execute automatically execute on the scheduled time with the help of a sample example. I hope you will enjoy the blog.

Rust:

Rust is a multi-paradigm system programming language that can provide better memory safety while maintaining high performance. It runs blazingly fast, prevents almost all crashes, and eliminates data races. It has an exceptional feature of memory management which can be achieved by the Ownership Concept.
Rust also comes with a fairly nice build system (cargo) that keeps your libraries up to date or locks you to a specific version. It also provides powerful features such as zero-cost abstractions, safe memory management, fearless concurrency and many more.
It also prevents segmentation faults and guarantees thread safety.

We have used Rust programming language to develop many command line and networking applications in which i faced a scenario many time like the program or application we have developed that should run in the regular interval of time. So for that we have to schedule the program. In this blog we see how we can do it.

Scheduler:

The Scheduler has a wide variety of uses – anything you want your program to do automatically, you can configure here. For example, you could use the scheduler to automatically process or upload the data on the server.

There are many crates in Rust by which we can schedule the task or program periodically as per our requirement. We will Use job_scheduler crate to achieve that.

First we have to add the dependency in the cargo.toml file:

[dependencies]
job_scheduler = "1.2.1"

Then you have create a object of jobScheduler and add the task in that scheduler object.

Example:

extern crate job_scheduler;

use job_scheduler::{Job, JobScheduler};
use std::time::Duration;

fn main() {
    let mut scheduler = JobScheduler::new();

    // Adding a task to scheduler to execute it in every 2 minutes.
    scheduler.add(Job::new("1/2 * * * * *".parse().unwrap(), || {
        println!("Get executed every 2 seconds!");
    }));

    // Adding a task to scheduler to execute it in every 2 minutes.
    scheduler.add(Job::new("1/10 * * * * *".parse().unwrap(), || {
        println!("Get executed every 10 seconds!");
    }));

    loop {
        scheduler.tick();
        std::thread::sleep(Duration::from_millis(100));
    }
}

Output:

This image has an empty alt attribute; its file name is screenshot-from-2020-12-21-11-32-51.png

Cron Expression:

Creating a schedule for a job is done using the FromStr impl for the Schedule type of the cron library.

The scheduling format is as follows:

sec   min   hour   day of month   month   day of week   year
*     *     *      *              *       *             *

Time is specified for UTC and not your local timezone. Note that the year may be omitted.

Comma separated values such as 5,8,10 represent more than one time value. So for example, a schedule of 0 2,14,26 * * * * would execute on the 2nd, 14th, and 26th minute of every hour.

Ranges can be specified with a dash. A schedule of 0 0 * 5-10 * * would execute once per hour but only on day 5 through 10 of the month.

Day of the week can be specified as an abbreviation or the full name. A schedule of 0 0 6 * * Sun,Sat would execute at 6am on Sunday and Saturday.

Note: I hope our blogs help you to enhance your learning. I’ll post more blogs on Rust. Stay Tuned.

If you want to read more content like this?  Subscribe Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe Rust Times Newsletter: https://bit.ly/2Vdlld7 .

Happy learning!!!

This image has an empty alt attribute; its file name is screenshot-from-2020-06-08-11-00-35.png

References:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK