Module: Brine::Performing

Included in:
Brine
Defined in:
lib/brine/performing.rb

Overview

Support either immediate or defered evaluation of logic.

Defined Under Namespace

Classes: CollectingPerformer, ImmediatePerformer

Instance Method Summary collapse

Instance Method Details

#collect_actionsObject

Begin collecting, rather than immediately performing, actions.


92
93
94
# File 'lib/brine/performing.rb', line 92

def collect_actions
  @performer = CollectingPerformer.new
end

#perform(&actions) ⇒ Object

Pass the actions to the active Performer instance.

Parameters:

  • actions (Proc)

    The actions to pass to the Performer.


85
86
87
# File 'lib/brine/performing.rb', line 85

def perform(&actions)
  performer.perform(actions)
end

#performerPerformer, #perform

Expose the currently active Performer as a property.

The default implementation will be wired as needed upon first access.

Returns:

  • (Performer, #perform)

    Return the Performer to which actions will be sent.


67
68
69
# File 'lib/brine/performing.rb', line 67

def performer
  @performer || reset_performer
end

#poll_for(seconds, interval = poll_interval_seconds) ⇒ Object

Retry the provided block for the specified period.

If the provided block is evaluated successfully the result will be returned, if any exception is thrown it will be retried until the period elapses.

Parameters:

  • seconds (Number)

    Define the period (in seconds) for which the block will be retried.

  • interval (Number) (defaults to: poll_interval_seconds)

    Define how long to sleep between polling attempts (defaults to ‘#poll_interval_seconds`).

  • Provide (Block)

    the logic to retry within the defined period.

Returns:

  • (Object)

    The result of the block if successfully evaluated.


121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/brine/performing.rb', line 121

def poll_for(seconds, interval=poll_interval_seconds)
  failure = nil
  quit = Time.now + seconds
  while (Time.now < quit)
    begin
      return yield
    rescue Exception => ex
      failure = ex
      sleep interval
    end
  end
  raise failure
end

#poll_interval_secondsNumber

Determine the number of seconds between polling attempts.

This can be provided by the ‘BRINE_POLL_INTERVAL_SECONDS` environment variable (defaults to `0.5`).

Returns:

  • (Number)

    Return the number of seconds to sleep between poll attempts.


104
105
106
# File 'lib/brine/performing.rb', line 104

def poll_interval_seconds
  ENV['BRINE_POLL_INTERVAL_SECONDS'] || 0.25
end

#reset_performerPerformer, #perform

Reset the Performer instance to the default implementation.

Returns:

  • (Performer, #perform)

    Return the default implementation which will now be the active Performer.


76
77
78
# File 'lib/brine/performing.rb', line 76

def reset_performer
  @performer = ImmediatePerformer.new
end

#retrieve_duration(duration) ⇒ Number

Retrieve the duration in seconds for the given handle.

Currently this only supports values provided through environment variables of the format BRINE_DURATION_SECONDS_handle.

Parameters:

  • duration (String)

    Identify the duration whose length should be returned.

Returns:

  • (Number)

    Return the number of seconds defined for the requested duration.


144
145
146
147
148
149
150
# File 'lib/brine/performing.rb', line 144

def retrieve_duration(duration)
  if ENV["BRINE_DURATION_SECONDS_#{duration}"]
    ENV["BRINE_DURATION_SECONDS_#{duration}"].to_f
  else
    STDERR.puts("Duration #{duration} not defined")
  end
end