<commit_before>require 'travis/build'
require 'simple_states'
require 'multi_json'
require 'thread'

module Travis
  module Worker

    # Represents a single Worker which is bound to a single VM instance.
    class Worker
      include SimpleStates
      include Util::Logging

      states :created, :booting, :waiting, :working, :stopped

      event :boot,   :from => :created, :to => :waiting
      event :work,   :from => :waiting, :to => :waiting
      event :stop,   :to => :stopped

      # Required by SimpleStates
      attr_accessor :state


      # Returns the MessageHub used to subscribe to the builds queue.
      attr_reader :builds_hub

      # Returns the virtual machine used exclusivly by this worker.
      attr_reader :virtual_machine

      # Returns the current job payload being processed.
      attr_reader :job_payload

      # Returns the reason (Symbol) that the worker was stopped.
      attr_reader :stopped_reason

      # Returns the last error if the last job resulted in an error.
      attr_reader :last_error


      # Instantiates a new worker.
      #
      # builds_hub      - The MessagingHub used to subscribe to the builds queue.
      # virtual_machine - The virtual machine to be used by the worker.
      def initialize(builds_hub, virtual_machine)
        @builds_hub = builds_hub
        @virtual_machine = virtual_machine
      end

      # Boots the worker by preparing the VM and subscribing to the builds queue.
      #
      # Returns self.
      def boot
        self.state = :booting
        virtual_machine.prepare
        builds_hub.subscribe(:ack => true, :blocking => false) do |meta, payload|
          begin
            work(meta, payload)
          rescue Errno::ECONNREFUSED, Exception => error
            error(error, meta)
            false
          end
        end
        self
      end

      # Processes a build message payload.
      #
      # This method also changes the state of the Worker to :workering while processing the
      # job, and saves the current payload to job_payload for introspection during the
      # build process.
      #
      # Returns true if the job was processed successfully.
      def work(metadata, payload)
        start(payload)
        process
        finish(metadata)
        true
      end

      # Stops the worker by cancelling the builds queue subscription.
      def stop
        announce("Stopping Worker for accepting further jobs")
        builds_hub.cancel_subscription if builds_hub
      end


      protected

        def start(payload)<commit_msg>this really should be a method so we can unit test the rescue block more easily<commit_after>require 'travis/build'
require 'simple_states'
require 'multi_json'
require 'thread'

module Travis
  module Worker

    # Represents a single Worker which is bound to a single VM instance.
    class Worker
      include SimpleStates
      include Util::Logging

      states :created, :booting, :waiting, :working, :stopped

      event :boot,   :from => :created, :to => :waiting
      event :work,   :from => :waiting, :to => :waiting
      event :stop,   :to => :stopped

      # Required by SimpleStates
      attr_accessor :state


      # Returns the MessageHub used to subscribe to the builds queue.
      attr_reader :builds_hub

      # Returns the virtual machine used exclusivly by this worker.
      attr_reader :virtual_machine

      # Returns the current job payload being processed.
      attr_reader :job_payload

      # Returns the reason (Symbol) that the worker was stopped.
      attr_reader :stopped_reason

      # Returns the last error if the last job resulted in an error.
      attr_reader :last_error


      # Instantiates a new worker.
      #
      # builds_hub      - The MessagingHub used to subscribe to the builds queue.
      # virtual_machine - The virtual machine to be used by the worker.
      def initialize(builds_hub, virtual_machine)
        @builds_hub = builds_hub
        @virtual_machine = virtual_machine
      end

      # Boots the worker by preparing the VM and subscribing to the builds queue.
      #
      # Returns self.
      def boot
        self.state = :booting
        virtual_machine.prepare
        builds_hub.subscribe(:ack => true, :blocking => false, &method(:work))
        self
      end

      # Processes a build message payload.
      #
      # This method also changes the state of the Worker to :workering while processing the
      # job, and saves the current payload to job_payload for introspection during the
      # build process.
      #
      # Returns true.
      def work(metadata, payload)
        start(payload)
        process
        finish(metadata)
      rescue Errno::ECONNREFUSED, Exception => error
        error(error, metadata)
        false
      end

      # Stops the worker by cancelling the builds queue subscription.
      def stop
        announce("Stopping Worker for accepting further jobs")
        builds_hub.cancel_subscription if builds_hub
      end


      protected

        def start(payload)
{'fraction_non_alphanumeric': 0.05862613501776549, 'fraction_numerical': 0.0, 'mean_word_length': 2.346763540290621, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '4303580', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1307, 'n_words': 520}