Permalink
Browse files

Merge branch 'master' into gemification

Conflicts:
	README.md
	app.rb
	config/mustacci.example.yml
	lib/mustacci.rb
	lib/mustacci/worker.rb
  • Loading branch information...
2 parents 216e952 + f505749 commit b05e1f6954196f180b25fa1ce0c0f6af47b3fb50 @iain iain committed Jun 22, 2012
Showing with 94 additions and 48 deletions.
  1. +22 −9 lib/mustacci/build.rb
  2. +2 −1 lib/mustacci/database.rb
  3. +50 −10 lib/mustacci/worker.rb
  4. +18 −0 views/_faye_channel.slim
  5. +1 −14 views/build.slim
  6. +1 −14 views/project.slim
View
@@ -67,28 +67,41 @@ def elapsed
end
def success!
- self.document['completed'] = true
- self.document['completed_at'] = Time.now
- self.document['success'] = true
- self.document.save
+ document = reload
+ document['completed'] = true
+ document['completed_at'] = Time.now
+ document['success'] = true
+ document.save
end
def failure!
- self.document['completed'] = true
- self.document['completed_at'] = Time.now
- self.document['success'] = false
- self.document.save
+ document = reload
+ document['completed'] = true
+ document['completed_at'] = Time.now
+ document['success'] = false
+ document.save
+ end
+
+ def complete!
+ document = reload
+ document['completed'] = true
+ document['completed_at'] = Time.now
+ document.save
end
def save_log(log)
# 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")
end
private
+ def reload
+ database.get(self.document['_id'])
+ end
+
def database
@database ||= Mustacci::Database.new
end
View
@@ -1,9 +1,10 @@
+require 'mustacci'
require 'couchrest'
module Mustacci
class Database
- DATABASE = 'http://localhost:5984/mustacci'
+ DATABASE = "http://#{Mustacci.config.couchdb.hostname}:5984/mustacci"
attr_reader :connection
View
@@ -61,24 +61,46 @@ def process(data)
output = ''
- PTY.spawn "./script/runner #{@build.id}" do |read, write, pid|
+ notify_websocket
- read.each_line do |line|
- line = clean(line)
- write_to_websocket(line)
- output << line
+ begin
+ PTY.spawn "./script/runner #{@build.id}" do |read, write, pid|
+ begin
+ 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
+ rescue PTY::ChildExited
+ info "The runner process started in Mustacci::Worker exited"
+ ensure
+ @build.complete! unless @build.completed?
+ @build.save_log(output)
end
- @build.save_log(output)
-
info "Done building for build id: #{@build.id}"
end
+ private
+
def find_or_create_project
project = database.view('projects/by_name', key: repository_name)
- info repository_name
- info project.inspect
if project.any?
project = project.first['value']
@@ -88,7 +110,21 @@ def find_or_create_project
project
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
def create_build(project_id)
@@ -120,6 +156,10 @@ def socket
@ws ||= URI.parse("http://#{configuration.hostname}:#{configuration.websocket_port}/faye")
end
+ def notify_websocket
+ write_to_websocket("START")
+ end
+
def channels
[
"/build/#{@build.id}",
View
@@ -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());
+ }
+ });
View
@@ -1,18 +1,5 @@
- if @build.running?
- javascript:
- 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())
- });
+ == faye_channel @build.id
.row
.span12
View
@@ -1,17 +1,4 @@
-javascript:
- 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())
- });
+== faye_channel @project.id
.row
.span12

0 comments on commit b05e1f6

Please sign in to comment.