<commit_before>module Fezzik
  class HostTask < Rake::Task
    attr_accessor :roles

    def initialize(task_name, app)
      super
      @roles = []
      @host_actions = []
    end

    def enhance(deps = nil, &block)
      @host_actions << block if block_given?
      super(deps)
    end

    def execute(args = nil)
      return if Rake.application.options.dryrun

      # TODO(weave): Handle failure of a call to `run`. Throw a Fezzik::CommandFailedError.
      # TODO(weave): Call action with args (requires weave addition?)
      if @roles.empty?
        hosts = Fezzik.get(:domain).map { |domain| "#{Fezzik.get(:user)}@#{domain}" }
        @@connection_pool ||= Weave.connect(hosts)
        @host_actions.each { |action| @@connection_pool.execute(&action) }
      else
        @roles.each do |role|
          Fezzik.with_role(role) do
            hosts = Fezzik.get(:domain).map { |domain| "#{Fezzik.get(:user)}@#{domain}" }
            @@role_connection_pools ||= {}
            @@role_connection_pools[role] ||= Weave.connect(hosts)
            @host_actions.each { |action| @@role_connection_pools[role].execute(&action) }
          end
        end
      end
    end<commit_msg>Print nice error and abort when a Weave command fails.<commit_after>module Fezzik
  class HostTask < Rake::Task
    attr_accessor :roles

    def initialize(task_name, app)
      super
      @roles = []
      @host_actions = []
    end

    def enhance(deps = nil, &block)
      @host_actions << block if block_given?
      super(deps)
    end

    def execute(args = nil)
      return if Rake.application.options.dryrun

      # TODO(weave): Call action with args (requires weave addition?)
      if @roles.empty?
        hosts = Fezzik.get(:domain).map { |domain| "#{Fezzik.get(:user)}@#{domain}" }
        @@connection_pool ||= Weave.connect(hosts)
        @host_actions.each do |action|
          begin
            @@connection_pool.execute(&action)
          rescue Weave::Error => e
            abort_task(e)
          end
        end
      else
        @roles.each do |role|
          Fezzik.with_role(role) do
            hosts = Fezzik.get(:domain).map { |domain| "#{Fezzik.get(:user)}@#{domain}" }
            @@role_connection_pools ||= {}
            @@role_connection_pools[role] ||= Weave.connect(hosts)
            @host_actions.each do |action|
              begin
                @@role_connection_pools[role].execute(&action)
              rescue Weave::Error => e
                abort_task(e)
              end
            end
          end
        end
      end
    end

    private

    def abort_task(exception)
      STDERR.puts "Error running command in HostTask '#{@name}':"
      abort exception.message
    end
{'fraction_non_alphanumeric': 0.12101210121012101, 'fraction_numerical': 0.0, 'mean_word_length': 2.1869158878504673, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1645388', 'n_tokens_mistral': 867, 'n_tokens_neox': 837, 'n_words': 186}