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. This can be changed by passing a ‘history_max_size’ option to the storage when initializing ruote (‘history_max_size’ => 0) is acceptable.

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.



52
53
54
55
56
57
58
59
60
# File 'lib/ruote/log/default_history.rb', line 52

def initialize(context, options={})

  @context = context
  @options = options

  @max_size = context['history_max_size'] || DEFAULT_MAX_SIZE

  @history = []
end

Instance Method Details

#allObject

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



64
65
66
67
# File 'lib/ruote/log/default_history.rb', line 64

def all

  @history
end

#by_date(date) ⇒ Object



106
107
108
109
110
111
# File 'lib/ruote/log/default_history.rb', line 106

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.



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

def by_process(wfid)

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

#clear!Object

Forgets all the stored msgs.



119
120
121
122
# File 'lib/ruote/log/default_history.rb', line 119

def clear!

  @history.clear
end

#each(&block) ⇒ Object

Enabling Enumerable…



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

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.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ruote/log/default_history.rb', line 127

def on_msg(msg)

  return if @max_size < 1

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

  @history << msg

  while (@history.size > @max_size) do
    @history.shift
  end

rescue => e

  $stderr.puts '>' + '-' * 79
  $stderr.puts "#{self.class} issue, skipping"
  $stderr.puts e.inspect
  $stderr.puts e.backtrace[0, 2]
  $stderr.puts '<' + '-' * 79
end

#rangeObject

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



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

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.



78
79
80
81
82
83
# File 'lib/ruote/log/default_history.rb', line 78

def wfids

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