Module: Eventbox::Timer

Extended by:
Boxable
Defined in:
lib/eventbox/timer.rb

Overview

Simple timer services for Eventboxes

This module can be included into Eventbox classes to add simple timer functions.

class MyBox < Eventbox
  include Eventbox::Timer

  async_call def init
    super     # Make sure Timer#init is called
    timer_after(1) do
      puts "one second elapsed"
    end
  end
end

MyBox.new     # Schedule the alarm after 1 sec
sleep 2       # Wait for the timer to be triggered

The main functions are timer_after and timer_every. They schedule asynchronous calls to the given block:

timer_after(3.0) do
  # executed once after 3 seconds
end

timer_every(1.5) do
  # executed repeatedly every 1.5 seconds
end

Both functions return an Alarm object which can be used to cancel the alarm through timer_cancel.

Timer always uses one action thread per Eventbox object, regardless of the number of scheduled timers. All alarms are called from this thread. timer_after, timer_every and timer_cancel can be used within the event scope, in actions and from external scope. Alarms defined within the event scope must be non-blocking, as any other code in the event scope. Alarms defined in action or external scope should also avoid blocking code, otherwise one alarm can delay the next alarm.

Note: Each object that includes the Timer module must be explicit terminated by #shutdown!. It is (currently) not freed by the garbarge collector.

Defined Under Namespace

Classes: Alarm, InternalError, OneTimeAlarm, Reload, RepeatedAlarm

Method Summary

Methods included from Boxable

action, async_call, attr_accessor, attr_reader, attr_writer, sync_call, yield_call