module Evrone module CI class Router module Middleware UpdateBuildStatus = Struct.new(:app) do include Helper::Logger STARTED = 2 FINISHED = 3 BROKEN = 4 FAILED = 5 def call(env) update_status env.build, STARTED rs = -1 begin rs = app.call env rescue Exception => e logger.error("ERROR: #{e.inspect}\n BACKTRACE:\n#{e.backtrace.map{|i| " #{i}" }.join("\n")}") end case when rs == 0 update_status env.build, FINISHED when rs > 0 update_status env.build, BROKEN when rs < 0 update_status env.build, FAILED end rs end private def update_status(build, status) publish_status create_message(build, status), status end def create_message(build, status) tm = Time.now attributes = { build_id: build.message.id, status: status, tm: tm.to_i, tm_usec: tm.usec, matrix: build.matrix || [], jobs_count: build.jobs_count || 0, } if build.commit_info attributes.merge!( commit_author: build.commit_info.author, commit_author_email: build.commit_info.email, commit_sha: build.commit_info.sha, commit_message: build.commit_info.message ) end Message::BuildStatus.new attributes end def publish_status(message, status) logger.info "delivered build status #{message.inspect}" BuildStatusConsumer.publish message end end end end end end