Class: RuleEngine::RunQueue
- Inherits:
-
Object
- Object
- RuleEngine::RunQueue
- Defined in:
- lib/cirrocumulus/rules/run_queue.rb
Instance Method Summary collapse
- #enqueue(match_data) ⇒ Object
-
#initialize(engine) ⇒ RunQueue
constructor
A new instance of RunQueue.
- #run_queued_rules ⇒ Object
Constructor Details
#initialize(engine) ⇒ RunQueue
Returns a new instance of RunQueue.
21 22 23 24 25 26 |
# File 'lib/cirrocumulus/rules/run_queue.rb', line 21 def initialize(engine) @engine = engine @queue = [] @running_rules = [] @mutex = Mutex.new end |
Instance Method Details
#enqueue(match_data) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/cirrocumulus/rules/run_queue.rb', line 28 def enqueue(match_data) @mutex.synchronize do if !already_queued?(match_data.rule, match_data.parameters) debug "Enqueueing rule #{match_data.rule.name}#{match_data.parameters.inspect}" if !match_data.rule..blank? && match_data.rule..include?(:for) delay = match_data.rule.[:for] run_at = Time.now + delay debug "Will run this rule after #{delay.to_s} sec (at #{run_at.strftime("%H:%M:%S %d.%m.%Y")})" @queue << QueueEntry.new(match_data, run_at) else @queue << QueueEntry.new(match_data) end end end end |
#run_queued_rules ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 |
# File 'lib/cirrocumulus/rules/run_queue.rb', line 45 def run_queued_rules() rules_for_running = Queue.new @mutex.synchronize do count = @queue.count {|entry| entry.state == :queued} if count > 0 debug "There are #{count} rules waiting, running.." if count > 1 idx = 0 while count > 0 entry = @queue[idx] if entry.state != :queued idx += 1 next end if entry.run_at.blank? || (entry.run_at <= Time.now) rules_for_running << entry entry.state = :running end idx += 1 count -= 1 end end end while !rules_for_running.empty? do entry = rules_for_running.pop if entry.run_data.matched_facts.all? {|fact| !fact.is_deleted} # TODO: there should be smth like WeakReference begin debug "Executing #{entry.rule.name}#{entry.params.inspect}" entry.rule.code.call(@engine, entry.params) entry.state = :finished # TODO: cleanup @queue from this entries rescue Exception => e Log4r::Logger['kb'].warn "Exception while executing rule: %s\n%s" % [e.to_s, e.backtrace.to_s] end else entry.state = :finished end end end |