Class: Agent::Once

Inherits:
Object
  • Object
show all
Defined in:
lib/agent/once.rb

Direct Known Subclasses

BlockingOnce

Instance Method Summary collapse

Constructor Details

#initializeOnce

Returns a new instance of Once.



3
4
5
6
# File 'lib/agent/once.rb', line 3

def initialize
  @mutex     = Mutex.new
  @performed = false
end

Instance Method Details

#performObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/agent/once.rb', line 8

def perform
  # optimium path
  return nil, error if @performed

  # slow path
  @mutex.synchronize do
    return nil, error if @performed
    @performed = true
  end

  return yield, nil
end

#performed?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/agent/once.rb', line 21

def performed?
  @performed
end