Class: Temporalio::SimplePlugin

Inherits:
Object
  • Object
show all
Includes:
Client::Plugin, Worker::Plugin
Defined in:
lib/temporalio/simple_plugin.rb

Overview

Plugin that implements both Client::Plugin and Worker::Plugin and provides a simplified common set of settings for configuring both.

WARNING: Plugins are experimental.

Defined Under Namespace

Classes: Options

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, data_converter: nil, client_interceptors: nil, activities: nil, workflows: nil, worker_interceptors: nil, workflow_failure_exception_types: nil, run_context: nil) ⇒ SimplePlugin

Create a simple plugin.

Parameters:

  • name (String)

    Required string name for this plugin.

  • data_converter (Converters::DataConverter, Proc, nil) (defaults to: nil)

    Data converter to apply to clients and workflow replayers. This can be a proc that accepts the existing data converter and returns a new one.

  • client_interceptors (Array<Client::Integerceptor>, Proc, nil) (defaults to: nil)

    Client interceptors that are appended to the existing client set (which means if they implement worker interceptors they are applied for the workers too). A proc can be provided that accepts the existing array and returns a new one.

  • activities (Array<Activity::Definition, Class<Activity::Definition>, Activity::Definition::Info>, Proc, nil) (defaults to: nil)

    Activities to append to each worker activity set. A proc can be provided that accepts the existing array and returns a new one.

  • workflows (Array<Class<Workflow::Definition>>, Proc, nil) (defaults to: nil)

    Workflows to append to each worker workflow set. A proc can be provided that accepts the existing array and returns a new one.

  • worker_interceptors (Array<Interceptor::Activity, Interceptor::Workflow>, Proc, nil) (defaults to: nil)

    Worker interceptors that are appended to the existing worker or workflow replayer set. A proc can be provided that accepts the existing array and returns a new one.

  • workflow_failure_exception_types (Array<Class<Exception>>) (defaults to: nil)

    Workflow failure exception types that are appended to the existing worker or workflow replayer set. A proc can be provided that accepts the existing array and returns a new one.

  • run_context (Proc, nil) (defaults to: nil)

    A proc that intercepts both #run_worker or #with_workflow_replay_worker. The proc should accept two positional parameters: options and next_call. The options are either Worker::Plugin::RunWorkerOptions or Worker::Plugin::WithWorkflowReplayWorkerOptions. The next_call is a proc itself that accepts the options and returns a value. This run_context proc should return the result of the next_call.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/temporalio/simple_plugin.rb', line 56

def initialize(
  name:,
  data_converter: nil,
  client_interceptors: nil,
  activities: nil,
  workflows: nil,
  worker_interceptors: nil,
  workflow_failure_exception_types: nil,
  run_context: nil
)
  @options = Options.new(
    name:,
    data_converter:,
    client_interceptors:,
    activities:,
    workflows:,
    worker_interceptors:,
    workflow_failure_exception_types:,
    run_context:
  ).freeze
end

Instance Attribute Details

#optionsOptions (readonly)

Returns Frozen options for this plugin which has the same attributes as #initialize.

Returns:

  • (Options)

    Frozen options for this plugin which has the same attributes as #initialize.



30
31
32
# File 'lib/temporalio/simple_plugin.rb', line 30

def options
  @options
end

Instance Method Details

#configure_client(options) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/temporalio/simple_plugin.rb', line 84

def configure_client(options)
  if (data_converter = _single_option(new: @options.data_converter, existing: options.data_converter,
                                      type: Converters::DataConverter, name: 'data converter'))
    options = options.with(data_converter:)
  end
  if (interceptors = _array_option(new: @options.client_interceptors, existing: options.interceptors,
                                   name: 'client interceptor'))
    options = options.with(interceptors:)
  end
  options
end

#configure_worker(options) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/temporalio/simple_plugin.rb', line 102

def configure_worker(options)
  if (activities = _array_option(new: @options.activities, existing: options.activities, name: 'activity'))
    options = options.with(activities:)
  end
  if (workflows = _array_option(new: @options.workflows, existing: options.workflows, name: 'workflow'))
    options = options.with(workflows:)
  end
  if (interceptors = _array_option(new: @options.worker_interceptors, existing: options.interceptors,
                                   name: 'worker interceptor'))
    options = options.with(interceptors:)
  end
  if (workflow_failure_exception_types = _array_option(new: @options.workflow_failure_exception_types,
                                                       existing: options.workflow_failure_exception_types,
                                                       name: 'workflow failure exception types'))
    options = options.with(workflow_failure_exception_types:)
  end
  options
end

#configure_workflow_replayer(options) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/temporalio/simple_plugin.rb', line 131

def configure_workflow_replayer(options)
  if (data_converter = _single_option(new: @options.data_converter, existing: options.data_converter,
                                      type: Converters::DataConverter, name: 'data converter'))
    options = options.with(data_converter:)
  end
  if (workflows = _array_option(new: @options.workflows, existing: options.workflows, name: 'workflow'))
    options = options.with(workflows:)
  end
  if (interceptors = _array_option(new: @options.worker_interceptors, existing: options.interceptors,
                                   name: 'worker interceptor'))
    options = options.with(interceptors:)
  end
  if (workflow_failure_exception_types = _array_option(new: @options.workflow_failure_exception_types,
                                                       existing: options.workflow_failure_exception_types,
                                                       name: 'workflow failure exception types'))
    options = options.with(workflow_failure_exception_types:)
  end
  options
end

#connect_client(options, next_call) ⇒ Object



97
98
99
# File 'lib/temporalio/simple_plugin.rb', line 97

def connect_client(options, next_call)
  next_call.call(options)
end

#nameObject



79
80
81
# File 'lib/temporalio/simple_plugin.rb', line 79

def name
  @options.name
end

#run_worker(options, next_call) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/temporalio/simple_plugin.rb', line 122

def run_worker(options, next_call)
  if @options.run_context
    @options.run_context.call(options, next_call) # steep:ignore NoMethod
  else
    next_call.call(options)
  end
end

#with_workflow_replay_worker(options, next_call) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/temporalio/simple_plugin.rb', line 152

def with_workflow_replay_worker(options, next_call)
  if @options.run_context
    @options.run_context.call(options, next_call) # steep:ignore NoMethod
  else
    next_call.call(options)
  end
end