Class: GitHubEventWatcher::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/github-event-watcher/watcher.rb

Constant Summary collapse

NOTIFY_MESSAGE =
"X"

Instance Method Summary collapse

Constructor Details

#initialize(state, logger) ⇒ Watcher

Returns a new instance of Watcher.



25
26
27
28
29
30
31
# File 'lib/github-event-watcher/watcher.rb', line 25

def initialize(state, logger)
  @repositories = []
  @state = state
  @logger = logger
  @running = true
  @notify_pipe = IO.pipe
end

Instance Method Details

#add_repository(name) ⇒ Object



33
34
35
36
# File 'lib/github-event-watcher/watcher.rb', line 33

def add_repository(name)
  @logger.info("[watcher][repository] add: <#{name}>")
  @repositories << name
end

#stopObject



64
65
66
67
# File 'lib/github-event-watcher/watcher.rb', line 64

def stop
  @running = false
  @notify_pipe[1].write(NOTIFY_MESSAGE)
end

#watchObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/github-event-watcher/watcher.rb', line 38

def watch
  i = 0
  while @running
    name = @repositories[i]
    events = fetch_events(name)
    processed_event_id = @state.processed_event_id(name)
    events = remove_processed_events(events, processed_event_id)
    @logger.info("[watcher][watch][#{name}] target events: <#{events.size}>")
    sorted_events = events.sort_by do |event|
      event.id
    end
    sorted_events.each do |event|
      yield(event)
    end
    latest_event = sorted_events.last
    if latest_event
      @logger.info("[watcher][watch][#{name}] last processed event ID: " +
                   "<#{latest_event.id}>")
      @state.update_processed_event_id(name, latest_event.id)
    end
    readables, = IO.select([@notify_pipe[0]], nil, nil, 60)
    readables[0].read(NOTIFY_MESSAGE.size) if readables
    i = (i + 1) % @repositories.size
  end
end