fractor 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a3fc9be0fb1d8993186e56bbef30b33396314e878f8a9ea3d31d79efd3c46f06
4
- data.tar.gz: 8edb3ff38f5c054d9c1e477bfb799605fd4827eb679dc10db224ad1eb45e8d64
3
+ metadata.gz: b51bfbe1d8aac8575dcd85deb88b286b8f3b26e391a228e01d43f3a466d91f26
4
+ data.tar.gz: '08198a0b664d2ddfa9cbef0a173f1672f9584dba1700f82374b0b9916cad91a6'
5
5
  SHA512:
6
- metadata.gz: 17fbad9c566525c9b01ccd8f613a6fe2eb2be62298894fc4ae636736ad678f3d1c46fbf49cce5e84d6e3584cc0a5e31ce06f92a994f2a3b92e58cceedabafcbc
7
- data.tar.gz: a33d27eb70ffbd8769c6db1f5484788d0b187b5c1feb33ba65602579a2ecc17c3c14be6654ad74c414a63cf9b5d19a818bd462293eca0ca13bfd1f92272760f1
6
+ metadata.gz: af7c2e42e19b28ced53b9e3aecf37409b9aff3c92d8829a0835312a4a5852069530b36f57c01c84727d09ad9de3b308ecfa05a5a812454402a300219938bce19
7
+ data.tar.gz: e08de69150ff8d528d481fcb1e872cd2d274e6b332f6d2ad3ad3b879e13328d50af3fae3294aecd81851dc7d6f3f6cc317570a7978b0f1a8adcd142d4b18a8f5
@@ -13,10 +13,10 @@ module Fractor
13
13
 
14
14
  def add_result(result)
15
15
  if result.success?
16
- puts "Work completed successfully: Result: #{result.result}"
16
+ puts "Work completed successfully: Result: #{result.result}" if ENV["FRACTOR_DEBUG"]
17
17
  @results << result
18
18
  else
19
- puts "Error processing work: #{result}"
19
+ puts "Error processing work: #{result}" if ENV["FRACTOR_DEBUG"]
20
20
  @errors << result
21
21
  end
22
22
 
@@ -88,19 +88,32 @@ module Fractor
88
88
 
89
89
  # Sets up a signal handler for graceful shutdown (Ctrl+C).
90
90
  def setup_signal_handler
91
- # Need access to @workers within the trap block
91
+ # Store instance variables in local variables for the signal handler
92
92
  workers_ref = @workers
93
+
94
+ # Trap INT signal (Ctrl+C)
93
95
  Signal.trap("INT") do
94
- puts "\nCtrl+C received. Initiating immediate shutdown..."
95
- puts "Attempting to close worker Ractors..."
96
+ puts "\nCtrl+C received. Initiating immediate shutdown..." if ENV["FRACTOR_DEBUG"]
97
+
98
+ # Set running to false to break the main loop
99
+ @running = false
100
+
101
+ puts "Sending shutdown message to all Ractors..." if ENV["FRACTOR_DEBUG"]
102
+
103
+ # Send shutdown message to each worker Ractor
96
104
  workers_ref.each do |w|
97
- w.close # Use the close method of WrappedRactor
98
- puts "Closed Ractor: #{w.name}"
105
+ w.send(:shutdown)
106
+ puts "Sent shutdown to Ractor: #{w.name}" if ENV["FRACTOR_DEBUG"]
99
107
  rescue StandardError => e
100
- puts "Error closing Ractor #{w.name}: #{e.message}"
108
+ puts "Error sending shutdown to Ractor #{w.name}: #{e.message}" if ENV["FRACTOR_DEBUG"]
101
109
  end
102
- puts "Exiting now."
103
- exit(1) # Exit immediately
110
+
111
+ puts "Exiting now." if ENV["FRACTOR_DEBUG"]
112
+ exit!(1) # Use exit! to exit immediately without running at_exit handlers
113
+ rescue Exception => e
114
+ puts "Error in signal handler: #{e.class}: #{e.message}" if ENV["FRACTOR_DEBUG"]
115
+ puts e.backtrace.join("\n") if ENV["FRACTOR_DEBUG"]
116
+ exit!(1)
104
117
  end
105
118
  end
106
119
 
@@ -208,7 +221,7 @@ module Fractor
208
221
  # Stop the supervisor (for continuous mode)
209
222
  def stop
210
223
  @running = false
211
- puts "Stopping supervisor..."
224
+ puts "Stopping supervisor..." if ENV["FRACTOR_DEBUG"]
212
225
  end
213
226
 
214
227
  private
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Fractor
4
4
  # Fractor version
5
- VERSION = "0.1.2"
5
+ VERSION = "0.1.3"
6
6
  end
@@ -9,7 +9,7 @@ module Fractor
9
9
  # Initializes the WrappedRactor with a name and the Worker class to instantiate.
10
10
  # The worker_class parameter allows flexibility in specifying the worker type.
11
11
  def initialize(name, worker_class)
12
- puts "Creating Ractor #{name} with worker #{worker_class}"
12
+ puts "Creating Ractor #{name} with worker #{worker_class}" if ENV["FRACTOR_DEBUG"]
13
13
  @name = name
14
14
  @worker_class = worker_class # Store the worker class
