-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add metered billing cron for services (#1285)
- Loading branch information
1 parent
35c7587
commit 20eb55e
Showing
9 changed files
with
80 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule Cron.Task.Metering do | ||
@moduledoc """ | ||
Backfills all repository readmes | ||
""" | ||
use Cron | ||
alias Core.Schema.PlatformSubscription | ||
alias Core.Services.Payments | ||
|
||
def run() do | ||
PlatformSubscription.metered() | ||
|> PlatformSubscription.ordered(asc: :id) | ||
|> Core.Repo.stream(method: :keyset) | ||
|> Core.throttle(count: 50, pause: :timer.seconds(1)) | ||
|> Flow.from_enumerable(stages: 5, max_demand: 5) | ||
|> Flow.map(&deliver/1) | ||
|> log() | ||
end | ||
|
||
defp deliver(%PlatformSubscription{} = subscription) do | ||
Logger.info "sending usage records for account #{subscription.account_id}" | ||
Payments.send_usage(subscription) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule Cron.Task.MeteringTest do | ||
use Core.SchemaCase, async: false | ||
use Mimic | ||
alias Cron.Task.Metering | ||
|
||
setup :set_mimic_global | ||
|
||
describe "#run/0" do | ||
test "it will sync usage for all updated accounts" do | ||
account = insert(:account) | ||
insert(:platform_subscription, account: account, metered_id: "id_1") | ||
insert(:cluster, account: account, service_count: 2) | ||
insert(:cluster, account: account, service_count: 4) | ||
expect(Stripe.SubscriptionItem.Usage, :create, fn "id_1", %{action: :set, quantity: 2} -> {:ok, "id_1"} end) | ||
|
||
1 = Metering.run() | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
Mimic.copy(Core.Conduit.Broker) | ||
Mimic.copy(Stripe.SubscriptionItem.Usage) | ||
ExUnit.configure formatters: [JUnitFormatter, ExUnit.CLIFormatter] | ||
ExUnit.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters