Class: Cuted::Queue
- Inherits:
-
Object
- Object
- Cuted::Queue
- Defined in:
- lib/cuted/queue.rb
Instance Attribute Summary collapse
-
#queue ⇒ Object
Returns the value of attribute queue.
-
#running ⇒ Object
Returns the value of attribute running.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Queue
constructor
A new instance of Queue.
-
#peek ⇒ Object
Return the highest priority command.
-
#pop_run(continue = true) ⇒ Object
Find the highest priority item and run it.
-
#push_cmd(cmd) ⇒ Object
Takes the text of a command or any structure that responds to :run.
-
#sort ⇒ Object
Puts the highest priority commands first.
Constructor Details
#initialize(opts = {}) ⇒ Queue
Returns a new instance of Queue.
13 14 15 16 17 18 19 20 21 |
# File 'lib/cuted/queue.rb', line 13 def initialize(opts = {}) @queue = [] @running = [] # If the options are set, then use them; otherwise defaults. @defaults = opts[:defaults] || {} @max_concurrent = opts[:max_concurrent] || 4 @logger = opts[:logger] end |
Instance Attribute Details
#queue ⇒ Object
Returns the value of attribute queue.
11 12 13 |
# File 'lib/cuted/queue.rb', line 11 def queue @queue end |
#running ⇒ Object
Returns the value of attribute running.
11 12 13 |
# File 'lib/cuted/queue.rb', line 11 def running @running end |
Instance Method Details
#peek ⇒ Object
Return the highest priority command.
53 54 55 |
# File 'lib/cuted/queue.rb', line 53 def peek @queue[0] end |
#pop_run(continue = true) ⇒ Object
Find the highest priority item and run it.
If continue is true, then run other commands in the queue when done.
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 89 90 91 92 93 94 |
# File 'lib/cuted/queue.rb', line 59 def pop_run(continue = true) # If there are no commands, do nothing. if @queue.length < 1 then @logger.info 'No commands to run' if @logger return # If there isn't enough free weight to run, do nothing. elsif peek.weight > @max_concurrent - @running.size and @running.size > 0 then @logger.info 'Not enough free weight to run' if @logger return end # Pop off the highest priority command. cmd = @queue.delete_at(0) sort # Run the command and return it. @logger.info "Starting #{cmd}" if @logger # Find out the weight of this command. weight = cmd.respond_to?(:weight) ? cmd.weight : 1 # Add this command that many times to the running array. @running.concat([cmd] * weight) # In a separate thread... thread = Thread.new do # Run the command. cmd.run @logger.info "Finished #{cmd}" if @logger # Make the command remove itself from the running list. @running.delete_if { |o| o === cmd } # Try to run queued commands once this one is done if we're continuing. pop_run if continue end end |
#push_cmd(cmd) ⇒ Object
Takes the text of a command or any structure that responds to :run.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cuted/queue.rb', line 24 def push_cmd(cmd) @logger.info "Pushed command '#{cmd}'" if @logger # Really we just need a command to be able to run. if not cmd.respond_to?(:run) require 'cuted/command' cmd = Command.new(cmd) end # Set up the command with the default values. @defaults.each do |k, v| cmd.send("#{k}=", v) if cmd.respond_to?("#{k}=") end @queue << cmd sort cmd end |
#sort ⇒ Object
Puts the highest priority commands first.
45 46 47 48 49 50 |
# File 'lib/cuted/queue.rb', line 45 def sort # If there is no priority, use 0. @queue.sort_by! { |cmd| cmd.respond_to?(:priority) ? -cmd.priority : 0 } self end |