Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

tokio-cron-scheduler

> 编程语言
Open source

Schedule tasks on Tokio using cron-like annotation

735 stars0 likes1 views
WebsiteGitHub

About

Schedule tasks on Tokio using cron-like annotation

tokio-cron-scheduler

Use cron-like scheduling in an async tokio environment. Also, schedule tasks instantly or repeat them at a fixed duration. Task data can optionally be persisted using PostgreSQL or Nats.

Inspired by https://github.com/lholden/job_scheduler

Usage

Please see the Documentation for more details.

Be sure to add the job_scheduler crate to your Cargo.toml:

toml
[dependencies]
tokio-cron-scheduler = "*"

Creating a schedule for a job is done using any ToString impl, leveraging the Cron type of the croner library.

The scheduling format is as follows:

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

Time is specified for UTC and not your local timezone. Note that the year may be omitted. If you want for your timezone, append _tz to the job creation calls (for instance Job::new_async vs Job::new_async_tz).

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 days 5 through 10 of the month.

The 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 6 am on Sunday and Saturday.

Per job, you can be notified when the jobs were started, stopped and removed. Because these notifications are scheduled using tokio::spawn, the order of these are not guaranteed if the task finishes quickly.

A simple usage example:

…

Timezone changes

You can create a job using a specific timezone using the JobBuilder API. chrono-tz is not included into the dependencies, so you need to add it to your Cargo.toml if you would like to have easy creation of a Timezone struct.

…

Similar Libraries

  • job_scheduler The crate that inspired this one
  • croner-rust the cron expression parser we use.
  • schedule-rs is a similar rust library that implements its own cron expression parser.

License

TokioCronScheduler is licensed under either of

  • Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  • MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

Custom storage

The MetadataStore and NotificationStore traits can be implemented and be used in the JobScheduler.

A default volatile hashmap-based version is provided by the SimpleMetadataStore and SimpleNotificationStore. A persistent version using Nats is provided with NatsMetadataStore and NatsNotificationStore.

Contributing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Please see the CONTRIBUTING file for more information.

Features

english

Since 0.13.0

Enables the schedule text to be interpreted in English. This is done using the english-to-cron crate. For instance "every 15 seconds" will be converted in the background to "0/15 * * * * ? *".

has_bytes

Since 0.7

Enables Prost-generated data structures to be used by stores that need to get the bytes of the data structs. The Nats and Postgres stores depend on this feature being enabled.

postgres_storage

Since 0.6

Adds the Postgres metadata store and notification store (PostgresMetadataStore, PostgresNotificationStore). Use a Postgres database to store the metadata and notification data.

See PostgreSQL docs

postgres_native_tls

Since 0.6

Uses postgres-native-tls crate as the TLS provider for the PostgreSQL connection.

postgres_openssl

Since 0.6

Uses the postgres-openssl crate as the TLS provider for the PostgreSQL connection.

nats_storage

Since 0.6

Adds the Nats metadata store and notification store (NatsMetadataStore, NatsNotificationStore). Use a Nats system as a way to store the metadata and notifications.

See Nats docs

signal

Since 0.5

Adds shutdown_on_signal and shutdown_on_ctrl_c to the scheduler. Both shut the system down (stop the scheduler and remove all the tasks) when a signal is received.

As this leverages the signal handling from Tokio, this is only available on Unix systems.

Writing tests

When doing a tokio::test, remember to have it run in a multi-threaded context otherwise, the test will hang on scheduler.add().

For example:

…

Examples

simple

Runs the in-memory hashmap-based storage

bash
 cargo run --example simple --features="tracing-subscriber"

postgres

Needs a running PostgreSQL instance first:

bash
docker run --rm -it -p 5432:5432 -e POSTGRES_USER="postgres" -e POSTGRES_PASSWORD="" -e POSTGRES_HOST_AUTH_METHOD="trust" postgres:14.1

Then run the example:

bash
POSTGRES_INIT_METADATA=true POSTGRES_INIT_NOTIFICATIONS=true cargo run --example postgres --features="postgres_storage tracing-subscriber"

nats

Needs a running Nats instance first with Jetstream enabled:

bash
docker run --rm -it -p 4222:4222 -p 6222:6222 -p 7222:7222 -p 8222:8222 nats -js -DV

Then run the example:

bash
cargo run --example nats --features="nats_storage tracing-subscriber"

Design

Job activity

Create job

Create notification

Delete job

Delete notification

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rust

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言