Class: Fractor::WrappedRactor

Inherits:
Object
  • Object
show all
Defined in:
lib/fractor/wrapped_ractor.rb

Overview

Base class for wrapped Ractors with shared functionality. Subclasses implement Ruby 3.x and Ruby 4.0+ specific communication patterns.

Direct Known Subclasses

WrappedRactor3, WrappedRactor4

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, worker_class) ⇒ WrappedRactor

Initializes the WrappedRactor with a name and the Worker class.

Parameters:

  • name (String)

    Name for the ractor

  • worker_class (Class)

    Worker class to instantiate



28
29
30
31
32
33
# File 'lib/fractor/wrapped_ractor.rb', line 28

def initialize(name, worker_class)
  puts "Creating Ractor #{name} with worker #{worker_class}" if ENV["FRACTOR_DEBUG"]
  @name = name
  @worker_class = worker_class
  @ractor = nil
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/fractor/wrapped_ractor.rb', line 7

def name
  @name
end

#ractorObject (readonly)

Returns the value of attribute ractor.



7
8
9
# File 'lib/fractor/wrapped_ractor.rb', line 7

def ractor
  @ractor
end

#worker_classObject (readonly)

Returns the value of attribute worker_class.



7
8
9
# File 'lib/fractor/wrapped_ractor.rb', line 7

def worker_class
  @worker_class
end

Class Method Details

.create(name, worker_class, **kwargs) ⇒ WrappedRactor

Factory method to create the appropriate WrappedRactor implementation based on the current Ruby version.

Parameters:

  • name (String)

    Name for the ractor

  • worker_class (Class)

    Worker class to instantiate

  • kwargs (Hash)

    Additional keyword arguments for subclass initialization

Returns:



16
17
18
19
20
21
22
# File 'lib/fractor/wrapped_ractor.rb', line 16

def self.create(name, worker_class, **kwargs)
  if Fractor::RUBY_4_0_OR_HIGHER
    WrappedRactor4.new(name, worker_class, **kwargs)
  else
    WrappedRactor3.new(name, worker_class, **kwargs)
  end
end

Instance Method Details

#closeBoolean

Closes the Ractor.

Returns:

  • (Boolean)

    true if closed successfully



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fractor/wrapped_ractor.rb', line 58

def close
  return true if @ractor.nil?

  begin
    # Send a nil message to signal we're done
    begin
      @ractor.send(nil)
    rescue StandardError
      nil
    end

    # Mark as closed in our object
    old_ractor = @ractor
    @ractor = nil

    # If available in this Ruby version, we'll try kill
    if old_ractor.respond_to?(:kill)
      begin
        old_ractor.kill
      rescue StandardError
        nil
      end
    end

    true
  rescue Exception => e
    puts "Warning: Error closing Ractor #{@name}: #{e.message}" if ENV["FRACTOR_DEBUG"]
    # Consider it closed even if there was an error
    @ractor = nil
    true
  end
end

#closed?Boolean

Checks if the Ractor is closed or unavailable. Uses a timeout to avoid blocking on Windows Ruby 3.4 where Ractor#inspect can block if the ractor is waiting on receive.

Returns:

  • (Boolean)

    true if closed, false otherwise



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fractor/wrapped_ractor.rb', line 96

def closed?
  return true if @ractor.nil?

  # Use a timeout to avoid blocking indefinitely on Windows Ruby 3.4
  result = Timeout.timeout(0.1) do
    @ractor.inspect
  rescue Timeout::Error
    # Timeout means ractor is still running (not terminated)
    "#<Ractor:blocked>"
  end

  if result.include?("terminated")
    # If terminated, clean up our reference
    @ractor = nil
    return true
  end

  false
rescue Exception
  # If we get an exception, the Ractor is likely terminated
  @ractor = nil
  true
end

#receive_messageHash?

Receives a message from the Ractor. Must be implemented by subclasses.

Returns:

  • (Hash, nil)

    The message received or nil

Raises:

  • (NotImplementedError)


51
52
53
# File 'lib/fractor/wrapped_ractor.rb', line 51

def receive_message
  raise NotImplementedError, "Subclasses must implement #receive_message"
end

#send(work) ⇒ Boolean

Sends work to the Ractor. Must be implemented by subclasses.

Parameters:

Returns:

  • (Boolean)

    true if sent successfully, false otherwise

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/fractor/wrapped_ractor.rb', line 44

def send(work)
  raise NotImplementedError, "Subclasses must implement #send"
end

#startObject

Starts the underlying Ractor. Must be implemented by subclasses.

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/fractor/wrapped_ractor.rb', line 36

def start
  raise NotImplementedError, "Subclasses must implement #start"
end