class Driskell::Listen::Event::Loop

Attributes

config[R]
state[RW]
wait_thread[R]

Public Class Methods

new(config) click to toggle source
# File lib/driskell-listen/event/loop.rb, line 14
def initialize(config)
  @config = config
  @wait_thread = nil
  @state = :paused
  @reasons = ::Queue.new
end

Public Instance Methods

pause() click to toggle source
# File lib/driskell-listen/event/loop.rb, line 55
def pause
  # TODO: works?
  # fail NotImplementedError
end
paused?() click to toggle source
# File lib/driskell-listen/event/loop.rb, line 28
def paused?
  wait_thread && state == :paused
end
processing?() click to toggle source
# File lib/driskell-listen/event/loop.rb, line 32
def processing?
  return false if stopped?
  return false if paused?
  state == :processing
end
resume() click to toggle source
# File lib/driskell-listen/event/loop.rb, line 49
def resume
  fail Error::NotStarted if stopped?
  return unless wait_thread
  _wakeup(:resume)
end
setup() click to toggle source
# File lib/driskell-listen/event/loop.rb, line 38
def setup
  # TODO: use a Fiber instead?
  q = ::Queue.new
  @wait_thread = Internals::ThreadPool.add do
    _wait_for_changes(q, config)
  end

  Driskell::Listen::Logger.debug('Waiting for processing to start...')
  Timeout.timeout(5) { q.pop }
end
stopped?() click to toggle source
# File lib/driskell-listen/event/loop.rb, line 69
def stopped?
  !wait_thread
end
teardown() click to toggle source
# File lib/driskell-listen/event/loop.rb, line 60
def teardown
  return unless wait_thread
  if wait_thread.alive?
    _wakeup(:teardown)
    wait_thread.join
  end
  @wait_thread = nil
end
wakeup_on_event() click to toggle source
# File lib/driskell-listen/event/loop.rb, line 21
def wakeup_on_event
  return if stopped?
  return unless processing?
  return unless wait_thread.alive?
  _wakeup(:event)
end

Private Instance Methods

_nice_error(ex) click to toggle source
# File lib/driskell-listen/event/loop.rb, line 100
def _nice_error(ex)
  indent = "\n -- "
  msg = format(
    'exception while processing events: %s Backtrace:%s%s',
    ex,
    indent,
    ex.backtrace * indent
  )
  Driskell::Listen::Logger.error(msg)
end
_sleep(*args) click to toggle source
# File lib/driskell-listen/event/loop.rb, line 89
def _sleep(*args)
  Kernel.sleep(*args)
end
_wait_for_changes(ready_queue, config) click to toggle source
# File lib/driskell-listen/event/loop.rb, line 80
def _wait_for_changes(ready_queue, config)
  processor = Event::Processor.new(config, @reasons)

  _wait_until_resumed(ready_queue)
  processor.loop_for(config.min_delay_between_events)
rescue StandardError => ex
  _nice_error(ex)
end
_wait_until_resumed(ready_queue) click to toggle source
# File lib/driskell-listen/event/loop.rb, line 93
def _wait_until_resumed(ready_queue)
  self.state = :paused
  ready_queue << :ready
  sleep
  self.state = :processing
end
_wakeup(reason) click to toggle source
# File lib/driskell-listen/event/loop.rb, line 111
def _wakeup(reason)
  @reasons << reason
  wait_thread.wakeup
end