Class: Burner::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/burner/payload.rb

Overview

The input for all Job#perform methods. The main notion of this object is its ‘registers’ attribute. This registers attribute is a key-indifferent hash, accessible on Payload using the brackets setter and getter methods. This is dynamic and weak on purpose and is subject to whatever the Job#perform methods decides it is. This definitely adds an order-of-magnitude complexity to this whole library and lifecycle, but I am not sure there is any other way around it: trying to build a generic, open-ended processing pipeline to serve almost any use case.

The side_effects attribute can also be utilized as a way for jobs to emit any data in a more structured/additive manner. The initial use case for this was for Burner’s core IO jobs to report back the files it has written in a more structured data way (as opposed to simply writing some information to the output.)

The ‘time’ attribute is important in that it should for the replaying of pipelines and jobs. Instead of having job’s utilizing Time.now, Date.today, etc… they should rather opt to use this value instead.

The notion of params are somewhat conflated with registers here. Both are hashes and both serve as data stores. The difference is registers are really meant to be the shared-data repository across jobs, while params are, more or less, the input into the pipeline. It is debatable if mutation of the params should be allowed but the design decision was made early on to allow for params to be mutable albeit with registers being the preferred mutable store.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params: {}, registers: {}, side_effects: [], time: Time.now.utc) ⇒ Payload

Returns a new instance of Payload.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/burner/payload.rb', line 40

def initialize(
  params: {},
  registers: {},
  side_effects: [],
  time: Time.now.utc
)
  @params       = Data.new(params)
  @registers    = Data.new(registers)
  @side_effects = side_effects || []
  @time         = time || Time.now.utc
end

Instance Attribute Details

#side_effectsObject (readonly)

Returns the value of attribute side_effects.



37
38
39
# File 'lib/burner/payload.rb', line 37

def side_effects
  @side_effects
end

#timeObject (readonly)

Returns the value of attribute time.



37
38
39
# File 'lib/burner/payload.rb', line 37

def time
  @time
end

Instance Method Details

#[](key) ⇒ Object

Retrieve a register’s value.



86
87
88
# File 'lib/burner/payload.rb', line 86

def [](key)
  @registers[key]
end

#[]=(key, value) ⇒ Object

Set a register’s value.



81
82
83
# File 'lib/burner/payload.rb', line 81

def []=(key, value)
  @registers[key] = value
end

#add_side_effect(side_effect) ⇒ Object

Add a side effect of a job. This helps to keep track of things jobs do outside of its register mutations.



76
77
78
# File 'lib/burner/payload.rb', line 76

def add_side_effect(side_effect)
  tap { side_effects << side_effect }
end

#halt_pipelineObject

Set halt_pipeline to true. This will indicate to the pipeline to stop all subsequent processing.



92
93
94
# File 'lib/burner/payload.rb', line 92

def halt_pipeline
  @halt_pipeline = true
end

#halt_pipeline?Boolean

Check and see if halt_pipeline was called.

Returns:

  • (Boolean)


97
98
99
# File 'lib/burner/payload.rb', line 97

def halt_pipeline?
  @halt_pipeline || false
end

#param(key) ⇒ Object

Law of Demeter: While params is an accessible hash, it is preferred that the public class methods are used.



66
67
68
# File 'lib/burner/payload.rb', line 66

def param(key)
  @params[key]
end

#paramsObject

Backwards compatibility. This allows for control over the underlying data structure while still maintaining the same public API as before.



54
55
56
# File 'lib/burner/payload.rb', line 54

def params
  @params.to_h
end

#params_and_registers_hashObject



101
102
103
104
105
106
# File 'lib/burner/payload.rb', line 101

def params_and_registers_hash
  registers_hash = @registers.transform_keys { |key| "__#{key}_register" }
  params_hash    = @params.to_h

  params_hash.merge(registers_hash)
end

#registersObject

Backwards compatibility. This allows for control over the underlying data structure while still maintaining the same public API as before.



60
61
62
# File 'lib/burner/payload.rb', line 60

def registers
  @registers.to_h
end

#update_param(key, value) ⇒ Object



70
71
72
# File 'lib/burner/payload.rb', line 70

def update_param(key, value)
  tap { @params[key] = value }
end