module Bramble::State

Helpers for detecting and managing the state of a given `handle` while the job is (or isn't) running

Constants

SEPARATOR

Private Class Methods

percent_between_keys(total_key, finished_key) click to toggle source
# File lib/bramble/state.rb, line 79
def self.percent_between_keys(total_key, finished_key)
  total = storage.get(total_key).to_f
  if total == 0
    0
  else
    finished = storage.get(finished_key).to_i
    finished / total
  end
end
storage() click to toggle source
# File lib/bramble/state.rb, line 75
def self.storage
  Bramble::Storage
end

Public Instance Methods

clear_job(handle) click to toggle source

Clear the state of `handle`

# File lib/bramble/state.rb, line 30
def clear_job(handle)
  handle_name, job_id = handle.split(SEPARATOR)
  storage.delete(job_id_key(handle_name))
  storage.delete(status_key(handle))
  clear_reduce(handle)
  clear_map(handle)
end
clear_map(handle) click to toggle source

Clear all traces of the `.map` operation for `handle`

# File lib/bramble/state.rb, line 55
def clear_map(handle)
  map_group_keys = storage.map_keys_get(keys_key(handle))
  map_group_keys.each do |group_key|
    storage.delete(data_key(handle, group_key))
  end
  storage.delete(keys_key(handle))
  storage.delete(map_total_count_key(handle))
  storage.delete(map_finished_count_key(handle))
end
clear_reduce(handle) click to toggle source

Clear all traces of the `.reduce` operation for `handle`

# File lib/bramble/state.rb, line 66
def clear_reduce(handle)
  storage.delete(reduce_total_count_key(handle))
  storage.delete(reduce_finished_count_key(handle))
  storage.delete(result_key(handle))
  storage.delete(finished_at_key(handle))
end
percent_mapped(handle) click to toggle source

How many values of `handle` have been sent to `.map`?

# File lib/bramble/state.rb, line 39
def percent_mapped(handle)
  percent_between_keys(
    map_total_count_key(handle),
    map_finished_count_key(handle)
  )
end
percent_reduced(handle) click to toggle source

How many values of `handle` have been sent to `.reduce?`

# File lib/bramble/state.rb, line 47
def percent_reduced(handle)
  percent_between_keys(
    reduce_total_count_key(handle),
    reduce_finished_count_key(handle)
  )
end
running?(handle) { || ... } click to toggle source

Run the block and return true if the `job_id` is still active

# File lib/bramble/state.rb, line 9
def running?(handle)
  handle_name, job_id = handle.split(SEPARATOR)
  is_running = storage.get(job_id_key(handle_name)) == job_id
  if block_given?
    yield
  end
  is_running
end
start_job(handle) click to toggle source

Mark `handle` as started

# File lib/bramble/state.rb, line 19
def start_job(handle)
  handle_name, job_id = handle.split(SEPARATOR)
  previous_job_id = storage.get(job_id_key(handle_name))
  if previous_job_id
    clear_job("#{handle_name}:#{previous_job_id}")
  end
  storage.set(status_key(handle), "started")
  storage.set(job_id_key(handle_name), job_id)
end