Class: DirtyPipeline::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/dirty_pipeline/event.rb

Constant Summary collapse

NEW =
"new".freeze
START =
"started".freeze
FAILURE =
"failed".freeze
ABORT =
"aborted".freeze
RETRY =
"retry".freeze
SUCCESS =
"succeeded".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, data: nil, error: nil) ⇒ Event

Returns a new instance of Event.



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
# File 'lib/dirty_pipeline/event.rb', line 25

def initialize(options = {}, data: nil,  error: nil)
  unless options.empty?
    options_hash = options.to_h
    data  ||= options_hash["data"]
    error ||= options_hash["error"]
  end

  data_hash = data.to_h

  @tx_id     = data_hash.fetch("transaction_uuid")
  @id        = data_hash.fetch("uuid")
  transition = data_hash.fetch("transition")
  args       = data_hash.fetch("args").to_a
  @data = {
    "uuid" => @id,
    "transaction_uuid" => @tx_id,
    "transition" => transition,
    "args" => args,
    "created_at" => Time.now,
    "cache" => {},
    "attempts_count" => 1,
    "status" => NEW,
  }.merge(data_hash)
  @error = error
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



24
25
26
# File 'lib/dirty_pipeline/event.rb', line 24

def data
  @data
end

#errorObject (readonly)

Returns the value of attribute error.



24
25
26
# File 'lib/dirty_pipeline/event.rb', line 24

def error
  @error
end

#idObject (readonly)

Returns the value of attribute id.



24
25
26
# File 'lib/dirty_pipeline/event.rb', line 24

def id
  @id
end

#tx_idObject (readonly)

Returns the value of attribute tx_id.



24
25
26
# File 'lib/dirty_pipeline/event.rb', line 24

def tx_id
  @tx_id
end

Class Method Details

.create(transition, *args, tx_id:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dirty_pipeline/event.rb', line 12

def self.create(transition, *args, tx_id:)
  new(
    data: {
      # FIXME: SecureRandom
      "uuid" => SecureRandom.uuid,
      "transaction_uuid" => tx_id,
      "transition" => transition,
      "args" => args,
    }
  )
end

Instance Method Details

#attempt_retry!Object



82
83
84
85
# File 'lib/dirty_pipeline/event.rb', line 82

def attempt_retry!
  @data["updated_at"] = Time.now
  @data["attempts_count"] = attempts_count + 1
end

#attempts_countObject



78
79
80
# File 'lib/dirty_pipeline/event.rb', line 78

def attempts_count
  @data["attempts_count"].to_i
end

#complete(changes, destination) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/dirty_pipeline/event.rb', line 87

def complete(changes, destination)
  @data.merge!(
    "destination" => destination,
    "changes" => changes,
    "updated_at" => Time.now,
    "status" => SUCCESS,
  )
end


69
70
71
72
73
74
75
76
# File 'lib/dirty_pipeline/event.rb', line 69

def link_exception(exception)
  @error = {
    "exception" => exception.class.to_s,
    "exception_message" => exception.message,
    "created_at" => Time.now,
  }
  failure!
end

#to_hObject



51
52
53
# File 'lib/dirty_pipeline/event.rb', line 51

def to_h
  {data: @data, error: @error}
end