Class: Stockpile::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/stockpile/executor.rb

Overview

Stockpile::Executor

Executes passed in block of code and writes computed result into cache with an expiration of a given TTL. If execution is locked will wait for value to appear in cache instead. Will timeout after given amount of time and will execute block if no value can be read from cache.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, key, ttl) ⇒ Executor

Returns a new instance of Executor.



31
32
33
34
35
# File 'lib/stockpile/executor.rb', line 31

def initialize(db, key, ttl)
  @db = db
  @key = key
  @ttl = ttl
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



25
26
27
# File 'lib/stockpile/executor.rb', line 25

def db
  @db
end

#keyObject (readonly)

Returns the value of attribute key.



25
26
27
# File 'lib/stockpile/executor.rb', line 25

def key
  @key
end

#ttlObject (readonly)

Returns the value of attribute ttl.



25
26
27
# File 'lib/stockpile/executor.rb', line 25

def ttl
  @ttl
end

Class Method Details

.perform(db: :default, key:, ttl:, &block) ⇒ Object



27
28
29
# File 'lib/stockpile/executor.rb', line 27

def self.perform(db: :default, key:, ttl:, &block)
  new(db, key, ttl).perform(&block)
end

Instance Method Details

#perform(&block) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/stockpile/executor.rb', line 37

def perform(&block)
  if execution(&block).success?
    cache_and_release_execution
  else
    wait_for_cache_or_yield(&block)
  end
end