Runs a Background Loop Forever
This simple Ruby gem helps you run a loop forever, in a background thread.
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:
# 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.backtracesThat's it.
How to contribute
Read these guidelines. Make sure your build is green before you contribute your pull request. You will need to have Ruby 3.0+ and Bundler installed. Then:
bundle update
bundle exec rakeIf it's clean and you don't see any error messages, submit your pull request.