Skip to content

Instantly share code, notes, and snippets.

@sorentwo
Last active August 13, 2024 14:27
Show Gist options
  • Save sorentwo/d6be222091db7ba3c5b50d8bcabca252 to your computer and use it in GitHub Desktop.
Save sorentwo/d6be222091db7ba3c5b50d8bcabca252 to your computer and use it in GitHub Desktop.
Fly Cloud for Oban Auto Scaling
defmodule Oban.Pro.Clouds.Fly do
@moduledoc false
@behaviour Oban.Pro.Cloud
@enforce_keys [:app_id, :auth_token, :regions]
defstruct @enforce_keys
@url "https://api.fly.io/graphql"
@query """
mutation ScaleApp($input: ScaleAppInput!) {
scaleApp(input: $input) {
delta {
fromCount
toCount
}
}
}
"""
@impl Oban.Pro.Cloud
def init(opts) do
struct!(__MODULE__, opts)
end
@impl Oban.Pro.Cloud
def scale(quantity, %{app_id: app_id, auth_token: auth_token, regions: regions} = conf) do
regions = Enum.map(regions, &%{region: &1, count: quantity})
body = %{
query: @query,
variables: %{input: %{appId: app_id, regions: regions}}
}
Req.post!(@url, auth: {:bearer, auth_token}, json: body)
{:ok, conf}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment