Class: LcpRuby::BackgroundJobs::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/lcp_ruby/background_jobs/definition.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attrs) ⇒ Definition

Returns a new instance of Definition.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 10

def initialize(**attrs)
  @name = attrs[:name].to_s
  @handler_type = attrs[:handler_type]&.to_s
  @handler_class = attrs[:handler_class]&.to_s
  @handler_config = attrs[:handler_config] || {}
  @queue = attrs[:queue]&.to_s || "default"
  @retry_count = attrs[:retry_count] || 3
  @retry_interval = attrs[:retry_interval]&.to_s || "exponential"
  @timeout = attrs[:timeout]
  @unique_by = attrs[:unique_by]
  @schedule = attrs[:schedule]&.to_s
  @steps = attrs[:steps] || []
  @on_step_error = attrs[:on_step_error]&.to_s || "stop"
  @triggers = attrs[:triggers] || []
  @params = attrs[:params] || {}
  @source_path = attrs[:source_path]
  @source_type = attrs[:source_type]

  validate!
end

Instance Attribute Details

#handler_classObject (readonly)

Returns the value of attribute handler_class.



4
5
6
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 4

def handler_class
  @handler_class
end

#handler_configObject (readonly)

Returns the value of attribute handler_config.



4
5
6
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 4

def handler_config
  @handler_config
end

#handler_typeObject (readonly)

Returns the value of attribute handler_type.



4
5
6
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 4

def handler_type
  @handler_type
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 4

def name
  @name
end

#on_step_errorObject (readonly)

Returns the value of attribute on_step_error.



4
5
6
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 4

def on_step_error
  @on_step_error
end

#paramsObject (readonly)

Returns the value of attribute params.



4
5
6
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 4

def params
  @params
end

#queueObject (readonly)

Returns the value of attribute queue.



4
5
6
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 4

def queue
  @queue
end

#retry_countObject (readonly)

Returns the value of attribute retry_count.



4
5
6
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 4

def retry_count
  @retry_count
end

#retry_intervalObject (readonly)

Returns the value of attribute retry_interval.



4
5
6
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 4

def retry_interval
  @retry_interval
end

#scheduleObject (readonly)

Returns the value of attribute schedule.



4
5
6
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 4

def schedule
  @schedule
end

#source_pathObject

Returns the value of attribute source_path.



8
9
10
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 8

def source_path
  @source_path
end

#source_typeObject

Returns the value of attribute source_type.



8
9
10
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 8

def source_type
  @source_type
end

#stepsObject (readonly)

Returns the value of attribute steps.



4
5
6
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 4

def steps
  @steps
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



4
5
6
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 4

def timeout
  @timeout
end

#triggersObject (readonly)

Returns the value of attribute triggers.



4
5
6
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 4

def triggers
  @triggers
end

#unique_byObject (readonly)

Returns the value of attribute unique_by.



4
5
6
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 4

def unique_by
  @unique_by
end

Class Method Details

.from_hash(hash) ⇒ Object



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
64
65
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 31

def self.from_hash(hash)
  hash = hash.transform_keys(&:to_s)

  handler = hash["handler"]
  handler_type, handler_class, handler_config = normalize_handler(handler)

  steps = normalize_steps(hash["steps"])

  retry_count = hash["retry"] || hash["retry_count"] || 3
  retry_interval = hash["retry_interval"] || "exponential"

  triggers = (hash["triggers"] || []).map { |t| t.transform_keys(&:to_s) }
  params = hash["params"] || {}
  params = params.transform_keys(&:to_s) if params.is_a?(Hash)

  unique_by = hash["unique_by"]
  unique_by = unique_by.map(&:to_s) if unique_by.is_a?(Array)

  new(
    name: hash["name"],
    handler_type: handler_type,
    handler_class: handler_class,
    handler_config: handler_config,
    queue: hash["queue"],
    retry_count: retry_count.to_i,
    retry_interval: retry_interval,
    timeout: hash["timeout"]&.to_i,
    unique_by: unique_by,
    schedule: hash["schedule"],
    steps: steps,
    on_step_error: hash["on_step_error"],
    triggers: triggers,
    params: params
  )
end

Instance Method Details

#class_handler?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 67

def class_handler?
  handler_type == "class"
end

#declarative_handler?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 71

def declarative_handler?
  handler_type == "declarative"
end

#has_unique_by?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 83

def has_unique_by?
  unique_by.is_a?(Array) && unique_by.any?
end

#multi_step?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 75

def multi_step?
  steps.any?
end

#scheduled?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/lcp_ruby/background_jobs/definition.rb', line 79

def scheduled?
  schedule.present?
end