Class: DamageControl::Poller

Inherits:
Object
  • Object
show all
Defined in:
lib/damagecontrol/poller.rb

Overview

Polls all projects in intervals.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sleeptime = 60, &proc) ⇒ Poller

Creates a new poller. Takes a block that will receive |project, changesets| each time new changesets are found in a polled project



13
14
15
16
17
# File 'lib/damagecontrol/poller.rb', line 13

def initialize(sleeptime=60, &proc)
  @projects = []
  @sleeptime = sleeptime
  @proc = proc
end

Instance Attribute Details

#projectsObject (readonly)

Returns the value of attribute projects.



8
9
10
# File 'lib/damagecontrol/poller.rb', line 8

def projects
  @projects
end

Instance Method Details

#add_all_projectsObject

Adds all projects



66
67
68
69
70
# File 'lib/damagecontrol/poller.rb', line 66

def add_all_projects
  Project.find_all.each do |project|
    add_project(project)
  end
end

#add_project(project) ⇒ Object

Adds a project to poll. If the project is already added it is replaced with then new one, otherwise appended to the end.



21
22
23
24
# File 'lib/damagecontrol/poller.rb', line 21

def add_project(project)
  index = @projects.index(project) || @projects.length
  @projects[index] = project
end

#pollObject

Polls all registered projects and persists RSS, changesets and diffs to disk. If a block is passed, the project and the changesets will be yielded to the block for each new changesets object.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/damagecontrol/poller.rb', line 29

def poll
  @projects.each do |project|
    begin
      if(project.scm_exists?)
        project.poll do |changesets|
          if(changesets.empty?)
            Log.info "No changesets for #{project.name}"
          else
            @proc.call(project, changesets)
          end
        end
      end
    rescue => e
      $stderr.puts "Error polling #{project.name}"
      $stderr.puts e.message
      $stderr.puts "  " + e.backtrace.join("  \n")
    end
  end
end

#startObject

Runs poll in a separate thread.



50
51
52
53
54
55
56
57
58
# File 'lib/damagecontrol/poller.rb', line 50

def start
  add_all_projects
  @t = Thread.new do
    while(true)
      poll
      sleep(@sleeptime)
    end
  end
end

#stopObject

Stops thread after a start.



61
62
63
# File 'lib/damagecontrol/poller.rb', line 61

def stop
  @t.kill if @t && @t.alive?
end