Skip to content

Commit

Permalink
Merge branch 'master' into gemification
Browse files Browse the repository at this point in the history
Conflicts:
	README.md
	app.rb
	config/mustacci.example.yml
	lib/mustacci.rb
	lib/mustacci/worker.rb
  • Loading branch information
iain committed Jun 22, 2012
2 parents 216e952 + f505749 commit b05e1f6
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 48 deletions.
31 changes: 22 additions & 9 deletions lib/mustacci/build.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -67,28 +67,41 @@ def elapsed
end end


def success! def success!
self.document['completed'] = true document = reload
self.document['completed_at'] = Time.now document['completed'] = true
self.document['success'] = true document['completed_at'] = Time.now
self.document.save document['success'] = true
document.save
end end


def failure! def failure!
self.document['completed'] = true document = reload
self.document['completed_at'] = Time.now document['completed'] = true
self.document['success'] = false document['completed_at'] = Time.now
self.document.save document['success'] = false
document.save
end

def complete!
document = reload
document['completed'] = true
document['completed_at'] = Time.now
document.save
end end


def save_log(log) def save_log(log)
# We need to reload the document to prevent a 409 Conflict error # We need to reload the document to prevent a 409 Conflict error
reloaded_document = database.get(self.document['_id']) reloaded_document = reload


database.connection.put_attachment(reloaded_document, "output.html", log, content_type: "text/html") database.connection.put_attachment(reloaded_document, "output.html", log, content_type: "text/html")
end end


private private


def reload
database.get(self.document['_id'])
end

def database def database
@database ||= Mustacci::Database.new @database ||= Mustacci::Database.new
end end
Expand Down
3 changes: 2 additions & 1 deletion lib/mustacci/database.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,9 +1,10 @@
require 'mustacci'
require 'couchrest' require 'couchrest'


module Mustacci module Mustacci
class Database class Database


DATABASE = 'http://localhost:5984/mustacci' DATABASE = "http://#{Mustacci.config.couchdb.hostname}:5984/mustacci"


attr_reader :connection attr_reader :connection


Expand Down
60 changes: 50 additions & 10 deletions lib/mustacci/worker.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,24 +61,46 @@ def process(data)


output = '' output = ''


PTY.spawn "./script/runner #{@build.id}" do |read, write, pid| notify_websocket


read.each_line do |line| begin
line = clean(line) PTY.spawn "./script/runner #{@build.id}" do |read, write, pid|
write_to_websocket(line) begin
output << line line = ''

read.each_char do |char|
line << char

# Send output to web socket per line, not per character
if char == "\n"
line = clean(line)
write_to_websocket(line)
output << line
line = ''
end
end
rescue Errno::EIO
# This "error" is raised when the child process is done sending I/O
# to the pty. For some reason Ruby does not handle this standard
# behavior very well.
#
# See: http://stackoverflow.com/questions/1154846/continuously-read-from-stdout-of-external-process-in-ruby
end
end end
rescue PTY::ChildExited
info "The runner process started in Mustacci::Worker exited"
ensure
@build.complete! unless @build.completed?
@build.save_log(output)
end end


@build.save_log(output)

info "Done building for build id: #{@build.id}" info "Done building for build id: #{@build.id}"
end end


private

def find_or_create_project def find_or_create_project
project = database.view('projects/by_name', key: repository_name) project = database.view('projects/by_name', key: repository_name)
info repository_name
info project.inspect


if project.any? if project.any?
project = project.first['value'] project = project.first['value']
Expand All @@ -88,7 +110,21 @@ def find_or_create_project
project project
end end


Project.new(project) Mustacci::Project.new(project)
end

def create_build(project_id)
build = {
type: 'build',
project_id: project_id,
payload_id: @payload.id,
success: false,
completed: false,
started_at: Time.now
}

database.save(build)
Mustacci::Build.load(build['_id'])
end end


def create_build(project_id) def create_build(project_id)
Expand Down Expand Up @@ -120,6 +156,10 @@ def socket
@ws ||= URI.parse("http://#{configuration.hostname}:#{configuration.websocket_port}/faye") @ws ||= URI.parse("http://#{configuration.hostname}:#{configuration.websocket_port}/faye")
end end


def notify_websocket
write_to_websocket("START")
end

def channels def channels
[ [
"/build/#{@build.id}", "/build/#{@build.id}",
Expand Down
18 changes: 18 additions & 0 deletions views/_faye_channel.slim
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,18 @@
javascript:
var client = new Faye.Client("#{{faye_uri}}");
var channel = [ '/build', "#{channel_id}" ].join('/');
var subscription = client.subscribe(channel, function(message) {
$('#console span.empty').remove();
var line = message.text;
if(line == "START") {
$("#console").empty();
} else {
// Show the line
$('#console').append(line);
// Scroll to the bottom
$(document).scrollTop($(document).height());
}
});
15 changes: 1 addition & 14 deletions views/build.slim
Original file line number Original file line Diff line number Diff line change
@@ -1,18 +1,5 @@
- if @build.running? - if @build.running?
javascript: == faye_channel @build.id
var client = new Faye.Client("#{faye_uri}");
var channel = [ '/build', "#{@build.id}" ].join('/');
var subscription = client.subscribe(channel, function(message) {
$('#console span.empty').remove();
var line = message.text;
// Show the line
$('#console').append(line);
// Scroll to the bottom
$(document).scrollTop($(document).height())
});


.row .row
.span12 .span12
Expand Down
15 changes: 1 addition & 14 deletions views/project.slim
Original file line number Original file line Diff line number Diff line change
@@ -1,17 +1,4 @@
javascript: == faye_channel @project.id
var client = new Faye.Client("#{faye_uri}");
var channel = [ '/build', "#{@project.id}" ].join('/');
var subscription = client.subscribe(channel, function(message) {
$('#console span.empty').remove();
var line = message.text;
// Show the line
$('#console').append(line);
// Scroll to the bottom
$(document).scrollTop($(document).height())
});


.row .row
.span12 .span12
Expand Down

0 comments on commit b05e1f6

Please sign in to comment.