@@ -49,20 +49,47 @@ def activity_executor(executor_name)
4949 # @param cancel_raise [Boolean] Whether to raise.
5050 def activity_cancel_raise ( cancel_raise )
5151 unless cancel_raise . is_a? ( TrueClass ) || cancel_raise . is_a? ( FalseClass )
52- raise ArgumentError ,
53- 'Must be a boolean'
52+ raise ArgumentError , 'Must be a boolean'
5453 end
5554
5655 @activity_cancel_raise = cancel_raise
5756 end
57+
58+ # Set an activity as dynamic. Dynamic activities do not have names and handle any activity that is not otherwise
59+ # registered. A worker can only have one dynamic activity. It is often useful to use {activity_raw_args} with
60+ # this.
61+ #
62+ # @param value [Boolean] Whether the activity is dynamic.
63+ def activity_dynamic ( value = true ) # rubocop:disable Style/OptionalBooleanParameter
64+ raise ArgumentError , 'Must be a boolean' unless value . is_a? ( TrueClass ) || value . is_a? ( FalseClass )
65+
66+ @activity_dynamic = value
67+ end
68+
69+ # Have activity arguments delivered to `execute` as {Converters::RawValue}s. These are wrappers for the raw
70+ # payloads that have not been converted to types (but they have been decoded by the codec if present). They can
71+ # be converted with {Context#payload_converter}.
72+ #
73+ # @param value [Boolean] Whether the activity accepts raw arguments.
74+ def activity_raw_args ( value = true ) # rubocop:disable Style/OptionalBooleanParameter
75+ raise ArgumentError , 'Must be a boolean' unless value . is_a? ( TrueClass ) || value . is_a? ( FalseClass )
76+
77+ @activity_raw_args = value
78+ end
5879 end
5980
6081 # @!visibility private
6182 def self . _activity_definition_details
83+ activity_name = @activity_name
84+ raise 'Cannot have activity name specified for dynamic activity' if activity_name && @activity_dynamic
85+
86+ # Default to unqualified class name if not dynamic
87+ activity_name ||= name . to_s . split ( '::' ) . last unless @activity_dynamic
6288 {
63- activity_name : @activity_name || name . to_s . split ( '::' ) . last ,
89+ activity_name :,
6490 activity_executor : @activity_executor || :default ,
65- activity_cancel_raise : @activity_cancel_raise . nil? ? true : @activity_cancel_raise
91+ activity_cancel_raise : @activity_cancel_raise . nil? ? true : @activity_cancel_raise ,
92+ activity_raw_args : @activity_raw_args . nil? ? false : @activity_raw_args
6693 }
6794 end
6895
@@ -75,7 +102,7 @@ def execute(*args)
75102 # Definition info of an activity. Activities are usually classes/instances that extend {Definition}, but
76103 # definitions can also be manually created with a block via {initialize} here.
77104 class Info
78- # @return [String, Symbol] Name of the activity.
105+ # @return [String, Symbol, nil ] Name of the activity, or nil if the activity is dynamic .
79106 attr_reader :name
80107
81108 # @return [Proc] Proc for the activity.
@@ -87,6 +114,9 @@ class Info
87114 # @return [Boolean] Whether to raise in thread/fiber on cancellation. Default is `true`.
88115 attr_reader :cancel_raise
89116
117+ # @return [Boolean] Whether to use {Converters::RawValue}s as arguments.
118+ attr_reader :raw_args
119+
90120 # Obtain definition info representing the given activity, which can be a class, instance, or definition info.
91121 #
92122 # @param activity [Definition, Class<Definition>, Info] Activity to get info for.
@@ -105,14 +135,16 @@ def self.from_activity(activity)
105135 new (
106136 name : details [ :activity_name ] ,
107137 executor : details [ :activity_executor ] ,
108- cancel_raise : details [ :activity_cancel_raise ]
138+ cancel_raise : details [ :activity_cancel_raise ] ,
139+ raw_args : details [ :activity_raw_args ]
109140 ) { |*args | activity . new . execute ( *args ) } # Instantiate and call
110141 when Definition
111142 details = activity . class . _activity_definition_details
112143 new (
113144 name : details [ :activity_name ] ,
114145 executor : details [ :activity_executor ] ,
115- cancel_raise : details [ :activity_cancel_raise ]
146+ cancel_raise : details [ :activity_cancel_raise ] ,
147+ raw_args : details [ :activity_raw_args ]
116148 ) { |*args | activity . execute ( *args ) } # Just and call
117149 when Info
118150 activity
@@ -123,17 +155,19 @@ def self.from_activity(activity)
123155
124156 # Manually create activity definition info. Most users will use an instance/class of {Definition}.
125157 #
126- # @param name [String, Symbol] Name of the activity.
158+ # @param name [String, Symbol, nil ] Name of the activity or nil for dynamic activity.
127159 # @param executor [Symbol] Name of the executor.
128160 # @param cancel_raise [Boolean] Whether to raise in thread/fiber on cancellation.
161+ # @param raw_args [Boolean] Whether to use {Converters::RawValue}s as arguments.
129162 # @yield Use this block as the activity.
130- def initialize ( name :, executor : :default , cancel_raise : true , &block )
163+ def initialize ( name :, executor : :default , cancel_raise : true , raw_args : false , &block )
131164 @name = name
132165 raise ArgumentError , 'Must give block' unless block_given?
133166
134167 @proc = block
135168 @executor = executor
136169 @cancel_raise = cancel_raise
170+ @raw_args = raw_args
137171 end
138172 end
139173 end
0 commit comments