exec
Exec
A lightweight library for executing blocks at specified intervals. This library aims to solve the following:
- Reliably run blocks at specified intervals regardless of the runtime duration of the block.
- Keep it light (avoid timezones and dependencies)
Installation
-
Add the dependency to your
shard.yml:dependencies: exec: github: mgomes/exec -
Run
shards install
Usage
require "exec"
Specify the execution intervals for your code blocks:
Exec.every(1.second) do
do_work
end
Exec.every(3.hours) do
do_work
end
Caveats
-
In the usage examples above, if the
do_workmethod takes longer to execute than the specified interval, the next interval will be executed before the existing code block has completed. Therefore, multiple versions ofdo_workwill be executing concurrently. If this is a concern for your code block, ensure you have enough margin built into the interval or that you've built in other safety measures to prevent corruption. -
You'll likely want to compile your applications with the
-Dpreview_mtflag to take advantage of parallel execution. You can then run your binary with theCRYSTAL_WORKERS=Nenvironment variable. SetNto the number of cores on your CPU.
exec
- 0
- 1
- 0
- 0
- 0
- almost 4 years ago
- January 20, 2022
MIT License
Fri, 28 Nov 2025 04:34:01 GMT