Class: Makara::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/makara/context.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context_data) ⇒ Context

Returns a new instance of Context.



9
10
11
12
13
14
15
# File 'lib/makara/context.rb', line 9

def initialize(context_data)
  @stored_data = context_data
  @staged_data = {}
  @dirty = @was_dirty = false

  freeze_time
end

Instance Attribute Details

#current_timestampObject (readonly)

Returns the value of attribute current_timestamp.



7
8
9
# File 'lib/makara/context.rb', line 7

def current_timestamp
  @current_timestamp
end

#staged_dataObject

Returns the value of attribute staged_data.



6
7
8
# File 'lib/makara/context.rb', line 6

def staged_data
  @staged_data
end

#stored_dataObject

Returns the value of attribute stored_data.



6
7
8
# File 'lib/makara/context.rb', line 6

def stored_data
  @stored_data
end

Class Method Details

.nextObject



111
112
113
114
115
# File 'lib/makara/context.rb', line 111

def next
  if current.commit
    current.stored_data
  end
end

.release(proxy_id) ⇒ Object



117
118
119
# File 'lib/makara/context.rb', line 117

def release(proxy_id)
  current.release(proxy_id)
end

.release_allObject



121
122
123
# File 'lib/makara/context.rb', line 121

def release_all
  current.release_all
end

.set_current(context_data) ⇒ Object



98
99
100
# File 'lib/makara/context.rb', line 98

def set_current(context_data)
  set(:makara_current_context, new(context_data))
end

.stick(proxy_id, ttl) ⇒ Object

Called by ‘Proxy#stick_to_master!` to use master in subsequent requests



103
104
105
# File 'lib/makara/context.rb', line 103

def stick(proxy_id, ttl)
  current.stage(proxy_id, ttl)
end

.stuck?(proxy_id) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/makara/context.rb', line 107

def stuck?(proxy_id)
  current.staged?(proxy_id) || current.stuck?(proxy_id)
end

Instance Method Details

#commitObject

Stores the staged data with an expiration time based on the current time, and clears any expired entries. Returns true if any changes were made to the current store



47
48
49
50
51
52
53
54
# File 'lib/makara/context.rb', line 47

def commit
  freeze_time
  release_expired
  store_staged_data
  clean

  was_dirty?
end

#release(proxy_id) ⇒ Object



29
30
31
32
# File 'lib/makara/context.rb', line 29

def release(proxy_id)
  @dirty ||= !!stored_data.delete(proxy_id)
  staged_data.delete(proxy_id)
end

#release_allObject



34
35
36
37
38
39
40
41
42
# File 'lib/makara/context.rb', line 34

def release_all
  if self.stored_data.any?
    self.stored_data = {}
    # We need to track a change made to the current stored data
    # so we can commit it later
    @dirty = true
  end
  self.staged_data = {}
end

#stage(proxy_id, ttl) ⇒ Object



17
18
19
# File 'lib/makara/context.rb', line 17

def stage(proxy_id, ttl)
  staged_data[proxy_id] = [staged_data[proxy_id].to_f, ttl.to_f].max
end

#staged?(proxy_id) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/makara/context.rb', line 25

def staged?(proxy_id)
  staged_data.key?(proxy_id)
end

#stuck?(proxy_id) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/makara/context.rb', line 21

def stuck?(proxy_id)
  stored_data[proxy_id] && !expired?(stored_data[proxy_id])
end