Skip to content

Instantly share code, notes, and snippets.

@sorentwo
Last active March 19, 2023 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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}) do
headers = [
{"Content-Type", "application/json"},
{"Accept", "application/json"},
{"Authorization", "Bearer #{auth_token}"}
]
regions = Enum.map(regions, &%{region: &1, count: quantity})
body = %{
query: @query,
variables: %{input: %{appId: app_id, regions: regions}}
}
Req.post!(@url, json: body, headers: headers)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment