Class: Ruote::DefaultHistory

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

Overview

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

This class includes Enumerable.

NOTE:

this default history is worthless when there are multiple workers. It only keeps track of the msgs processed by the worker in the same context. Msgs processed by other workers (in different Ruby runtimes) are not seen (they are tracked by the DefaultHistory next to those workers).

By default, this history keeps track of the latest 1’000 msgs.

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from Enumerable

#each_with_object

Constructor Details

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

Returns a new instance of DefaultHistory.



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

def initialize(context, options={})

  @context = context
  @options = options

  @history = []
end

Instance Method Details

#allObject

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



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

def all

  @history
end

#by_date(date) ⇒ Object



102
103
104
105
106
107
# File 'lib/ruote/log/default_history.rb', line 102

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.



84
85
86
87
88
89
# File 'lib/ruote/log/default_history.rb', line 84

def by_process(wfid)

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

#clear!Object

Forgets all the stored msgs.



115
116
117
118
# File 'lib/ruote/log/default_history.rb', line 115

def clear!

  @history.clear
end

#each(&block) ⇒ Object

Enabling Enumerable…



67
68
69
70
# File 'lib/ruote/log/default_history.rb', line 67

def each(&block)

  @history.each(&block)
end

#on_msg(msg) ⇒ Object

This method is called by the worker via the context. Successfully processed msgs are passed here.



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ruote/log/default_history.rb', line 123

def on_msg(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).



94
95
96
97
98
99
100
# File 'lib/ruote/log/default_history.rb', line 94

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.



74
75
76
77
78
79
# File 'lib/ruote/log/default_history.rb', line 74

def wfids

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