Sha256: 446608d24745be2c9eef575f7c2ae999d616bb5503b9209a22f6bc8ad7fb188a

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

module OwskiLog
  module Status
    OK = 'OK'
    ERROR = 'ERROR'
  end

  class Event
    attr_accessor :key, :description,
                  :status, :cause,
                  :parent, :start_time,
                  :end_time, :sub_events

    def initialize(key, description,
                   status: nil, cause: nil,
                   parent: nil, start_time: Time.now.utc,
                   end_time: nil, sub_events: [])
      @key = key
      @description = description
      @status = status
      @cause = cause
      @parent = parent
      @start_time = start_time
      @end_time = end_time
      @sub_events = sub_events
    end

    def finish(status, cause: nil)
      @end_time = Time.now.utc
      @status = status
      @cause = cause
      self
    end

    def is_finished?
      @end_time && @status
    end

    def to_json(*a)
      {
          key: @key,
          description: @description,
          status: @status,
          start_time: @start_time,
          end_time: @end_time,
          sub_events: @sub_events
      }.to_json(*a)
    end
  end

  module EventManager
    def self.register_event(event_collection, event, parent_key: nil)
      raise ArgumentError.new('You can only register OwskiLog::Event.') unless event.is_a? Event

      if parent_key != nil
        event.parent = find_event(event_collection,parent_key)
      end

      event_collection[event.key] = event
    end

    def self.find_event(event_collection, event_key)
      event_collection[event_key]
    end

    def self.finish_event(event_collection, event_key, status, cause: nil)
      ev = find_event(event_collection, event_key)
      if ev
        ev.finish(status, cause: cause)
        if ev.parent != nil && cause != nil
          ev.parent.finish(status, cause: cause)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
owskilog-0.2.2 lib/owskilog/event.rb