https://github.com/shyiko/skedule
A human-friendly alternative to cron. Designed after GAE's schedule for Kotlin and/or Java 8+.
https://github.com/shyiko/skedule
Last synced: 19 days ago
JSON representation
A human-friendly alternative to cron. Designed after GAE's schedule for Kotlin and/or Java 8+.
- Host: GitHub
- URL: https://github.com/shyiko/skedule
- Owner: shyiko
- Created: 2017-07-08T17:20:03.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-03-16T06:52:27.000Z (about 3 years ago)
- Last Synced: 2025-04-21T09:06:48.015Z (about 1 month ago)
- Language: Kotlin
- Size: 77.1 KB
- Stars: 82
- Watchers: 4
- Forks: 7
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
skedule
A human-friendly alternative to cron.
Designed after GAE's schedule for Kotlin and/or Java 8+.Features:
- **TZ support**;
- **fluent** / **immutable** / **java.time.***-based API;
- **zero dependencies**.## Usage
```xml
com.github.shyiko.skedule
skedule
0.4.0
kalvanized```
> (java)
```java
// creating schedule from a string
Schedule.parse("every monday 09:00");// programmatic construction
Schedule.at(LocalTime.of(9, 0)).every(DayOfWeek.MONDAY).toString().equals("every monday 09:00");ZonedDateTime now = ZonedDateTime.parse("2007-12-03T10:15:30+02:00[Europe/Kiev]");
ZonedDateTime nxt = ZonedDateTime.parse("2007-12-10T09:00:00+02:00[Europe/Kiev]");// determining "next" time
Schedule.parse("every monday 09:00").next(now).equals(nxt);// iterating over (infinite) schedule
Schedule.parse("every monday 09:00").iterate(now)/*: Iterator */.next().equals(nxt);
```#### Format
Schedule format is described in [GAE cron.xml reference](https://cloud.google.com/appengine/docs/standard/java/config/cronref#schedule_format).
Here are some examples (taken from the official GAE documentation):```
every 12 hours
every 5 minutes from 10:00 to 14:00
every day 00:00
every monday 09:00
2nd,third mon,wed,thu of march 17:00
1st monday of sep,oct,nov 17:00
1 of jan,april,july,oct 00:00
```Additionally:
- `hour` can be used in place of `1 hours` (same goes for `1 minutes`)
(e.g. `every hour`, `every minute from 10:00 to 14:00`) (since [0.3.0](https://github.com/shyiko/skedule/blob/master/CHANGELOG.md#030---2017-07-10))
- `-` can be used to join consequent days & months (e.g. `every mon-fri 09:00`, `1-7 of jun-aug 00:00`) (since [0.3.0](https://github.com/shyiko/skedule/blob/master/CHANGELOG.md#030---2017-07-10))#### (example) Scheduling using ScheduledThreadPoolExecutor
```java
import com.github.shyiko.skedule.Schedule;import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
executor.setRemoveOnCancelPolicy(true);
ZonedDateTime now = ZonedDateTime.now();
executor.schedule(
() -> {
System.out.println("TASK 'every 5 minutes from 12:00 to 12:30' EXECUTED");
// re-schedule if needed
},
Schedule.from(LocalTime.NOON, LocalTime.of(12, 30)).every(5, ChronoUnit.MINUTES)
.next(now).toEpochSecond() - now.toEpochSecond(),
TimeUnit.SECONDS
);
executor.schedule(
() -> {
System.out.println("TASK 'every day 12:00' EXECUTED");
// re-schedule if needed
},
Schedule.at(LocalTime.NOON).everyDay()
.next(now).toEpochSecond() - now.toEpochSecond(),
TimeUnit.SECONDS
);
```> NOTE #1: Be careful with [java.util.TimerTask::cancel()](http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/TimerTask.java#l119) (if you choose to go with java.util.Timer).
> NOTE #2: If you are thinking of using java.util.concurrent.DelayQueue - keep in mind that remove() operation is O(n).
> NOTE #3: If you have a huge number of scheduled tasks >= 10th of thousands you might want to consider switching to
[Hierarchical Wheel Timer](https://pdfs.semanticscholar.org/0a14/2c84aeccc16b22c758cb57063fe227e83277.pdf)(s).## Development
```sh
git clone https://github.com/shyiko/skedule && cd skedule
./mvnw # shows how to build, test, etc. project
```## Legal
All code, unless specified otherwise, is licensed under the [MIT](https://opensource.org/licenses/MIT) license.
Copyright (c) 2017 Stanley Shyiko.