Class: BuildBuddy::Scheduler

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Celluloid::Internals::Logger
Defined in:
lib/build_buddy/scheduler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScheduler

Returns a new instance of Scheduler.



14
15
16
17
18
# File 'lib/build_buddy/scheduler.rb', line 14

def initialize()
  @build_queue = Queue.new
  @done_queue = Queue.new
  @build_timer = nil
end

Instance Attribute Details

#active_buildObject (readonly)

Returns the value of attribute active_build.



12
13
14
# File 'lib/build_buddy/scheduler.rb', line 12

def active_build
  @active_build
end

Instance Method Details

#on_build_completed(build_data) ⇒ Object



103
104
105
106
# File 'lib/build_buddy/scheduler.rb', line 103

def on_build_completed(build_data)
  @active_build = nil
  @done_queue.push(build_data)
end

#on_build_intervalObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/build_buddy/scheduler.rb', line 54

def on_build_interval
  if @active_build.nil?
    if @build_queue.length > 0
      build_data = @build_queue.pop()
      @active_build = build_data
      Celluloid::Actor[:recorder].async.record_build_data(build_data)
      if build_data.build_type == :pull_request
        Celluloid::Actor[:gitter].async.set_status(
            build_data.repo_full_name, build_data.repo_sha, :pending, "This build has started")
      end
      Celluloid::Actor[:builder].async.start_build(build_data)
    elsif @done_queue.length > 0
      build_data = @done_queue.pop
      status_message = build_data.termination_type == :killed ? "was stopped" : build_data.exit_code != 0 ? "failed" : "succeeded"
      status_message += '. '

      if build_data.build_type == :pull_request
        message = "The buddy build #{status_message}"
        Celluloid::Actor[:gitter].async.set_status(
            build_data.repo_full_name, build_data.repo_sha,
            build_data.termination_type == :killed ? :failure : build_data.exit_code != 0 ? :error : :success,
            message)
        info "Pull request build #{status_message}"
      else
        status_message += "Log file at #{Config.server_base_uri + '/log/' + build_data._id.to_s}."
        if build_data.build_type == :master
          message = "A build of the `master` branch #{status_message}"
          info "`master` branch build #{status_message}"
        else
          message = "A build of the `#{build_data.build_version}` branch #{status_message}"
          info "Release branch build #{status_message}"
        end
        Celluloid::Actor[:slacker].async.notify_channel(message)
      end
      Celluloid::Actor[:recorder].async.update_build_data(build_data)
    else
      @build_timer.cancel
      @build_timer = nil
      info "Build timer stopped"
    end
  else
    # Make sure that the build has not run too long and kill if necessary
    start_time = @active_build.start_time
    if !start_time.nil? and Time.now.utc - start_time > Config.kill_build_after_mins * 60
      Celluloid::Actor[:builder].async.stop_build()
    end
  end
end

#queue_a_build(build_data) ⇒ Object



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

def queue_a_build(build_data)
  @build_queue.push(build_data)

  case build_data.build_type
    when :pull_request
      Celluloid::Actor[:gitter].async.set_status(
          build_data.repo_full_name, build_data.repo_sha, :pending, "This build is in the queue")
      info "Pull request build queued"
    when :master
      info "`master` branch build queued"
    when :release
      info "Release branch build queued"
  end

  if @build_timer.nil?
    @build_timer = every(5) { on_build_interval }
    info "Build timer started"
  end
end

#queue_lengthObject



40
41
42
# File 'lib/build_buddy/scheduler.rb', line 40

def queue_length
  @build_queue.length
end

#stop_buildObject



44
45
46
47
48
49
50
51
52
# File 'lib/build_buddy/scheduler.rb', line 44

def stop_build
  # Centralize stopping bulids here in case we allow multiple active builders in future
  unless @active_build.nil?
    Celluloid::Actor[:builder].stop_build
    true
  else
    false
  end
end