Sha256: 6fe062c37d8a6000963de32c92639032727d79c23f7472891ed7e2eab1b1606d

Contents?: true

Size: 1.9 KB

Versions: 3

Compression:

Stored size: 1.9 KB

Contents

module Scrooge
  module Strategy
    class Stage
      
      # Represents a duration sensitive stage / phase of execution.
      
      STATES = { :initialized => 0, 
                 :execute => 1,
                 :terminated => 2 }
      
      attr_accessor :signature,
                    :duration,
                    :payload 
      
      # Requires a unique signature and stores a payload for later execution.
      #
      # Valid options is
      # * :for : the phase / stage duration, in seconds 
      #               
      def initialize( signature, options = {}, &block )
        @signature = signature                  
        @duration = options[:for] || 0 
        @payload = block
        @state = :initialized
      end
      
      # Always executeable when initialized and not yet terminated.
      #
      def executeable?
        initialized? && !terminated?
      end
      
      # Enter the :execute state, call the payload and sleep for the defined duration.
      # Returns the payload result on completion and ensures that the current state is
      # :terminated.
      #
      def execute!
        begin
          Scrooge::Base.profile.log( "Execute stage #{signature.inspect} ...", true)
          @state = :execute
          result = call!
          sleep( @duration )      
          result
        ensure
          @state = :terminated
        end
      end
      alias :run! :execute!
      
      # Are we running ?
      #
      def execute?
        @state == :execute
      end
      alias :running? :execute?
      
      # Is this stage pending execution ?
      #
      def initialized?
        @state == :initialized
      end
      
      # Has this stage already terminated ?
      #
      def terminated?
        @state == :terminated
      end
      
      private
      
        def call! #:nodoc:
          Scrooge::Base.profile.instance_eval( &@payload )
        end
      
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
methodmissing-scrooge-1.0.2 lib/scrooge/strategy/stage.rb
methodmissing-scrooge-1.0.3 lib/scrooge/strategy/stage.rb
methodmissing-scrooge-1.0.4 lib/scrooge/strategy/stage.rb