[Why] In some cases,the query for exiting the monitor may not be the same as the query for monitoring. To meet this requirement, an additional zone stop_query is added for processing. Example: query = {"job_id" => 1}, means I want to monitoring the job 1. stop_query = {"job_state" => "extract_finished"}, means I want to stop monitoring when I get a log, "job_state" equals to "extract_finished".
Signed-off-by: Wu Zhende wuzhende666@163.com --- lib/monitor.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/lib/monitor.rb b/lib/monitor.rb index baf172d3..fcf7e220 100755 --- a/lib/monitor.rb +++ b/lib/monitor.rb @@ -11,7 +11,7 @@ require 'eventmachine' require 'json'
class Monitor - attr_accessor :monitor_url, :query, :overrides, :action, :job, :result + attr_accessor :monitor_url, :query, :overrides, :action, :job, :result, :stop_query
def initialize(monitor_url = '', query = {}, action = {}) @monitor_url = monitor_url @@ -26,6 +26,7 @@ class Monitor @defaults = {} load_default @result = [] + @stop_query = {} end
def load_default @@ -100,6 +101,13 @@ class Monitor EM.stop end
+ def stop(data, web_socket) + @stop_query.each do |key, value| + return false unless data[key] == value + end + stop_em(web_socket) + end + def run(timeout = nil) merge_overrides field_check @@ -128,7 +136,7 @@ class Monitor output(data) connect(data, ws)
- stop_em(ws) if @action['stop'] + stop(data, ws) if @action['stop'] end
ws.on :close do |event|