Skip to content

Commit

Permalink
Switch app.rb to use ExternalCommand and CleanedOutput classes
Browse files Browse the repository at this point in the history
This simplifies the `RebuilderJob#perform` method quite a bit, and means
that we're using tested classes, rather than just having a big ball of
code.
  • Loading branch information
chrismytton committed Feb 8, 2017
1 parent c98c8ff commit c28d349
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

require 'active_support/core_ext'
require_relative 'lib/external_command'
require_relative 'lib/cleaned_output'

configure :production do
require 'rollbar/middleware/sinatra'
Expand Down Expand Up @@ -49,36 +50,28 @@ def perform(country_slug, legislature_slug, source = nil)
branch_parts = [country_slug, legislature_slug, Time.now.to_i]
branch = branch_parts.join('-').parameterize
message = "#{country.name}: Refresh from upstream changes"
output = ''
child_status = nil
command = nil
with_git_repo.commit_changes_to_branch(branch, message) do
run('bundle install --quiet --jobs 4 --without test')
ExternalCommand.new(command: 'bundle install --quiet --jobs 4 --without test').run
Dir.chdir(File.dirname(legislature.popolo)) do
if source
output, child_status = run('bundle exec rake clean default 2>&1', 'REBUILD_SOURCE' => source)
else
output, child_status = run('bundle exec rake clobber default 2>&1')
end
command = ExternalCommand.new(
command: "bundle exec rake #{source ? 'clean' : 'clobber'} default 2>&1",
env: { 'REBUILD_SOURCE' => source }
).run
end
end

with_git_repo.commit_changes_to_branch(branch, 'Refresh countries.json') do
run('bundle exec rake countries.json', 'EP_COUNTRY_REFRESH' => country_slug)
end

unless child_status && child_status.success?
Rollbar.error("Failed to build #{country.name} - #{legislature.name}\n\n" + output)
unless command.success?
Rollbar.error("Failed to build #{country.name} - #{legislature.name}\n\n" + command.output)
return
end
if ENV.key?('MORPH_API_KEY')
api_key = ERB::Util.url_encode(ENV['MORPH_API_KEY'])
output = output.gsub(api_key, 'REDACTED').uncolorize
end
# Only use last 64k of output
output = output[-64_000..-1] || output
title = "#{country.name} (#{legislature.name}): refresh data"
body = "Automated data refresh for #{country.name} - #{legislature.name}" \
"\n\n#### Output\n\n```\n#{output}\n```"
"\n\n#### Output\n\n```\n#{CleanedOutput.new(output: command.output)}\n```"
Sidekiq.redis do |conn|
key = "body:#{branch}"
conn.set(key, body)
Expand Down

0 comments on commit c28d349

Please sign in to comment.