Parent

Included Modules

Class/Module Index [+]

Quicksearch

Cinch::Timer

Timers are used for executing code in the future, either repeatedly or only once.

In Cinch, two ways for creating timers are available:

@see Helpers#Timer For dynamically creating timers @see Plugin::ClassMethods#timer For declaring timers in plugins @note It is possible to directly create instances of this class,

but the referenced methods should suffice.

@since 2.0.0

Attributes

block[R]

@return [Proc]

bot[R]

@return [Bot]

interval[RW]

@return [Numeric] The interval (in seconds) of the timer

shots[RW]

@return [Integer] The remaining number of shots before this timer

will stop. This value will automatically reset after
restarting the timer.
started[R]

@return [Boolean]

started?[R]

@return [Boolean]

thread_group[R]

@return [ThreadGroup] @api private

threaded[RW]

@return [Boolean] If true, each invocation will be

executed in a thread of its own.
threaded?[RW]

@return [Boolean] If true, each invocation will be

executed in a thread of its own.

Public Class Methods

new(bot, options, &block) click to toggle source

@param [Bot] bot The instance of {Bot} the timer is associated

with

@option options [Numeric] :interval The interval (in seconds) of

the timer

@option options [Integer] :shots (Float::INFINITY) How often should the

timer fire?

@option options [Boolean] :threaded (true) If true, each invocation will be

executed in a thread of its own.

@option options [Boolean] :start_automatically (true) If true,

the timer will automatically start after the bot finished
connecting.

@option options [Boolean] :stop_automaticall (true) If true, the

timer will automatically stop when the bot disconnects.
# File lib/cinch/timer.rb, line 64
def initialize(bot, options, &block)
  options = {:threaded => true, :shots => Float::INFINITY, :start_automatically => true, :stop_automatically => true}.merge(options)

  @bot        = bot
  @interval   = options[:interval].to_f
  @threaded   = options[:threaded]
  @orig_shots = options[:shots]
  # Setting @shots here so the attr_reader won't return nil
  @shots      = @orig_shots
  @block      = block

  @started = false
  @thread_group = ThreadGroup.new

  if options[:start_automatically]
    @bot.on :connect, //, self do |m, timer|
      timer.start
    end
  end

  if options[:stop_automatically]
    @bot.on :disconnect, //, self do |m, timer|
      timer.stop
    end
  end
end

Public Instance Methods

start() click to toggle source

Start the timer

@return [void]

# File lib/cinch/timer.rb, line 99
def start
  return if @started

  @bot.loggers.debug "[timer] Starting timer #{self}"

  @shots = @orig_shots

  @thread_group.add Thread.new {
    while @shots > 0 do
      sleep @interval
      if threaded?
        Thread.new do
          rescue_exception do
            @block.call
          end
        end
      else
        rescue_exception do
          @block.call
        end
      end

      @shots -= 1
    end
  }

  @started = true
end
stop() click to toggle source

Stop the timer

@return [void]

# File lib/cinch/timer.rb, line 131
def stop
  return unless @started

  @bot.loggers.debug "[timer] Stopping timer #{self}"

  @thread_group.list.each { |thread| thread.kill }
  @started = false
end
stopped?() click to toggle source

@return [Boolean]

# File lib/cinch/timer.rb, line 92
def stopped?
  !@started
end
to_s() click to toggle source

@return [String]

# File lib/cinch/timer.rb, line 141
def to_s
  "<Cinch::Timer %s/%s shots, %ds interval, %sthreaded, %sstarted, block: %s>" % [@orig_shots - @shots, @orig_shots, @interval, @threaded ? "" : "not ", @started ? "" : "not ", @block]
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.