lib/dirty_pipeline/event.rb in dirty_pipeline-0.5.0 vs lib/dirty_pipeline/event.rb in dirty_pipeline-0.6.0

- old
+ new

@@ -3,12 +3,13 @@ module DirtyPipeline class Event NEW = "new".freeze START = "started".freeze FAILURE = "failed".freeze + ABORT = "aborted".freeze RETRY = "retry".freeze - SUCCESS = "success".freeze + SUCCESS = "succeeded".freeze def self.create(transition, *args, tx_id:) new( data: { "uuid" => Nanoid.generate, @@ -17,37 +18,24 @@ "args" => args, } ) end - def self.load(json) - return unless json - new(JSON.load(json)) - end - - def self.dump(event) - JSON.dump(event.to_h) - end - - def dump - self.class.dump(self) - end - attr_reader :id, :tx_id, :error, :data def initialize(options = {}, data: nil, error: nil) unless options.empty? options_hash = options.to_h data ||= options_hash["data"] error ||= options_hash["error"] - transition = options_hash["transition"] - args = options_hash["args"] end data_hash = data.to_h - @tx_id = data_hash.fetch("transaction_uuid") - @id = data_hash.fetch("uuid") + @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, @@ -65,11 +53,11 @@ %w(args transition cache destination changes).each do |method_name| define_method("#{method_name}") { @data[method_name] } end - %w(new start retry failure).each do |method_name| + %w(new start retry failure success abort).each do |method_name| define_method("#{method_name}?") do @data["status"] == self.class.const_get(method_name.upcase) end define_method("#{method_name}!") do @@ -79,19 +67,19 @@ def link_exception(exception) @error = { "exception" => exception.class.to_s, "exception_message" => exception.message, - "created_at" => Time.current, + "created_at" => Time.now, } failure! end def attempts_count @data["attempts_count"].to_i end - def attempt_retry + def attempt_retry! @data["updated_at"] = Time.now @data["attempts_count"] = attempts_count + 1 end def complete(changes, destination)