Class: Always
- Inherits:
-
Object
- Object
- Always
- Defined in:
- lib/always.rb
Overview
Always.
In order to start five threads performing the same piece of code over and over again, with a 60-seconds pause between cycles, do this:
require 'always'
a = Always.new(5) do
puts 'Hello, world!'
end
a.start!(60)
Then, in order to stop them all together:
a.stop!
It’s possible to get a quick summary of the thread pool, by calling to_s. The result will be a “T/C/E” string, where T is the total number of currently running threads, C is the total number of all cycles so far, and E is the total number of all errors seen so far.
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2024-2025 Yegor Bugayenko
- License
-
MIT
Constant Summary collapse
- VERSION =
The version of the framework.
'0.0.0'
Instance Attribute Summary collapse
-
#backtraces ⇒ Array<Exception>
readonly
Get the array of most recent exception backtraces.
Instance Method Summary collapse
-
#initialize(total, max_backtraces: 32, name: "always-#{SecureRandom.hex(4)}", &block) ⇒ Always
constructor
Constructor.
-
#on_error(&block) ⇒ Always
What to do when an exception occurs?.
-
#start!(pause = 0) ⇒ Object
Start them all and let them run forever (until the
stopmethod is called). -
#stop! ⇒ Object
Stop them all.
-
#to_s ⇒ String
Represent its internal state as a string.
Constructor Details
#initialize(total, max_backtraces: 32, name: "always-#{SecureRandom.hex(4)}", &block) ⇒ Always
Constructor.
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/always.rb', line 49 def initialize(total, max_backtraces: 32, name: "always-#{SecureRandom.hex(4)}", &block) raise "The number of threads (#{total}) must be positive" unless total.positive? @total = total @block = block @on_error = nil @name = name @threads = [] @backtraces = [] @cycles = Concurrent::Atom.new(0) @errors = Concurrent::Atom.new(0) @max_backtraces = max_backtraces end |
Instance Attribute Details
#backtraces ⇒ Array<Exception> (readonly)
Get the array of most recent exception backtraces.
40 41 42 |
# File 'lib/always.rb', line 40 def backtraces @backtraces end |
Instance Method Details
#on_error(&block) ⇒ Always
What to do when an exception occurs?
Call it like this (the e provided is the exception and i is the number of the thread where it occurred):
a = Always.new(5)
a.on_error do |e, i|
puts e.
end
If the block that you provided will also throw an error, it will simply be ignored (not logged anywhere, just ignored!)
76 77 78 79 |
# File 'lib/always.rb', line 76 def on_error(&block) @on_error = block self end |
#start!(pause = 0) ⇒ Object
Start them all and let them run forever (until the stop method is called).
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/always.rb', line 83 def start!(pause = 0) raise 'It is running now, call .stop() first' unless @threads.empty? (0..(@total - 1)).each do |i| t = Thread.new do body(pause) end t.name = "#{@name}-#{i + 1}" @threads[i] = t end end |
#stop! ⇒ Object
Stop them all.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/always.rb', line 97 def stop! raise 'It is not running now, call .start() first' if @threads.empty? @threads.delete_if do |t| t.kill sleep(0.001) while t.alive? true end @cycles.swap { |_| 0 } @errors.swap { |_| 0 } end |
#to_s ⇒ String
Represent its internal state as a string.
117 118 119 |
# File 'lib/always.rb', line 117 def to_s "#{@threads.size}/#{@cycles.value}/#{@errors.value}" end |