Class: Ruote::DefaultHistory

Inherits:
Object
  • Object
show all
Defined in:
lib/ruote/log/default_history.rb

Overview

A default history implementation, only keeps the most recent stuff in memory.

NOTE : this default history is worthless when there are multiple workers. It only keeps track of the ‘local’ worker if there is one present.

Constant Summary collapse

DATE_REGEX =
/!(\d{4}-\d{2}-\d{2})!/
DEFAULT_MAX_SIZE =
1000

Instance Method Summary collapse

Constructor Details

#initialize(context, options = {}) ⇒ DefaultHistory

Returns a new instance of DefaultHistory.



40
41
42
43
44
45
46
47
48
49
# File 'lib/ruote/log/default_history.rb', line 40

def initialize(context, options={})

  @context = context
  @options = options

  @history = []

  @context.worker.subscribe(:all, self) if @context.worker
    # only care about logging if there is a worker present
end

Instance Method Details

#allObject

Returns all the msgs (events), most recent one is last.



53
54
55
56
# File 'lib/ruote/log/default_history.rb', line 53

def all

  @history
end

#by_date(date) ⇒ Object



88
89
90
91
92
93
# File 'lib/ruote/log/default_history.rb', line 88

def by_date(date)

  d = Time.parse(date.to_s).utc.strftime('%Y-%m-%d')

  @history.select { |m| Time.parse(m['seen_at']).strftime('%Y-%m-%d') == d }
end

#by_process(wfid) ⇒ Object Also known as: by_wfid

Returns all the msgs (events) for a given wfid. (Well, all the msgs that are kept.



70
71
72
73
74
75
# File 'lib/ruote/log/default_history.rb', line 70

def by_process(wfid)

  @history.select { |msg|
    (msg['wfid'] || (msg['fei']['wfid'] rescue nil)) == wfid
  }
end

#clear!Object

Forgets all the stored msgs.



101
102
103
104
# File 'lib/ruote/log/default_history.rb', line 101

def clear!

  @history.clear
end

#notify(msg) ⇒ Object

This is the method called by the workqueue. Incoming engine events are ‘processed’ here.



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ruote/log/default_history.rb', line 109

def notify(msg)

  msg = Ruote.fulldup(msg)
  msg['seen_at'] = Ruote.now_to_utc_s

  @history << msg

  while (@history.size > (@options[:max_size] || DEFAULT_MAX_SIZE)) do
    @history.shift
  end
end

#rangeObject

Returns an array [ most recent date, oldest date ] (Time instances).



80
81
82
83
84
85
86
# File 'lib/ruote/log/default_history.rb', line 80

def range

  now = Time.now

  [ (Time.parse(@history.first['seen_at']) rescue now),
    (Time.parse(@history.last['seen_at']) rescue now) ]
end

#wfidsObject

Returns all the wfids for which some piece of history is kept.



60
61
62
63
64
65
# File 'lib/ruote/log/default_history.rb', line 60

def wfids

  @history.collect { |msg|
    msg['wfid'] || (msg['fei']['wfid'] rescue nil)
  }.compact.uniq.sort
end