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



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

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

Instance Attribute Details

#projectsObject (readonly)

Returns the value of attribute projects.



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

def projects
  @projects
end

Instance Method Details

#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.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/damagecontrol/poller.rb', line 22

def poll
  Log.info "Starting polling cycle"
  Project.find_all.each do |project|
    Log.info "Polling #{project.name}"
    begin
      if(project.scm_exists?)
        Log.info "Polling #{project.name}"
        project.poll do |changesets|
          if(changesets.empty?)
            Log.info "No changesets for #{project.name}"
          else
            @proc.call(project, changesets)
          end
        end
      else
        Log.info "Not polling #{project.name} since its scm doesn't exist"
      end
    rescue => e
      Log.error "Error polling #{project.name}"
      Log.error  e.message
      Log.error "  " + e.backtrace.join("  \n")
    end
  end
end

#startObject

Runs poll in a separate thread.



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

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

#stopObject

Stops thread after a start.



59
60
61
# File 'lib/damagecontrol/poller.rb', line 59

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