Module: Bramble::State

Extended by:
Keys
Defined in:
lib/bramble/state.rb

Overview

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

Constant Summary collapse

SEPARATOR =
":"

Class Method Summary collapse

Methods included from Keys

data_key, finished_at_key, job_id_key, keys_key, map_finished_count_key, map_total_count_key, namespace, reduce_finished_count_key, reduce_total_count_key, result_key, status_key

Class Method Details

.clear_job(handle) ⇒ Object

Clear the state of ‘handle`



30
31
32
33
34
35
36
# 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) ⇒ Object

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



55
56
57
58
59
60
61
62
63
# 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) ⇒ Object

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



66
67
68
69
70
71
# 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) ⇒ Object

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



39
40
41
42
43
44
# 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) ⇒ Object

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



47
48
49
50
51
52
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
# 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) ⇒ Object

Mark ‘handle` as started



19
20
21
22
23
24
25
26
27
# 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