15
15
  @ractor = nil # Initialize ractor as nil
@@ -17,10 +17,10 @@ module Fractor
17
17
 
18
18
  # Starts the underlying Ractor.
19
19
  def start
20
- puts "Starting Ractor #{@name}"
20
+ puts "Starting Ractor #{@name}" if ENV["FRACTOR_DEBUG"]
21
21
  # Pass worker_class to the Ractor block
22
22
  @ractor = Ractor.new(@name, @worker_class) do |name, worker_cls|
23
- puts "Ractor #{name} started with worker class #{worker_cls}"
23
+ puts "Ractor #{name} started with worker class #{worker_cls}" if ENV["FRACTOR_DEBUG"]
24
24
  # Yield an initialization message
25
25
  Ractor.yield({ type: :initialize, processor: name })
26
26
 
@@ -29,19 +29,26 @@ module Fractor
29
29
 
30
30
  loop do
31
31
  # Ractor.receive will block until a message is received
32
- puts "Waiting for work in #{name}"
32
+ puts "Waiting for work in #{name}" if ENV["FRACTOR_DEBUG"]
33
33
  work = Ractor.receive
34
- puts "Received work #{work.inspect} in #{name}"
34
+
35
+ # Handle shutdown message
36
+ if work == :shutdown
37
+ puts "Received shutdown message in Ractor #{name}, terminating..." if ENV["FRACTOR_DEBUG"]
38
+ break
39
+ end
40
+
41
+ puts "Received work #{work.inspect} in #{name}" if ENV["FRACTOR_DEBUG"]
35
42
 
36
43
  begin
37
44
  # Process the work using the instantiated worker
38
45
  result = worker.process(work)
39
- puts "Sending result #{result.inspect} from Ractor #{name}"
46
+ puts "Sending result #{result.inspect} from Ractor #{name}" if ENV["FRACTOR_DEBUG"]
40
47
  # Yield the result back
41
48
  Ractor.yield({ type: :result, result: result, processor: name })
42
49
  rescue StandardError => e
43
50
  # Handle errors during processing
44
- puts "Error processing work #{work.inspect} in Ractor #{name}: #{e.message}\n#{e.backtrace.join("\n")}"
51
+ puts "Error processing work #{work.inspect} in Ractor #{name}: #{e.message}\n#{e.backtrace.join("\n")}" if ENV["FRACTOR_DEBUG"]
45
52
  # Yield an error message back
46
53
  # Ensure the original work object is included in the error result
47
54
  error_result = Fractor::WorkResult.new(error: e.message, work: work)
@@ -49,14 +56,14 @@ module Fractor
49
56
  end
50
57
  end
51
58
  rescue Ractor::ClosedError
52
- puts "Ractor #{name} closed."
59
+ puts "Ractor #{name} closed." if ENV["FRACTOR_DEBUG"]
53
60
  rescue StandardError => e
54
- puts "Unexpected error in Ractor #{name}: #{e.message}\n#{e.backtrace.join("\n")}"
61
+ puts "Unexpected error in Ractor #{name}: #{e.message}\n#{e.backtrace.join("\n")}" if ENV["FRACTOR_DEBUG"]
55
62
  # Optionally yield a critical error message if needed
56
63
  ensure
57
- puts "Ractor #{name} shutting down."
64
+ puts "Ractor #{name} shutting down." if ENV["FRACTOR_DEBUG"]
58
65
  end
59
- puts "Ractor #{@name} instance created: #{@ractor}"
66
+ puts "Ractor #{@name} instance created: #{@ractor}" if ENV["FRACTOR_DEBUG"]
60
67
  end
61
68
 
62
69
  # Sends work to the Ractor if it's active.
@@ -66,11 +73,11 @@ module Fractor
66
73
  @ractor.send(work)
67
74
  true
68
75
  rescue Exception => e
69
- puts "Warning: Error sending work to Ractor #{@name}: #{e.message}"
76
+ puts "Warning: Error sending work to Ractor #{@name}: #{e.message}" if ENV["FRACTOR_DEBUG"]
70
77
  false
71
78
  end
72
79
  else
73
- puts "Warning: Attempted to send work to nil Ractor #{@name}"
80
+ puts "Warning: Attempted to send work to nil Ractor #{@name}" if ENV["FRACTOR_DEBUG"]
74
81
  false
75
82
  end
76
83
  end
@@ -108,7 +115,7 @@ module Fractor
108
115
 
109
116
  true
110
117
  rescue Exception => e
111
- puts "Warning: Error closing Ractor #{@name}: #{e.message}"
118
+ puts "Warning: Error closing Ractor #{@name}: #{e.message}" if ENV["FRACTOR_DEBUG"]
112
119
  # Consider it closed even if there was an error
113
120
  @ractor = nil
114
121
  true
@@ -131,7 +138,7 @@ module Fractor
131
138
  false
132
139
  rescue Exception => e
133
140
  # If we get an exception, the Ractor is likely terminated
134
- puts "Ractor #{@name} appears to be terminated: #{e.message}"
141
+ puts "Ractor #{@name} appears to be terminated: #{e.message}" if ENV["FRACTOR_DEBUG"]
135
142
  @ractor = nil
136
143
  true
137
144
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fractor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ronald Tse