class Thread::Every

An every runs the given block every given seconds, you can then get the value, check if the value is old and you can check how many seconds until the next run.

Constants

Cancel
Restart

Public Class Methods

finalizer(thread) click to toggle source

@private

# File lib/thread/every.rb, line 70
def self.finalizer(thread)
        proc {
                thread.raise Cancel.new
        }
end
new(every, &block) click to toggle source

Create an every with the given seconds and block.

# File lib/thread/every.rb, line 21
def initialize(every, &block)
        raise ArgumentError, 'no block given' unless block

        @every  = every
        @old    = true
        @mutex  = Mutex.new
        @thread = Thread.new {
                loop do
                        begin
                                value = block.call

                                @mutex.synchronize {
                                        @at        = Time.now
                                        @value     = value
                                        @old       = false
                                        @exception = nil
                                }
                        rescue Restart
                                next
                        rescue Exception => e
                                @mutex.synchronize {
                                        @at        = Time.now
                                        @exception = e
                                }

                                break if e.is_a? Cancel
                        end

                        cond.broadcast if cond?

                        begin
                                sleep @every
                        rescue Restart
                                next
                        rescue Cancel => e
                                @mutex.synchronize {
                                        @at        = Time.now
                                        @exception = e
                                }

                                break
                        end
                end
        }

        ObjectSpace.define_finalizer self, self.class.finalizer(@thread)
end

Public Instance Methods

called_at() click to toggle source

Gets the Time when the block was called.

# File lib/thread/every.rb, line 131
def called_at
        @mutex.synchronize {
                @at
        }
end
cancel() click to toggle source

Cancel the every, {#value} will yield a Cancel exception.

# File lib/thread/every.rb, line 84
def cancel
        @mutex.synchronize {
                @thread.raise Cancel.new('every cancelled')
        }

        self
end
cancelled?() click to toggle source

Check if the every has been cancelled.

# File lib/thread/every.rb, line 93
def cancelled?
        @mutex.synchronize {
                @exception.is_a? Cancel
        }
end
cancelled_at() click to toggle source

Checks when the every was cancelled.

# File lib/thread/every.rb, line 100
def cancelled_at
        if cancelled?
                @mutex.synchronize {
                        @at
                }
        end
end
every(seconds) click to toggle source

Change the number of seconds between each call.

# File lib/thread/every.rb, line 77
def every(seconds)
        @every = seconds

        restart
end
next_in() click to toggle source

Gets how many seconds are missing before another call.

# File lib/thread/every.rb, line 138
def next_in
        return if cancelled?

        @mutex.synchronize {
                @every - (Time.now - @at)
        }
end
old?() click to toggle source

Check if the every is old, after the first value call it becomes old, until another run of the block is gone)

# File lib/thread/every.rb, line 124
def old?
        @mutex.synchronize {
                @old
        }
end
restart() click to toggle source

Restart the every.

# File lib/thread/every.rb, line 109
def restart
        @mutex.synchronize {
                @thread.raise Restart.new
        }

        self
end
running?() click to toggle source

Check if the every is running.

# File lib/thread/every.rb, line 118
def running?
        !cancelled?
end
value(timeout = nil) click to toggle source

Gets the current every value.

# File lib/thread/every.rb, line 147
def value(timeout = nil)
        @mutex.synchronize {
                if @old
                        cond.wait(@mutex, *timeout)
                end

                @old = true

                if @exception
                        raise @exception
                else
                        @value
                end
        }
end
Also aliased as: ~
value!() click to toggle source

Gets the current every value, without blocking and waiting for the next call.

# File lib/thread/every.rb, line 167
def value!
        @mutex.synchronize {
                @old = true

                @value unless @exception
        }
end
~(timeout = nil)
Alias for: value

Private Instance Methods

cond() click to toggle source
# File lib/thread/every.rb, line 180
def cond
        @cond ||= ConditionVariable.new
end
cond?() click to toggle source
# File lib/thread/every.rb, line 176
def cond?
        instance_variable_defined? :@cond
end