Module: Droid::EMTimerUtils::ClassMethods

Defined in:
lib/droid/em.rb

Instance Method Summary collapse

Instance Method Details

#em_exception(e) ⇒ Object



15
16
17
18
# File 'lib/droid/em.rb', line 15

def em_exception(e)
  msg = format_em_exception(e)
  log.error "[EM.timer] #{msg}", :exception => e
end

#format_em_exception(e) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/droid/em.rb', line 20

def format_em_exception(e)
  # avoid backtrace in /usr or vendor if possible
  system, app = e.backtrace.partition { |b| b =~ /(^\/usr\/|vendor)/ }
  reordered_backtrace = app + system

  # avoid "/" as the method name (we want the controller action)
  row = 0
  row = 1 if reordered_backtrace[row].match(/in `\/'$/)

  # get file and method name
  begin
    file, method = reordered_backtrace[row].match(/(.*):in `(.*)'$/)[1..2]
    file.gsub!(/.*\//, '')
    "#{e.class} in #{file} #{method}: #{e.message}"
  rescue
    "#{e.class} in #{e.backtrace.first}: #{e.message}"
  end
end

#periodic_timer(duration, now = false, &blk) ⇒ Object

Add a periodic timer. If the now argument is true, run the block immediately in addition to scheduling the periodic timer.



46
47
48
49
# File 'lib/droid/em.rb', line 46

def periodic_timer(duration, now=false, &blk)
  timer(1, &blk) if now
  EM.add_periodic_timer(duration) { trap_exceptions(&blk) }
end

#timer(duration, &blk) ⇒ Object

One-shot timer



40
41
42
# File 'lib/droid/em.rb', line 40

def timer(duration, &blk)
  EM.add_timer(duration) { trap_exceptions(&blk) }
end

#trap_exceptionsObject

Trap exceptions leaving the block and log them. Do not re-raise



9
10
11
12
13
# File 'lib/droid/em.rb', line 9

def trap_exceptions
  yield
rescue => e
  em_exception(e)
end