Class: Phronomy::Runtime::TaskRegistry Private

Inherits:
Object
  • Object
show all
Defined in:
lib/phronomy/engine/runtime/task_registry.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Internal registry of active Task instances for a Phronomy::Runtime.

Tracks every task that has been spawned but not yet completed so that #shutdown can drain them. Tasks that complete synchronously deregister themselves before the caller returns from #spawn.

Instance Method Summary collapse

Constructor Details

#initializeTaskRegistry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of TaskRegistry.



12
13
14
15
# File 'lib/phronomy/engine/runtime/task_registry.rb', line 12

def initialize
  @mutex = Mutex.new
  @tasks = []
end

Instance Method Details

#deregister(task) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Removes task from the registry.



25
26
27
# File 'lib/phronomy/engine/runtime/task_registry.rb', line 25

def deregister(task)
  @mutex.synchronize { @tasks.delete(task) }
end

#drainObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compatibility helper for callers that explicitly require an unbounded drain. Runtime#shutdown uses #drain_until.



64
65
66
67
68
69
70
# File 'lib/phronomy/engine/runtime/task_registry.rb', line 64

def drain
  snapshot.each do |task|
    task.join
  rescue
    nil
  end
end

#drain_until(deadline) ⇒ Symbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Waits for registered tasks until the absolute monotonic deadline. The registry is re-snapshotted because tasks may create follow-up tasks while Runtime shutdown is draining accepted work.

Parameters:

  • deadline (Numeric)

    absolute Process::CLOCK_MONOTONIC value

Returns:

  • (Symbol)

    :empty or :timeout



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/phronomy/engine/runtime/task_registry.rb', line 36

def drain_until(deadline)
  loop do
    tasks = snapshot
    return :empty if tasks.empty?

    tasks.each do |task|
      remaining = deadline - monotonic_now
      return :timeout if remaining <= 0

      begin
        task.join(remaining)
      rescue
        # Some backends re-raise the task error from join. The task's
        # ensure block still deregisters it, so continue the drain.
        nil
      end

      return :timeout if task.alive? && monotonic_now >= deadline
    end

    return :empty if empty?
    return :timeout if monotonic_now >= deadline
  end
end

#empty?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


74
75
76
# File 'lib/phronomy/engine/runtime/task_registry.rb', line 74

def empty?
  @mutex.synchronize { @tasks.empty? }
end

#register(task) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds task unless it already completed synchronously.



19
20
21
# File 'lib/phronomy/engine/runtime/task_registry.rb', line 19

def register(task)
  @mutex.synchronize { @tasks << task unless task.done? }
end

#sizeInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Integer)


80
81
82
# File 'lib/phronomy/engine/runtime/task_registry.rb', line 80

def size
  @mutex.synchronize { @tasks.size }
end