https://github.com/yegor256/always
A simple Ruby framework that spins a loop forever, in a background thread
https://github.com/yegor256/always
concurrency ruby ruby-gem thread threading
Last synced: 6 months ago
JSON representation
A simple Ruby framework that spins a loop forever, in a background thread
- Host: GitHub
- URL: https://github.com/yegor256/always
- Owner: yegor256
- License: mit
- Created: 2024-05-31T10:51:00.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-09-16T13:36:41.000Z (almost 2 years ago)
- Last Synced: 2024-09-17T13:03:17.825Z (almost 2 years ago)
- Topics: concurrency, ruby, ruby-gem, thread, threading
- Language: Ruby
- Homepage: https://rubygems.org/gems/always
- Size: 69.3 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Runs a Background Loop Forever
[](https://www.rultor.com/p/yegor256/always)
[](https://www.jetbrains.com/ruby/)
[](https://github.com/yegor256/always/actions/workflows/rake.yml)
[](https://www.0pdd.com/p?name=yegor256/always)
[](https://badge.fury.io/rb/always)
[](https://codecov.io/github/yegor256/always?branch=master)
[](https://rubydoc.info/github/yegor256/always/master/frames)
[](https://hitsofcode.com/view/github/yegor256/always)
[](https://github.com/yegor256/always/blob/master/LICENSE.txt)
This simple Ruby gem helps you run a loop forever, in a background thread.
```ruby
require 'always'
# Prepare, with five threads and a block:
a = Always.new(5) do
puts "I'm alive"
end
# Start them all together spinning forever with 30-seconds delay between cycles:
a.start!(30)
# Stop them all together:
a.stop!
```
You may be interested to get the backtraces of the exceptions that
happened most recently:
```ruby
# Keep the last 10 error backtraces in memory:
a = Always.new(5, max_backtraces: 10)
# Set an error handler:
a.on_error do |exception, thread_id|
puts "Error in thread #{thread_id}: #{exception.message}"
end
# Start them:
a.start!
# Retrieve the backtraces:
p a.backtraces
```
That's it.
## How to contribute
Read
[these guidelines](https://www.yegor256.com/2014/04/15/github-guidelines.html).
Make sure your build is green before you contribute
your pull request. You will need to have
[Ruby](https://www.ruby-lang.org/en/) 3.0+ and
[Bundler](https://bundler.io/) installed. Then:
```bash
bundle update
bundle exec rake
```
If it's clean and you don't see any error messages, submit your pull request.