Class: Ruote::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/ruote/svc/tracker.rb

Overview

The tracker service is used by the “listen” expression. This services sees all the msg processed by a worker and triggers any listener interested in a particular msg.

Look at the ListenExpression for more details.

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Tracker

Returns a new instance of Tracker.



37
38
39
40
# File 'lib/ruote/svc/tracker.rb', line 37

def initialize(context)

  @context = context
end

Instance Method Details

#add_tracker(wfid, action, tracker_id, conditions, msg) ⇒ Object

Adds a tracker (usually when a ‘listen’ expression gets applied).

The tracker_id may be nil (one will then get generated).

Returns the tracker_id.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ruote/svc/tracker.rb', line 64

def add_tracker(wfid, action, tracker_id, conditions, msg)

  tracker_id ||= [
    'tracker', wfid, action,
    Ruote.generate_subid(conditions.hash.to_s + msg.hash.to_s)
  ].collect(&:to_s).join('_')

  conditions =
    conditions && conditions.remap { |(k, v), h| h[k] = Array(v) }

  doc = @context.storage.get_trackers

  doc['trackers'][tracker_id] =
    { 'wfid' => wfid,
      'action' => action,
      'id' => tracker_id,
      'conditions' => conditions,
      'msg' => msg }

  r = @context.storage.put(doc)

  add_tracker(wfid, action, tracker_id, conditions, msg) if r
    # the put failed, have to redo the work

  tracker_id
end

#on_msg(msg) ⇒ Object

The worker calls this method via the context after each successful msg processing.



53
54
55
56
# File 'lib/ruote/svc/tracker.rb', line 53

def on_msg(msg)

  on_message(false, msg)
end

#on_pre_msg(msg) ⇒ Object

The worker calls this method via the context before each msg gets processed.



45
46
47
48
# File 'lib/ruote/svc/tracker.rb', line 45

def on_pre_msg(msg)

  on_message(true, msg)
end

#remove_tracker(fei_sid_or_id, wfid = nil) ⇒ Object

Removes a tracker (usually when a ‘listen’ expression replies to its parent expression or is cancelled).



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ruote/svc/tracker.rb', line 94

def remove_tracker(fei_sid_or_id, wfid=nil)

  tracker_id =
    if fei_sid_or_id.is_a?(String)
      fei_sid_or_id
    else
      Ruote.to_storage_id(fei_sid_or_id)
    end

  remove([ tracker_id ], wfid)
end