Created
June 1, 2011 01:24
-
-
Save nutrun/1001610 to your computer and use it in GitHub Desktop.
Distributed process balancing example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jobs = [ | |
{:id => :job1, :weight => 3}, | |
{:id => :job2, :weight => 2}, | |
{:id => :job3, :weight => 2}, | |
{:id => :job4, :weight => 4}, | |
{:id => :job5, :weight => 4}, | |
{:id => :job6, :weight => 1} | |
] | |
nodes = [ | |
{:id => :node1, :weight => 0, :jobs => []}, | |
{:id => :node2, :weight => 0, :jobs => []}, | |
{:id => :node3, :weight => 0, :jobs => []}, | |
{:id => :node4, :weight => 0, :jobs => []} | |
] | |
jobs.sort! { |a, b| b[:weight] <=> a[:weight] } | |
jobs.each do |job| | |
nodes.sort! { |a, b| a[:weight] <=> b[:weight] } | |
nodes.first[:jobs] << job[:id] | |
nodes.first[:weight] += job[:weight] | |
end | |
require 'pp' | |
pp nodes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment