Class: Taski::Execution::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/taski/execution/registry.rb

Instance Method Summary collapse

Constructor Details

#initializeRegistry



8
9
10
11
12
13
# File 'lib/taski/execution/registry.rb', line 8

def initialize
  @tasks = {}
  @threads = []
  @monitor = Monitor.new
  @abort_requested = false
end

Instance Method Details

#abort_requested?Boolean



54
55
56
# File 'lib/taski/execution/registry.rb', line 54

def abort_requested?
  @monitor.synchronize { @abort_requested }
end

#get_or_create(task_class) { ... } ⇒ Object

Returns The task instance.

Yields:

  • Block to create the task instance if it doesn’t exist



18
19
20
# File 'lib/taski/execution/registry.rb', line 18

def get_or_create(task_class)
  @tasks[task_class] ||= yield
end

#get_task(task_class) ⇒ Object

Returns The task instance.

Raises:

  • (RuntimeError)

    If the task is not registered



25
26
27
28
29
# File 'lib/taski/execution/registry.rb', line 25

def get_task(task_class)
  @tasks.fetch(task_class) do
    raise "Task #{task_class} not registered"
  end
end

#register_thread(thread) ⇒ Object



32
33
34
# File 'lib/taski/execution/registry.rb', line 32

def register_thread(thread)
  @monitor.synchronize { @threads << thread }
end

#request_abort!Object



49
50
51
# File 'lib/taski/execution/registry.rb', line 49

def request_abort!
  @monitor.synchronize { @abort_requested = true }
end

#reset!Object



41
42
43
44
45
46
47
# File 'lib/taski/execution/registry.rb', line 41

def reset!
  @monitor.synchronize do
    @tasks.clear
    @threads.clear
    @abort_requested = false
  end
end

#run(task_class, exported_methods) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/taski/execution/registry.rb', line 61

def run(task_class, exported_methods)
  exported_methods.each do |method|
    task_class.public_send(method)
  end

  wait_all

  get_task(task_class).result
end

#wait_allObject



36
37
38
39
# File 'lib/taski/execution/registry.rb', line 36

def wait_all
  threads = @monitor.synchronize { @threads.dup }
  threads.each(&:join)
end