Skip to content

Instantly share code, notes, and snippets.

@sorentwo
Last active March 19, 2023 20:55
Embed
What would you like to do?
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