Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rails_app/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@

# Log to STDOUT
config.logger = ActiveSupport::TaggedLogging.logger(STDOUT)
config.log_level = "error"
end
9 changes: 7 additions & 2 deletions rails_app/lib/temporal_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ def self.instance

# Load config and apply defaults
args, kwargs = Temporalio::EnvConfig::ClientConfig.load_client_connect_options
args[0] ||= 'localhost:7233' # Default address
args[1] ||= 'default' # Default namespace

@instance = Temporalio::Client.connect(*args, **kwargs, logger: Rails.logger)
@instance = Temporalio::Client.connect(self.server_target, args[1], **kwargs, logger: Rails.logger)
end

def self.server_target
args, _kwargs = Temporalio::EnvConfig::ClientConfig.load_client_connect_options
server = args[0]
server || 'localhost:7233'
end

def self.instance=(instance)
Expand Down
10 changes: 10 additions & 0 deletions rails_app/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ class TestCase

# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all

parallelize(workers: :number_of_processors, threshold: 1)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work due to us not supporting using native backed Ruby structures across forks: https://ruby.temporal.io/#forking

This can be worked around by setting up a Temporal::Client for each process.

Suggested change
parallelize(workers: :number_of_processors, threshold: 1)
parallelize(workers: :number_of_processors, threshold: 1)
# Create a fresh runtime and client in each forked worker,
# connecting to the same dev server started in the parent.
parallelize_setup do |_worker|
TemporalClient.instance = Temporalio::Client.connect(
TemporalClient.server_target,
'default',
runtime: Temporalio::Runtime.new,
logger: Rails.logger
)
end

Note, this requires adding server_target getter/setter on the TemporalClient module in this sample.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick response @chris-olszewski ! I tried your sample code with parallelize_setup and I'm still encountering a few other issues:

  1. RuntimeError: Client already set
  2. Temporalio::Internal::Bridge::Error: Cannot create worker across forks (original runtime PID is 74216, current is 74376)
  3. The test suite freezes before finishing until i kill it

I pushed up the changes I made in case I got lost on the server_target implementation. Appreciate your help!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I apologize. I accidentally was using a build of the gem where I was leaking all of the native owned resources (which avoids the issue in a very dumb way).

The Temporalio::Testing::WorkflowEnvironment creates a new Runtime which can't be shared across processes, so that in addition to the client need to be setup per process.
e.g.

    parallelize_setup do |_worker|
      @temporal_test_env = Temporalio::Testing::WorkflowEnvironment.start_local(logger: Rails.logger)
      TemporalClient.instance = @temporal_test_env.client
    end
    parallelize_teardown do |_worker|
      @temporal_test_env&.shutdown
    end

And remove the module level test server start (and the temporal_client.rs changes are no longer necessary).

I realize this isn't great as you're starting a dev server per-process, but it should prevent sharing any native backed objects across forks.

parallelize_setup do |_worker|
TemporalClient.instance = Temporalio::Client.connect(
TemporalClient.server_target,
'default',
runtime: Temporalio::Runtime.new,
logger: Rails.logger
)
end
end
end

Expand Down