schedule.cr
Schedule
Schedule is a Crystal shard that provides a clear DSL to write periodic or scheduled tasks. It has the ability to stop or retry the job whenever is necessary, with proper ways to handle exceptions. See usage examples in examples folder.
Installation
Add this to your application's shard.yml
:
dependencies:
schedule:
github: hugoabonizio/schedule.cr
Usage
require "schedule"
# Print "Hello!" each 2 seconds
Schedule.every(2.seconds) do
puts "Hello!"
end
# Set a default exception handler
Schedule.exception_handler do |ex|
puts "Exception recued! #{ex.message}"
end
# Stop or retry a task
Schedule.every(100.milliseconds) do
begin
count += computed_value
rescue
Schedule.retry
end
Schedule.stop if count >= MAX_VALUE
end
# Execute a task after X interval
Schedule.after(2.seconds) do
puts "Hi!"
end
Scheduled tasks can be isolated having its own runner:
runner = Schedule::Runner.new
runner.every(100.milliseconds) do
Schedule.stop if condition
end
runner.exception_handler do |ex|
puts ex.message
end
Flow controw
A task can be stopped or retried using Schedule.stop
and Schedule.retry
respectively.
Schedule.every(10.seconds) do
result = try_to_update
Schedule.retry if result == -1
Schedule.stop if updates >= MAX_COUNT
end
Exception handlers
You can use the Schedule.exception_handler do ... end
form to set an exception handler or directly pass a proc to the Schedule.exception_handler
class property.
handler = ->(ex : Exception) { puts "Exception recued! #{ex.message}" }
Schedule.exception_handler = handler
Contributing
- Fork it ( https://github.com/hugoabonizio/schedule.cr/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- hugoabonizio Hugo Abonizio - creator, maintainer
Repository
schedule.cr
Owner
Statistic
- 0
- 0
- 0
- 0
- 0
- over 6 years ago
- June 5, 2018
License
MIT License
Links
Synced at
Thu, 07 Nov 2024 14:13:55 GMT
Languages