Module: Hatchet::ConditionConverter

Defined in:
lib/hatchet/condition_converter.rb

Overview

Shared logic for converting condition objects into protobuf messages.

Used by both Task (for workflow registration) and DurableContext (for runtime durable event registration) to avoid duplicating the condition processing logic.

Class Method Summary collapse

Class Method Details

.convert_condition(cond, action:, sleep_conditions:, user_event_conditions:, or_group_id: nil, readable_data_key: nil, proto_method: :to_proto, proto_arg: nil, config: nil) ⇒ Object

Convert a single condition into the appropriate protobuf message and append it to the provided accumulator arrays.

Parameters:

  • cond (Object)

    The condition (SleepCondition, UserEventCondition, Hash, or duck-typed)

  • action (Symbol)

    Proto action (:QUEUE or :SKIP)

  • sleep_conditions (Array)

    Accumulator for sleep condition protos

  • user_event_conditions (Array)

    Accumulator for user event condition protos

  • or_group_id (String, nil) (defaults to: nil)

    Shared OR group ID (defaults to a new UUID)

  • readable_data_key (String, nil) (defaults to: nil)

    Override for Hash readable_data_key (used by durable context)

  • proto_method (Symbol) (defaults to: :to_proto)

    Method to check for self-converting conditions (:to_proto or :to_durable_proto)

  • proto_arg (Object, nil) (defaults to: nil)

    Argument to pass to proto_method (action for :to_proto, key for :to_durable_proto)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hatchet/condition_converter.rb', line 25

def convert_condition(cond, action:, sleep_conditions:, user_event_conditions:,
                      or_group_id: nil, readable_data_key: nil,
                      proto_method: :to_proto, proto_arg: nil, config: nil)
  or_group_id ||= SecureRandom.uuid

  if cond.respond_to?(proto_method)
    # Condition object knows how to convert itself
    proto = cond.public_send(proto_method, proto_arg || action)
    case proto
    when ::V1::SleepMatchCondition
      sleep_conditions << proto
    when ::V1::UserEventMatchCondition
      user_event_conditions << proto
    end
  elsif cond.is_a?(Hatchet::SleepCondition)
    convert_sleep_condition(cond.duration, action: action, or_group_id: or_group_id,
                                           sleep_conditions: sleep_conditions,)
  elsif cond.is_a?(Hatchet::UserEventCondition)
    convert_user_event_condition(cond.event_key, action: action, or_group_id: or_group_id,
                                                 expression: cond.expression || "",
                                                 user_event_conditions: user_event_conditions,
                                                 config: config,)
  elsif cond.is_a?(Hash)
    convert_hash_condition(cond, action: action, or_group_id: or_group_id,
                                 readable_data_key: readable_data_key,
                                 sleep_conditions: sleep_conditions,
                                 user_event_conditions: user_event_conditions,
                                 config: config,)
  elsif cond.respond_to?(:event_key) && cond.event_key
    expression = cond.respond_to?(:expression) ? (cond.expression || "") : ""
    convert_user_event_condition(cond.event_key, action: action, or_group_id: or_group_id,
                                                 expression: expression,
                                                 user_event_conditions: user_event_conditions,
                                                 config: config,)
  elsif cond.respond_to?(:duration) && cond.duration
    convert_sleep_condition(cond.duration, action: action, or_group_id: or_group_id,
                                           sleep_conditions: sleep_conditions,)
  end
end

.convert_hash_condition(cond, action:, or_group_id:, readable_data_key:, sleep_conditions:, user_event_conditions:, config: nil) ⇒ Object

Parameters:

  • cond (Hash)

    Hash-based condition

  • config (Hatchet::Config, nil) (defaults to: nil)

    Config for namespace resolution



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/hatchet/condition_converter.rb', line 103

def convert_hash_condition(cond, action:, or_group_id:, readable_data_key:,
                           sleep_conditions:, user_event_conditions:, config: nil)
  base_key = readable_data_key || cond[:readable_data_key] || cond[:key] || ""

  base_args = {
    readable_data_key: base_key,
    or_group_id: cond[:or_group_id] || or_group_id,
    expression: cond[:expression] || "",
  }
  base_args[:action] = action if action

  base = ::V1::BaseMatchCondition.new(base_args)

  if cond[:sleep_for]
    sleep_conditions << ::V1::SleepMatchCondition.new(
      base: base,
      sleep_for: cond[:sleep_for].to_s,
    )
  elsif cond[:event_key]
    namespaced_key = config ? config.apply_namespace(cond[:event_key]) : cond[:event_key]
    user_event_conditions << ::V1::UserEventMatchCondition.new(
      base: base,
      user_event_key: namespaced_key,
    )
  end
end

.convert_sleep_condition(duration, action:, or_group_id:, sleep_conditions:) ⇒ Object

Parameters:

  • duration (Integer, String)

    Sleep duration (seconds as Integer, or Go duration string like ā€œ15sā€)



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hatchet/condition_converter.rb', line 66

def convert_sleep_condition(duration, action:, or_group_id:, sleep_conditions:)
  sleep_for = duration.is_a?(String) ? duration : "#{duration}s"

  base_args = {
    readable_data_key: "sleep:#{sleep_for}",
    or_group_id: or_group_id,
  }
  base_args[:action] = action if action

  base = ::V1::BaseMatchCondition.new(base_args)
  sleep_conditions << ::V1::SleepMatchCondition.new(
    base: base,
    sleep_for: sleep_for,
  )
end

.convert_user_event_condition(event_key, action:, or_group_id:, expression:, user_event_conditions:, config: nil) ⇒ Object

Parameters:

  • event_key (String)

    Event key

  • config (Hatchet::Config, nil) (defaults to: nil)

    Config for namespace resolution



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/hatchet/condition_converter.rb', line 84

def convert_user_event_condition(event_key, action:, or_group_id:, expression:, user_event_conditions:, config: nil)
  namespaced_key = config ? config.apply_namespace(event_key) : event_key

  base_args = {
    readable_data_key: namespaced_key,
    or_group_id: or_group_id,
    expression: expression,
  }
  base_args[:action] = action if action

  base = ::V1::BaseMatchCondition.new(base_args)
  user_event_conditions << ::V1::UserEventMatchCondition.new(
    base: base,
    user_event_key: namespaced_key,
  )
end