Class: BackgroundQueue::ServerLib::TaskRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/background_queue/server_lib/task_registry.rb

Overview

keep track if tasks already queued and running so if the same task comes in, we know to remove it.

Instance Method Summary collapse

Constructor Details

#initializeTaskRegistry

Returns a new instance of TaskRegistry.



6
7
8
9
# File 'lib/background_queue/server_lib/task_registry.rb', line 6

def initialize
  @waiting_tasks = {}
  @tasks = {}
end

Instance Method Details

#de_register(task_id) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/background_queue/server_lib/task_registry.rb', line 32

def de_register(task_id)
  @tasks.delete(task_id)
  waiting = get_waiting_task(task_id)
  if waiting
    @tasks[task_id] = waiting
  end
  waiting
end

#get_waiting_task(task_id) ⇒ Object



45
46
47
# File 'lib/background_queue/server_lib/task_registry.rb', line 45

def get_waiting_task(task_id)
  @waiting_tasks.delete(task_id)
end

#register(task) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/background_queue/server_lib/task_registry.rb', line 11

def register(task)
  existing_task = @tasks[task.id]
  if existing_task.nil? 
    #puts "nil task"
    @tasks[task.id] = task
    [:new, nil]
  elsif existing_task.running?
    #puts "task running"
    register_waiting_task(task)
    [:waiting, nil]
  elsif existing_task.waiting_to_retry?
    #puts "task waiting_to_retry"
    @tasks[task.id] = task
    [:waiting_to_retry, existing_task]
  else
    #puts "task waiting"
    @tasks[task.id] = task
    [:existing, existing_task]
  end
end

#register_waiting_task(task) ⇒ Object



41
42
43
# File 'lib/background_queue/server_lib/task_registry.rb', line 41

def register_waiting_task(task)
  @waiting_tasks[task.id] = task
end

#tasksObject



53
54
55
# File 'lib/background_queue/server_lib/task_registry.rb', line 53

def tasks
  @tasks
end

#waiting_tasksObject



49
50
51
# File 'lib/background_queue/server_lib/task_registry.rb', line 49

def waiting_tasks
  @waiting_tasks
end