Skip to content

Instantly share code, notes, and snippets.

@prio
Last active August 29, 2015 14:23
Show Gist options
  • Save prio/35fae57ce4050c5bcbc0 to your computer and use it in GitHub Desktop.
Save prio/35fae57ce4050c5bcbc0 to your computer and use it in GitHub Desktop.
get_best_pid
defmodule Scheduler do
def get_best_pid(group) do
mems = :pg2.get_members(group) |>
Enum.map(fn(pid) ->
[message_queue_len: msgs, stack_size: ss] = :rpc.pinfo(pid, [:message_queue_len, :stack_size])
{pid, msgs, ss}
end) |>
Enum.sort(fn ({_, q1, s1}, {_, q2, s2}) -> q1 < q2 or (q1 == q2 and s1 < s2) end)
case mems do
[{pid, _, _} | _] -> pid
[] -> {:error, :empty_process_group}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment