Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nutrun/1001610 to your computer and use it in GitHub Desktop.
Save nutrun/1001610 to your computer and use it in GitHub Desktop.
Distributed process balancing example
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