Module: Brine::Performing

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

Overview

A module supporting 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.



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

def collect_actions
  @performer = CollectingPerformer.new
end

#perform(&actions) ⇒ Object

Pass the actions to the current Performer instance.

Parameters:

  • The (Proc)

    thunk of the actions to be performed.



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

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

#performerPerformer, #perform

The currently active Performer instance exposed as a property.

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

Returns:

  • (Performer, #perform)

    The Performer to which actions will be sent.



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

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)

    The period (in seconds) during which the block will be retried.

  • interval (Number) (defaults to: poll_interval_seconds)

    How long to sleep between polling attempts, defaults to ‘#poll_interval_seconds`.

  • The (Block)

    logic to retry within the defined period.

Returns:

  • (Object)

    The result of the block if successfully evaluated.



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

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

The number of seconds between polling attempts.

Can be provided by the ‘BRINE_POLL_INTERVAL_SECONDS` environment variable. Defaults to `0.5`.

Returns:

  • (Number)

    The number of seconds to sleep between poll attempts.



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

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)

    The default implementation which will now be the ‘performer`.



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

def reset_performer
  @performer = ImmediatePerformer.new
end

#retrieve_duration(duration) ⇒ Number

The duration in seconds for the given handle.

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

@param duration The handle/name of the duration whose length should be returned.

Returns:

  • (Number)

    The number of seconds to poll for the requested duration.



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

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