Skip to content

Commit 33b3681

Browse files
author
Matthew Peck
committed
invert logic in assign relay
1 parent 1cba931 commit 33b3681

File tree

2 files changed

+31
-39
lines changed

2 files changed

+31
-39
lines changed

lib/cog/command/pipeline/executor.ex

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,9 @@ defmodule Cog.Command.Pipeline.Executor do
228228
# fail_pipeline_with_error({:disabled_bundle, bundle}, state)
229229
#
230230
case assign_relay(relays, bundle_name, version) do
231-
{nil, _} ->
232-
fail_pipeline_with_error({:no_relays, bundle_name}, state)
233-
{:no_relay_group, _} ->
234-
fail_pipeline_with_error({:no_relay_group, bundle_name}, state)
235-
{:no_enabled_relays, _} ->
236-
fail_pipeline_with_error({:no_enabled_relays, bundle_name}, state)
237-
{relay, relays} ->
231+
{:error, reason} ->
232+
fail_pipeline_with_error({reason, bundle_name}, state)
233+
{:ok, {relay, relays}} ->
238234
topic = "/bot/commands/#{relay}/#{bundle_name}/#{command_name}"
239235
reply_to_topic = "#{state.topic}/reply"
240236
req = request_for_plan(current_plan, request, user, reply_to_topic, state.service_token)
@@ -671,35 +667,31 @@ defmodule Cog.Command.Pipeline.Executor do
671667
end
672668

673669
defp assign_relay(relays, bundle_name, bundle_version) do
674-
# If the bundle is assigned to a relay group carry on, otherwise bail
675-
if Cog.Repository.Bundles.assigned_to_group?(bundle_name) do
676-
case Map.get(relays, bundle_name) do
677-
# No assignment so let's pick one
678-
nil ->
679-
case Relays.pick_one(bundle_name, bundle_version) do
680-
# No relays available
681-
{:error, :no_relays} ->
682-
{nil, relays}
683-
# relays exist, but are disabled
684-
{:error, :no_enabled_relays} ->
685-
{:no_enabled_relays, relays}
686-
# Store the selected relay in the relay cache
687-
{:ok, relay} ->
688-
{relay, Map.put(relays, bundle_name, relay)}
689-
end
690-
# Relay was previously assigned
691-
relay ->
692-
# Is the bundle still available on the relay? If not, remove the current assignment from the cache
693-
# and select a new relay
694-
if Relays.relay_available?(relay, bundle_name, bundle_version) do
695-
{relay, relays}
696-
else
697-
relays = Map.delete(relays, bundle_name)
698-
assign_relay(relays, bundle_name, bundle_version)
699-
end
700-
end
701-
else
702-
{:no_relay_group, relays}
670+
case Map.get(relays, bundle_name) do
671+
# No assignment so let's pick one
672+
nil ->
673+
case Relays.pick_one(bundle_name, bundle_version) do
674+
# Store the selected relay in the relay cache
675+
{:ok, relay} ->
676+
{:ok, {relay, Map.put(relays, bundle_name, relay)}}
677+
error ->
678+
# Query DB to clarify error before reporting to the user
679+
if Cog.Repository.Bundles.assigned_to_group?(bundle_name) do
680+
error
681+
else
682+
{:error, :no_relay_group}
683+
end
684+
end
685+
# Relay was previously assigned
686+
relay ->
687+
# Is the bundle still available on the relay? If not, remove the current assignment from the cache
688+
# and select a new relay
689+
if Relays.relay_available?(relay, bundle_name, bundle_version) do
690+
{:ok, {relay, relays}}
691+
else
692+
relays = Map.delete(relays, bundle_name)
693+
assign_relay(relays, bundle_name, bundle_version)
694+
end
703695
end
704696
end
705697

lib/cog/relay/tracker.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ defmodule Cog.Relay.Tracker do
120120
end
121121

122122
@doc """
123-
Return a tuple with a list of ALL relays serving the bundle and a list of the
124-
ENABLED relays serving the bundle.
123+
Return a tuple with the list of relays serving the bundle or an error and
124+
a reason.
125125
"""
126-
@spec relays(t, bundle_name, version) :: {[String.t], [String.t]}
126+
@spec relays(t, bundle_name, version) :: {:ok, [String.t]} | {:error, atom()}
127127
def relays(tracker, bundle_name, bundle_version) when is_binary(bundle_name) do
128128
relays = Map.get(tracker.map, {bundle_name, bundle_version}, MapSet.new)
129129
enabled_relays = MapSet.difference(relays, tracker.disabled)

0 commit comments

Comments
 (0)