Class: Hatchet::AdminClient
- Inherits:
-
Object
- Object
- Hatchet::AdminClient
- Defined in:
- lib/hatchet-sdk.rb,
sig/hatchet-sdk.rbs
Overview
Admin client for triggering and scheduling workflows.
Delegates to the gRPC admin client for actual RPC calls, while handling context variable propagation for parent-child workflow linking.
Instance Method Summary collapse
-
#initialize(client:) ⇒ AdminClient
constructor
A new instance of AdminClient.
-
#schedule_workflow(workflow, time, input: {}, options: nil) ⇒ Object
Schedule a workflow for future execution.
-
#trigger_workflow(workflow_or_task, input, options: nil) ⇒ Hash
Trigger a workflow run and wait for result.
-
#trigger_workflow_many(workflow_or_task, items, return_exceptions: false) ⇒ Array
Trigger many workflow runs and wait for all results.
-
#trigger_workflow_many_no_wait(workflow_or_task, items) ⇒ Array<WorkflowRunRef>
Trigger many workflow runs without waiting.
-
#trigger_workflow_no_wait(workflow_or_task, input, options: nil) ⇒ WorkflowRunRef
Trigger a workflow run without waiting for the result.
Constructor Details
#initialize(client:) ⇒ AdminClient
Returns a new instance of AdminClient.
312 313 314 315 |
# File 'lib/hatchet-sdk.rb', line 312 def initialize(client:) @client = client @spawn_indices = ContextVars::SpawnIndexTracker.new end |
Instance Method Details
#schedule_workflow(workflow, time, input: {}, options: nil) ⇒ Object
Schedule a workflow for future execution.
410 411 412 413 414 |
# File 'lib/hatchet-sdk.rb', line 410 def schedule_workflow(workflow, time, input: {}, options: nil) name = workflow.respond_to?(:name) ? workflow.name : workflow.to_s opts = () @client.admin_grpc.schedule_workflow(name, run_at: time, input: input, options: opts) end |
#trigger_workflow(workflow_or_task, input, options: nil) ⇒ Hash
Trigger a workflow run and wait for result.
323 324 325 326 |
# File 'lib/hatchet-sdk.rb', line 323 def trigger_workflow(workflow_or_task, input, options: nil) ref = trigger_workflow_no_wait(workflow_or_task, input, options: ) ref.result end |
#trigger_workflow_many(workflow_or_task, items, return_exceptions: false) ⇒ Array
Trigger many workflow runs and wait for all results.
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/hatchet-sdk.rb', line 354 def trigger_workflow_many(workflow_or_task, items, return_exceptions: false) refs = trigger_workflow_many_no_wait(workflow_or_task, items) # Collect results concurrently using threads so that all subscriptions # are sent at once rather than serially waiting for each one. threads = refs.map do |ref| Thread.new do if return_exceptions begin ref.result rescue StandardError => e e end else ref.result end end end threads.map(&:value) end |
#trigger_workflow_many_no_wait(workflow_or_task, items) ⇒ Array<WorkflowRunRef>
Trigger many workflow runs without waiting.
Uses bulk gRPC triggering for efficiency (batched by 1000).
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/hatchet-sdk.rb', line 383 def trigger_workflow_many_no_wait(workflow_or_task, items) name = workflow_or_task.respond_to?(:name) ? workflow_or_task.name : workflow_or_task.to_s # Build trigger items with context vars for parent-child linking trigger_items = items.map do |item| input = item[:input] || {} opts = (item[:options]) { input: input, options: opts } end run_ids = @client.admin_grpc.bulk_trigger_workflow(name, trigger_items) run_ids.map do |run_id| WorkflowRunRef.new( workflow_run_id: run_id, client: @client, listener: @client.workflow_run_listener, ) end end |
#trigger_workflow_no_wait(workflow_or_task, input, options: nil) ⇒ WorkflowRunRef
Trigger a workflow run without waiting for the result.
334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/hatchet-sdk.rb', line 334 def trigger_workflow_no_wait(workflow_or_task, input, options: nil) name = workflow_or_task.respond_to?(:name) ? workflow_or_task.name : workflow_or_task.to_s # Merge user options with context vars for parent-child linking opts = () run_id = @client.admin_grpc.trigger_workflow(name, input: input, options: opts) WorkflowRunRef.new( workflow_run_id: run_id, client: @client, listener: @client.workflow_run_listener, ) end |