Class: Backport::Server::Interval

Inherits:
Base
  • Object
show all
Defined in:
lib/backport/server/interval.rb

Overview

A Backport periodical interval server.

Instance Method Summary collapse

Methods inherited from Base

#start, #started?, #stop, #stopped?, #stopping

Constructor Details

#initialize(period, &block) {|| ... } ⇒ Interval

Returns a new instance of Interval.

Parameters:

  • period (Float)

    The interval time in seconds.

  • block (Proc)

    The proc to run on each interval.

Yield Parameters:



9
10
11
12
13
# File 'lib/backport/server/interval.rb', line 9

def initialize period, &block
  @period = period
  @block = block
  @last_time = Time.now
end

Instance Method Details

#startingObject



15
16
17
# File 'lib/backport/server/interval.rb', line 15

def starting
  @last_time = Time.now
end

#tickObject



19
20
21
22
23
24
# File 'lib/backport/server/interval.rb', line 19

def tick
  now = Time.now
  return unless now - @last_time >= @period
  @block.call self
  @last_time = now
end