Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/soasme/nim-schedules
A Nim scheduler library that lets you kick off jobs at regular intervals.
https://github.com/soasme/nim-schedules
job-scheduler nim nim-lang scheduler
Last synced: 3 months ago
JSON representation
A Nim scheduler library that lets you kick off jobs at regular intervals.
- Host: GitHub
- URL: https://github.com/soasme/nim-schedules
- Owner: soasme
- License: mit
- Created: 2019-09-24T10:55:05.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-07-23T05:07:12.000Z (over 3 years ago)
- Last Synced: 2024-08-03T03:05:25.539Z (6 months ago)
- Topics: job-scheduler, nim, nim-lang, scheduler
- Language: Nim
- Homepage: https://soasme.com/nim-schedules/schedules.html
- Size: 165 KB
- Stars: 64
- Watchers: 6
- Forks: 5
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-nim - schedules - A Nim scheduler library that lets you kick off jobs at regular intervals. (Operating System / Processes)
README
# nim-schedules
A Nim scheduler library that lets you kick off jobs at regular intervals.
Read the [documentation](https://www.soasme.com/nim-schedules/schedules.html).
Features:
* Simple to use API for scheduling jobs.
* Support scheduling both async and sync procs.
* Lightweight and zero dependencies.## Getting Started
```bash
$ nimble install schedules
```## Usage
```nim
# File: scheduleExample.nim
import schedules, times, asyncdispatchschedules:
every(seconds=10, id="tick"):
echo("tick", now())every(seconds=10, id="atick", async=true):
echo("tick", now())
await sleepAsync(3000)
```1. Schedule thread proc every 10 seconds.
2. Schedule async proc every 10 seconds.Run:
```bash
nim c --threads:on -r scheduleExample.nim
```Note:
* Don't forget **`--threads:on`** when compiling your application.
* The library schedules all jobs at a regular interval, but it'll be impacted
by your system load.## Advance Usages
### Cron
You can use `cron` to schedule jobs using cron-like syntax.
```nim
import schedules, times, asyncdispatchschedules:
cron(minute="*/1", hour="*", day_of_month="*", month="*", day_of_week="*", id="tick"):
echo("tick", now())cron(minute="*/1", hour="*", day_of_month="*", month="*", day_of_week="*", id="atick", async=true):
echo("tick", now())
await sleepAsync(3000)
```1. Schedule thread proc every minute.
2. Schedule async proc every minute.### Throttling
By default, only one instance of the job is to be scheduled at the same time.
If a job hasn't finished but the next run time has come, the next job will
not be scheduled.You can allow more instances by specifying `throttle=`. For example:
```nim
import schedules, times, asyncdispatch, osschedules:
every(seconds=1, id="tick", throttle=2):
echo("tick", now())
sleep(2000)every(seconds=1, id="async tick", async=true, throttle=2):
echo("async tick", now())
await sleepAsync(4000)
```### Customize Scheduler
Sometimes, you want to run the scheduler in parallel with other libraries.
In this case, you can create your own scheduler by macro `scheduler` and
start it later.Below is an example of co-exist jester and nim-schedules in one process.
```nim
import times, asyncdispatch, schedules, jesterscheduler mySched:
every(seconds=1, id="sync tick"):
echo("sync tick, seconds=1 ", now())router myRouter:
get "/":
resp "It's alive!"proc main():
# start schedules
asyncCheck mySched.start()# start jester
let port = paramStr(1).parseInt().Port
let settings = newSettings(port=port)
var jester = initJester(myrouter, settings=settings)# run
jester.serve()when isMainModule:
main()
```### Set Start Time and End Time
You can limit the schedules running in a designated range of time by specifying
`startTime` and `endTime`.For example,
```nim
import schedules, times, asyncdispatch, osscheduler demoSetRange:
every(
seconds=1,
id="tick",
startTime=initDateTime(2019, 1, 1),
endTime=now()+initDuration(seconds=10)
):
echo("tick", now())when isMainModule:
waitFor demoSetRange.start()
```Parameters `startTime` and `endTime` can be used independently. For example,
you can set startTime only, or set endTime only.## ChangeLog
Released:
* v0.2.0, 22 Jul, 2021, New feature: cron.
* v0.1.2, 8 Jul, 2021, Bugfix: the first job schedule should be after startTime.
* v0.1.1, update metadata.
* v0.1.0, initial release.## License
Nim-schedules is based on MIT license.