Sha256: 3a31be8a51faa8674cb992add81d0c8bb11156e26fc1bad0a790fddd16deb96e

Contents?: true

Size: 1.83 KB

Versions: 4

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module Lite
  module Command

    STATES = [
      PENDING     = "pending",
      EXECUTING   = "executing",
      COMPLETE    = "complete",
      INTERRUPTED = "interrupted"
    ].freeze

    module Internals
      module Executions

        def state
          @state || PENDING
        end

        def executed?
          complete? || interrupted?
        end

        STATES.each do |s|
          # eg: executing?
          define_method(:"#{s}?") { state == s }
        end

        private

        def executing!
          return unless pending?

          @state = EXECUTING
        end

        def complete!
          return if executed?

          @state = COMPLETE
        end

        def interrupted!
          return if executed?

          @state = INTERRUPTED
        end

        def before_execution
          increment_execution_index
          assign_execution_cmd_id
          start_monotonic_time

          run_hooks(:before_execution)
          executing!
          validate_context_attributes
          run_hooks(:on_executing)
        end

        def after_execution
          send(:"#{success? ? COMPLETE : INTERRUPTED}!")
          run_hooks(:"on_#{status}")
          run_hooks(:"on_#{state}")
          run_hooks(:after_execution)

          stop_monotonic_time
          append_execution_result
          freeze_execution_objects
        end

        def around_execution
          before_execution
          yield
          after_execution
        end

        def execute
          execute!
        rescue StandardError
          # Do nothing
        end

        def execute!
          around_execution { call }
        rescue StandardError => e
          fault(e, Utils.cmd_try(e, :type) || ERROR, metadata, exception: e)
          after_execution
          raise(e)
        end

      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lite-command-3.3.3 lib/lite/command/internals/executions.rb
lite-command-3.3.2 lib/lite/command/internals/executions.rb
lite-command-3.3.1 lib/lite/command/internals/executions.rb
lite-command-3.3.0 lib/lite/command/internals/executions.rb