Class: Flor::Waiter

Inherits:
Object
  • Object
show all
Defined in:
lib/flor/unit/waiter.rb

Constant Summary collapse

ROW_PSEUDO_POINTS =
%w[ status tag tasker var variable ].freeze

Instance Method Summary collapse

Constructor Details

#initialize(exid, opts) ⇒ Waiter

Returns a new instance of Waiter.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/flor/unit/waiter.rb', line 7

def initialize(exid, opts)

  serie, timeout, on_timeout =
    expand_args(opts)

  # TODO fail if the serie mixes msg_waiting with row_waiting...

  @exid = exid
  @serie = serie
  @timeout = timeout
  @on_timeout = on_timeout

  @queue = []
  @mutex = Mutex.new
  @var = ConditionVariable.new

  @executor = nil
end

Instance Method Details

#check(unit, rs) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/flor/unit/waiter.rb', line 68

def check(unit, rs)

  @mutex.synchronize do

    row = nil

    loop do

      break if @serie.empty?

      row = row_match?(unit, rs)
      return false unless row

      @serie.shift
    end

    @queue << [ unit, row ]
    @var.signal
  end

  true # serie over, remove me

rescue => err

#puts "!" * 80; p err
  unit.logger.warn(
    "#{self.class}#check()", err, '(returning true, aka remove me)')

  true # remove me
end

#msg_waiter?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
# File 'lib/flor/unit/waiter.rb', line 37

def msg_waiter?

  @serie.find { |_, points|
    points.find { |po|
      ! ROW_PSEUDO_POINTS.include?(po.split(':').first) } }
end

#notify(executor, message) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/flor/unit/waiter.rb', line 49

def notify(executor, message)

  @executor = executor
    # could be handy

  @mutex.synchronize do

    return false unless msg_match?(message)

    @serie.shift
    return false if @serie.any?

    @queue << [ executor, message ]
    @var.signal
  end

  true # serie over, remove me
end

#row_waiter?Boolean

“tasker”, not “task”, since “task” is already a message point

Returns:

  • (Boolean)


29
30
31
32
33
34
35
# File 'lib/flor/unit/waiter.rb', line 29

def row_waiter?

  @serie.find { |_, points|
    points.find { |po|
      pos = po.split(':')
      pos.length > 1 && ROW_PSEUDO_POINTS.include?(pos[0]) } }
end

#to_query_hashesObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/flor/unit/waiter.rb', line 121

def to_query_hashes

  @serie
    .inject([ [], [] ]) { |a, (nid, points)|

      points.each do |point|

        ss = point.split(':')

        h = {}
        h[:exid] = @exid if @exid
        h[:nid] = nid if nid

        case ss[0]
        when 'status'
          h[:status] = ss[1]
          a[0] << h
        when 'tag', 'tasker', 'var', 'variable'
          t = ss[0]; t = 'var' if t == 'variable'
          h[:type] = t
          h[:name] = ss[1]
          h[:value] = ss[2] if ss[2]
          a[1] << h
        else
          fail ArgumentError, "cannot turn to query_hash, #{self.inspect}"
        end
      end

      a }
end

#to_sObject



44
45
46
47
# File 'lib/flor/unit/waiter.rb', line 44

def to_s

  "#{super[0..-2]}#{{ exid: @exid, timeout: @timeout }.inspect}>"
end

#waitObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/flor/unit/waiter.rb', line 99

def wait

  @mutex.synchronize do

    if @queue.empty?

      @var.wait(@mutex, @timeout)
        # will wait "in aeternum" if @timeout is nil

      if @queue.empty?
        fail RuntimeError.new(
          "timeout for #{self.to_s}, " +
          "msg_waiter? #{ !! msg_waiter?}, row_waiter? #{ !! row_waiter?}"
        ) if @on_timeout == 'fail'
        return { 'exid' => @exid, 'timed_out' => @on_timeout }
      end
    end

    @queue.shift[1]
  end
end